Bài giảng SQL Server
Trang 1I Quản lý bán hàng:
1 Bài 1
SELECT MaKH, TenKH, DiaChi, MaSoThue, TaiKhoan, TenNganHang,
GhiChu
FROM dbo.DM_NhaCungCap
WHERE (DiaChi LIKE N'%Hà nội%')
2 Bài 2
dbo.DM_LoaiPhieuXuatNhap.TenLoai, dbo.DM_NhanVien.TenNhanVien
FROM 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.MaKH
WHERE (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 3
4 Bài 4
5 Bài 5
6 Bài 6
7 Bài 7
8 Bài 8
Trang 29 Bài 9
10 Bài 10
II Quản lý sinh viên:
SELECT MaSV, Ho_dem, Ten, NgaySinh, GioiTinh, MaLop
FROM dbo.HSSV
WHERE (YEAR(NgaySinh) = 1986) AND (Ten LIKE N'%Minh')
2 Bài 2
SELECT dbo.HSSV.MaSV, dbo.HSSV.Ho_dem, dbo.HSSV.Ten, dbo.HSSV.NgaySinh, dbo.HSSV.GioiTinh, dbo.DMLop.TenLop
FROM dbo.HSSV INNER JOIN
dbo.DMLop ON dbo.HSSV.MaLop = dbo.DMLop.MaLop
WHERE (dbo.DMLop.TenLop LIKE N'%Tin 3B%')
3 Bài 3
SELECT MaLop, COUNT(MaSV) AS SiSo
FROM dbo.HSSV
GROUP BY MaLop
4 Bài 4
SELECT dbo.DMMon.MaMon, dbo.DMMon.TenMon, dbo.DMMon.SoTiet, dbo.DMMon.SoDVHT, dbo.DMMon.HThuc
FROM dbo.KHGDHK INNER JOIN
dbo.DMMon ON dbo.KHGDHK.MaMon = dbo.DMMon.MaMon INNER JOIN
dbo.DMLop ON dbo.KHGDHK.MaLop = dbo.DMLop.MaLop
Trang 35 Bài 5
SELECT MaSV, TongD / TongDVHT AS DTB
FROM (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 A
6 Bài 6
SELECT dbo.HSSV.MaSV, dbo.HSSV.Ho_dem, dbo.HSSV.Ten, dbo.HSSV.NgaySinh, dbo.HSSV.GioiTinh, dbo.HSSV.MaLop FROM 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.MaMon WHERE (dbo.KHGDHK.MaHocKy = '07082') AND (dbo.DIEM.Diem1 < 5) AND (dbo.DMMon.TenMon LIKE N'Lập trình C')
7 Bài 7
SELECT MaSV, Ho_dem, Ten, NgaySinh, GioiTinh, MaLop
FROM dbo.HSSV
WHERE (MaSV NOT IN
(SELECT MaSV
FROM dbo.DIEM
WHERE (Diem1 < 5)))
Trang 48 Bài 8
SELECT DK1.MaSV, DK1.Ho_dem, DK1.Ten, DK1.NgaySinh, DK1.GioiTinh
FROM (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.MaSV
9 Bài 9
SELECT dbo.HSSV.MaSV, dbo.HSSV.Ho_dem, dbo.HSSV.Ten, dbo.HSSV.NgaySinh, dbo.HSSV.GioiTinh, dbo.HSSV.MaLop FROM (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
Trang 5(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.MaSV
III Quản lý nhân sự:
1 Bài 1
SELECT MaCB, HoDem, Ten, NgaySinh, GioiTinh, MaQue, NgayVaoDang, NgayNhapNgu, NgayXuatNgu
FROM dbo.HSCB
WHERE (MaQue LIKE N'%Hà Tây') AND (YEAR(NgayNhapNgu) BETWEEN 1960 AND 1965)
2 Bài 2
SELECT MaCB, HoDem, Ten, NgaySinh, GioiTinh, MaQue, NgayVaoDang, NgayNhapNgu, NgayXuatNgu
FROM dbo.HSCB
WHERE (YEAR(GETDATE()) - YEAR(NgaySinh) >= 50) AND (GioiTinh = 0) OR
(YEAR(GETDATE()) - YEAR(NgaySinh) >= 55) AND (GioiTinh = 1)
3 Bài 3
Trang 6SELECT MaDonVi, COUNT(MaCB) AS SiSoCB
FROM dbo.QTCongTac
WHERE (ToiNgay = NULL)
GROUP BY MaDonVi
4 Bài 4
SELECT TOP (100) PERCENT dbo.HSCB.HoDem, dbo.HSCB.Ten, dbo.DMDonVi.TenDonVi, dbo.DMChucVu.TenChucVu, dbo.QTCongTac.TuNgay,
dbo.QTCongTac.ToiNgay
FROM 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.MaChucVu
WHERE (dbo.HSCB.HoDem LIKE N'Nguyễn Văn') AND (dbo.HSCB.Ten LIKE N'A')
ORDER BY dbo.QTCongTac.TuNgay
5 Bài 5
SELECT dbo.HSCB.MaCB, dbo.HSCB.HoDem, dbo.HSCB.Ten, dbo.DMChucVu.TenChucVu
FROM dbo.DMChucVu INNER JOIN
dbo.QTCongTac ON dbo.DMChucVu.MaChucVu = dbo.QTCongTac.MaChucVu INNER JOIN
dbo.HSCB ON dbo.QTCongTac.MaCB = dbo.HSCB.MaCB
WHERE (dbo.DMChucVu.TenChucVu LIKE N'Phó phòng') AND (dbo.QTCongTac.ToiNgay IS NULL)
6 Bài 6
SELECT dbo.HSCB.MaCB, dbo.HSCB.HoDem, dbo.HSCB.Ten, dbo.DMChucVu.TenChucVu, dbo.QTCongTac.TuNgay, dbo.QTCongTac.ToiNgay
FROM dbo.DMChucVu INNER JOIN
dbo.QTCongTac ON dbo.DMChucVu.MaChucVu = dbo.QTCongTac.MaChucVu INNER JOIN
Trang 77 Bài 7
SELECT MaCB, HoDem, Ten, NgaySinh, GioiTinh, MaQue, NgayVaoDang, NgayNhapNgu, NgayXuatNgu
FROM dbo.HSCB
WHERE (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 8
SELECT ChuyenDen.TongCBChuyenDen, ChuyenDi.TongCBChuyenDi
FROM (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
Trang 8WHERE (YEAR(QTCongTac_1.ToiNgay) >= 2005) AND (DMDonVi_1.TenDonVi LIKE N'Phòng đào tạo') GROUP BY QTCongTac_1.MaCB) AS ChuyenDi
9 Bài 9
SELECT Nam.MaDonVi, Nam.TNam, Nu.TNu, Truong.TTruong, Pho.TPho, CanBo.TCanBo, Tong.TCB
FROM (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%')
Trang 9Truong.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