Bài tập SQL Server:
I. 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
SELECT dbo.DM_HoaDon.MaHD, dbo.DM_HoaDon.NgayLapHD, dbo.DM_NhaCungCap.TenKH,
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
9. Bài 9
10. Bài 10
II. Quản lý sinh viên:
1. Bài 1
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
WHERE (dbo.KHGDHK.MaHocKy LIKE '07082') AND (dbo.DMLop.TenLop LIKE N'%Tin 3B%')
5. 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)))
8. 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
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.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