Bài giảng SQL Server
Bài tập SQL Server:I. Quản lý bán hàng:1. Bài 1SELECT MaKH, TenKH, DiaChi, MaSoThue, TaiKhoan, TenNganHang, GhiChuFROM dbo.DM_NhaCungCapWHERE (DiaChi LIKE N'%Hà nội%')2. Bài 2SELECT dbo.DM_HoaDon.MaHD, dbo.DM_HoaDon.NgayLapHD, dbo.DM_NhaCungCap.TenKH, dbo.DM_LoaiPhieuXuatNhap.TenLoai, dbo.DM_NhanVien.TenNhanVienFROM dbo.DM_HoaDon INNER JOIN dbo.DM_LoaiPhieuXuatNhap ON dbo.DM_HoaDon.MaLoai = dbo.DM_LoaiPhieuXuatNhap.MaLoai INNER JOIN dbo.DM_NhanVien ON dbo.DM_HoaDon.MaNV = dbo.DM_NhanVien.MaNV INNER JOIN dbo.DM_NhaCungCap ON dbo.DM_HoaDon.MaKH = dbo.DM_NhaCungCap.MaKHWHERE (dbo.DM_HoaDon.MaLoai LIKE 'X%') AND (dbo.DM_HoaDon.NgayLapHD >= CONVERT(DATETIME, '2008-04-01 00:00:00', 102)) AND (dbo.DM_HoaDon.NgayLapHD <= CONVERT(DATETIME, '2008-04-30 00:00:00', 102))3. Bài 34. Bài 45. Bài 56. Bài 67. Bài 78. Bài 8 9. Bài 910. Bài 10II. Quản lý sinh viên:1. Bài 1SELECT MaSV, Ho_dem, Ten, NgaySinh, GioiTinh, MaLopFROM dbo.HSSVWHERE (YEAR(NgaySinh) = 1986) AND (Ten LIKE N'%Minh')2. Bài 2SELECT dbo.HSSV.MaSV, dbo.HSSV.Ho_dem, dbo.HSSV.Ten, dbo.HSSV.NgaySinh, dbo.HSSV.GioiTinh, dbo.DMLop.TenLopFROM dbo.HSSV INNER JOIN dbo.DMLop ON dbo.HSSV.MaLop = dbo.DMLop.MaLopWHERE (dbo.DMLop.TenLop LIKE N'%Tin 3B%')3. Bài 3SELECT MaLop, COUNT(MaSV) AS SiSoFROM dbo.HSSVGROUP BY MaLop4. Bài 4SELECT dbo.DMMon.MaMon, dbo.DMMon.TenMon, dbo.DMMon.SoTiet, dbo.DMMon.SoDVHT, dbo.DMMon.HThucFROM dbo.KHGDHK INNER JOIN dbo.DMMon ON dbo.KHGDHK.MaMon = dbo.DMMon.MaMon INNER JOIN dbo.DMLop ON dbo.KHGDHK.MaLop = dbo.DMLop.MaLop WHERE (dbo.KHGDHK.MaHocKy LIKE '07082') AND (dbo.DMLop.TenLop LIKE N'%Tin 3B%')5. Bài 5SELECT MaSV, TongD / TongDVHT AS DTBFROM (SELECT dbo.DIEM.MaSV, SUM(dbo.DIEM.Diem1 * dbo.DMMon.SoDVHT) AS TongD, SUM(dbo.DMMon.SoDVHT) AS TongDVHT FROM dbo.DIEM INNER JOIN dbo.DMMon ON dbo.DIEM.MaMon = dbo.DMMon.MaMon INNER JOIN dbo.KHGDHK ON dbo.DMMon.MaMon = dbo.KHGDHK.MaMon WHERE (dbo.KHGDHK.MaHocKy LIKE '07082') GROUP BY dbo.DIEM.MaSV) AS A6. Bài 6SELECT dbo.HSSV.MaSV, dbo.HSSV.Ho_dem, dbo.HSSV.Ten, dbo.HSSV.NgaySinh, dbo.HSSV.GioiTinh, dbo.HSSV.MaLopFROM dbo.HSSV INNER JOIN dbo.KHGDHK ON dbo.HSSV.MaLop = dbo.KHGDHK.MaLop INNER JOIN dbo.DIEM ON dbo.HSSV.MaSV = dbo.DIEM.MaSV INNER JOIN dbo.DMMon ON dbo.KHGDHK.MaMon = dbo.DMMon.MaMon AND dbo.DIEM.MaMon = dbo.DMMon.MaMonWHERE (dbo.KHGDHK.MaHocKy = '07082') AND (dbo.DIEM.Diem1 < 5) AND (dbo.DMMon.TenMon LIKE N'Lập trình C')7. Bài 7SELECT MaSV, Ho_dem, Ten, NgaySinh, GioiTinh, MaLopFROM dbo.HSSVWHERE (MaSV NOT IN (SELECT MaSV FROM dbo.DIEM WHERE (Diem1 < 5))) 8. Bài 8SELECT DK1.MaSV, DK1.Ho_dem, DK1.Ten, DK1.NgaySinh, DK1.GioiTinhFROM (SELECT MaSV, Ho_dem, Ten, NgaySinh, GioiTinh FROM dbo.HSSV WHERE (MaSV NOT IN (SELECT MaSV FROM dbo.DIEM WHERE (Diem1 < 7)))) AS DK1 INNER JOIN (SELECT MaSV, TongD / TongDVHT AS DTB FROM (SELECT DIEM_1.MaSV, SUM(DIEM_1.Diem1 * dbo.DMMon.SoDVHT) AS TongD, SUM(dbo.DMMon.SoDVHT) AS TongDVHT FROM dbo.DIEM AS DIEM_1 INNER JOIN dbo.DMMon ON DIEM_1.MaMon = dbo.DMMon.MaMon INNER JOIN dbo.KHGDHK ON dbo.DMMon.MaMon = dbo.KHGDHK.MaMon WHERE (dbo.KHGDHK.MaHocKy LIKE '07082') GROUP BY DIEM_1.MaSV) AS A WHERE (TongD / TongDVHT >= 8)) AS DK2 ON DK1.MaSV = DK2.MaSV9. Bài 9SELECT dbo.HSSV.MaSV, dbo.HSSV.Ho_dem, dbo.HSSV.Ten, dbo.HSSV.NgaySinh, dbo.HSSV.GioiTinh, dbo.HSSV.MaLopFROM (SELECT DiemTBMax.MaSV FROM (SELECT TOP (1) MaSV, TongD / TongDVHT AS DTB FROM (SELECT DIEM_1.MaSV, SUM(DIEM_1.Diem1 * dbo.DMMon.SoDVHT) AS TongD, SUM(dbo.DMMon.SoDVHT) AS TongDVHT FROM dbo.DIEM AS DIEM_1 INNER JOIN dbo.DMMon ON DIEM_1.MaMon = dbo.DMMon.MaMon INNER JOIN dbo.KHGDHK ON dbo.DMMon.MaMon = dbo.KHGDHK.MaMon WHERE (dbo.KHGDHK.MaHocKy LIKE '07082') GROUP BY DIEM_1.MaSV) AS A) AS DiemTBMax INNER JOIN (SELECT MaSV, TongD / TongDVHT AS DTB FROM (SELECT DIEM_1.MaSV, SUM(DIEM_1.Diem1 * DMMon_1.SoDVHT) AS TongD, SUM(DMMon_1.SoDVHT) AS TongDVHT FROM dbo.DIEM AS DIEM_1 INNER JOIN dbo.DMMon AS DMMon_1 ON DIEM_1.MaMon = DMMon_1.MaMon INNER JOIN dbo.KHGDHK AS KHGDHK_1 ON DMMon_1.MaMon = KHGDHK_1.MaMon WHERE (KHGDHK_1.MaHocKy LIKE '07082') GROUP BY DIEM_1.MaSV) AS A_1) AS DiemTB ON DiemTBMax.DTB = DiemTB.DTB) AS X INNER JOIN dbo.HSSV ON X.MaSV = dbo.HSSV.MaSVIII. Quản lý nhân sự:1. Bài 1SELECT MaCB, HoDem, Ten, NgaySinh, GioiTinh, MaQue, NgayVaoDang, NgayNhapNgu, NgayXuatNguFROM dbo.HSCBWHERE (MaQue LIKE N'%Hà Tây') AND (YEAR(NgayNhapNgu) BETWEEN 1960 AND 1965)2. Bài 2SELECT MaCB, HoDem, Ten, NgaySinh, GioiTinh, MaQue, NgayVaoDang, NgayNhapNgu, NgayXuatNguFROM dbo.HSCBWHERE (YEAR(GETDATE()) - YEAR(NgaySinh) >= 50) AND (GioiTinh = 0) OR (YEAR(GETDATE()) - YEAR(NgaySinh) >= 55) AND (GioiTinh = 1)3. Bài 3 SELECT MaDonVi, COUNT(MaCB) AS SiSoCBFROM dbo.QTCongTacWHERE (ToiNgay = NULL)GROUP BY MaDonVi4. Bài 4SELECT TOP (100) PERCENT dbo.HSCB.HoDem, dbo.HSCB.Ten, dbo.DMDonVi.TenDonVi, dbo.DMChucVu.TenChucVu, dbo.QTCongTac.TuNgay, dbo.QTCongTac.ToiNgayFROM dbo.QTCongTac INNER JOIN dbo.HSCB ON dbo.QTCongTac.MaCB = dbo.HSCB.MaCB INNER JOIN dbo.DMDonVi ON dbo.QTCongTac.MaDonVi = dbo.DMDonVi.MaDonVi INNER JOIN dbo.DMChucVu ON dbo.QTCongTac.MaChucVu = dbo.DMChucVu.MaChucVuWHERE (dbo.HSCB.HoDem LIKE N'Nguyễn Văn') AND (dbo.HSCB.Ten LIKE N'A')ORDER BY dbo.QTCongTac.TuNgay5. Bài 5SELECT dbo.HSCB.MaCB, dbo.HSCB.HoDem, dbo.HSCB.Ten, dbo.DMChucVu.TenChucVuFROM dbo.DMChucVu INNER JOIN dbo.QTCongTac ON dbo.DMChucVu.MaChucVu = dbo.QTCongTac.MaChucVu INNER JOIN dbo.HSCB ON dbo.QTCongTac.MaCB = dbo.HSCB.MaCBWHERE (dbo.DMChucVu.TenChucVu LIKE N'Phó phòng') AND (dbo.QTCongTac.ToiNgay IS NULL)6. Bài 6SELECT dbo.HSCB.MaCB, dbo.HSCB.HoDem, dbo.HSCB.Ten, dbo.DMChucVu.TenChucVu, dbo.QTCongTac.TuNgay, dbo.QTCongTac.ToiNgayFROM dbo.DMChucVu INNER JOIN dbo.QTCongTac ON dbo.DMChucVu.MaChucVu = dbo.QTCongTac.MaChucVu INNER JOIN dbo.HSCB ON dbo.QTCongTac.MaCB = dbo.HSCB.MaCBWHERE (dbo.DMChucVu.TenChucVu LIKE N'Trưởng Phòng')7. Bài 7SELECT MaCB, HoDem, Ten, NgaySinh, GioiTinh, MaQue, NgayVaoDang, NgayNhapNgu, NgayXuatNguFROM dbo.HSCBWHERE (MaCB IN (SELECT B.MaCB FROM (SELECT TOP (1) MaCB, COUNT(MaCB) AS SoLanLuanChuyen FROM dbo.QTCongTac GROUP BY MaCB ORDER BY SoLanLuanChuyen DESC) AS A INNER JOIN (SELECT TOP (100) PERCENT MaCB, COUNT(MaCB) AS SoLanLuanChuyen FROM dbo.QTCongTac AS QTCongTac_1 GROUP BY MaCB ORDER BY SoLanLuanChuyen DESC) AS B ON A.SoLanLuanChuyen = B.SoLanLuanChuyen))8. Bài 8SELECT ChuyenDen.TongCBChuyenDen, ChuyenDi.TongCBChuyenDiFROM (SELECT COUNT(dbo.QTCongTac.MaCB) AS TongCBChuyenDen FROM dbo.QTCongTac INNER JOIN dbo.DMDonVi ON dbo.QTCongTac.MaDonVi = dbo.DMDonVi.MaDonVi WHERE (YEAR(dbo.QTCongTac.TuNgay) >= 2005) AND (dbo.DMDonVi.TenDonVi LIKE N'Phòng đào tạo') GROUP BY dbo.QTCongTac.MaCB) AS ChuyenDen CROSS JOIN (SELECT COUNT(QTCongTac_1.MaCB) AS TongCBChuyenDi FROM dbo.QTCongTac AS QTCongTac_1 INNER JOIN dbo.DMDonVi AS DMDonVi_1 ON QTCongTac_1.MaDonVi = DMDonVi_1.MaDonVi WHERE (YEAR(QTCongTac_1.ToiNgay) >= 2005) AND (DMDonVi_1.TenDonVi LIKE N'Phòng đào tạo') GROUP BY QTCongTac_1.MaCB) AS ChuyenDi9. Bài 9SELECT Nam.MaDonVi, Nam.TNam, Nu.TNu, Truong.TTruong, Pho.TPho, CanBo.TCanBo, Tong.TCBFROM (SELECT dbo.QTCongTac.MaDonVi, COUNT(dbo.QTCongTac.MaCB) AS TNam FROM dbo.QTCongTac INNER JOIN dbo.HSCB ON dbo.QTCongTac.MaCB = dbo.HSCB.MaCB WHERE (dbo.HSCB.GioiTinh = 1) GROUP BY dbo.QTCongTac.MaDonVi) AS Nam INNER JOIN (SELECT QTCongTac_5.MaDonVi, COUNT(QTCongTac_5.MaCB) AS TNu FROM dbo.QTCongTac AS QTCongTac_5 INNER JOIN dbo.HSCB AS HSCB_1 ON QTCongTac_5.MaCB = HSCB_1.MaCB WHERE (HSCB_1.GioiTinh = 0) GROUP BY QTCongTac_5.MaDonVi) AS Nu ON Nam.MaDonVi = Nu.MaDonVi INNER JOIN (SELECT MaDonVi, COUNT(MaCB) AS TTruong FROM dbo.QTCongTac AS QTCongTac_4 WHERE (MaChucVu LIKE N'T%') GROUP BY MaDonVi) AS Truong ON Nam.MaDonVi = Truong.MaDonVi AND Nu.MaDonVi = Truong.MaDonVi INNER JOIN (SELECT MaDonVi, COUNT(MaCB) AS TPho FROM dbo.QTCongTac AS QTCongTac_3 WHERE (MaChucVu LIKE N'P%') GROUP BY MaDonVi) AS Pho ON Nam.MaDonVi = Pho.MaDonVi AND Nu.MaDonVi = Pho.MaDonVi AND Truong.MaDonVi = Pho.MaDonVi INNER JOIN (SELECT MaDonVi, COUNT(MaCB) AS TCanBo FROM dbo.QTCongTac AS QTCongTac_2 WHERE (MaChucVu LIKE N'CB%') GROUP BY MaDonVi) AS CanBo ON Nam.MaDonVi = CanBo.MaDonVi AND Nu.MaDonVi = CanBo.MaDonVi AND Truong.MaDonVi = CanBo.MaDonVi AND Pho.MaDonVi = CanBo.MaDonVi INNER JOIN (SELECT MaDonVi, COUNT(MaCB) AS TCB FROM dbo.QTCongTac AS QTCongTac_1 GROUP BY MaDonVi) AS Tong ON Nam.MaDonVi = Tong.MaDonVi AND Nu.MaDonVi = Tong.MaDonVi AND Truong.MaDonVi = Tong.MaDonVi AND Pho.MaDonVi = Tong.MaDonVi AND CanBo.MaDonVi = Tong.MaDonVi . '2008-04-30 00:00:00', 102))3. Bài 34. Bài 45. Bài 56. Bài 67. Bài 78. Bài 8 9. Bài 910. Bài 10II. Quản lý sinh viên:1. Bài 1SELECT MaSV, Ho_dem, Ten,. Bài tập SQL Server: I. Quản lý bán hàng:1. Bài 1SELECT MaKH, TenKH, DiaChi, MaSoThue, TaiKhoan,