Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

create table chitietHD (

SoHD varchar(255) not null,


MaHg varchar(255) not null,
SoLuong int,
GiaBan int,
primary key (SoHD),
foreign key (MaHg) references HangHoa(MaHg)
);

insert into Hanghoa(MaHg, TenHg, DVT, Dongia, Cohang)


values ('H001', "Bia", "lon", 10000, 1);
insert into Khach(MaKH, Hoten, Diachi, Daily)
values ('001', "Thanh", "Da Nang", 0);
insert into HoaDon(SoHD, Ngaylap, Ngaygiao, Trigia, MaKH)
values ('HD001', '2021-09-18', '2021-09-20', 2000000, 'KH001');
insert into Chitiet_HD(SoHD, MaHG, soluong, Giaban)
values ('HD001', 'KH001', 200, 10000);

a) select Hoten, DiaChi


from Khach
where Daily=1;

b) select Trigia
from Hoadon
where NgayLap='2008-05-12';

c) select a.Hoten, a.Diachi


from Khach a, Hoadon b
where a.MaKH = b.MaKH and a.Daily = 0 and b.Ngaylap = '2008-12-15';

d) select a.MaHG, a.TenHG, a.DVT


from Hanghoa a, HoaDon b, Chitiet_HD c
where a.MaHG = c.MaHG and b.SoHD = c.SoHD and b.Ngaylap = '2008-12-15';

e) select MaHG, TenHG


from Hanghoa
where Cohang = 1;

f) select a.MaHg, a.TenHG


from Hanghoa a, HoaDon b, Chitiet_HD c
where a.MaHG = c.MaHG and b.SoHD = c.SoHD and Trigia > 1000;

g) select distinc TenHG


from Hanghoa
where TenHG not in (select distinc a.TenHG
from HangHoa a, HoaDon b, Chitiet_HD c
where a.MaHG = c.MaHG and b.SoHD = c.SoHD);
h) select distinc MaKH, TenKH
from HangHoa
where Daily = 1 and MaKH not in (select distinc MaKH
from Khach a, HoaDon b, Chitiet_HD c
where a.MaKH = b.MaKH and b.SoHD = c.SoHD and (c.MaHG like
'H001' or c.MaHG like 'H002'));

i) select distinc a.TenHG


from Hang a, Khach b, Hoadon c, Chitiet_HD d
where a.MaHG = d.MaHG and c.SoHD = d.SoHD and b.MaKH = c.MaKH and Daily = 1 and
Diachi like "Da Nang" and in
(select distinc a.TenHG
from Hang a, Khach b, Hoadon c, Chitiet_HD d
where a.MaHG = d.MaHG and c.SoHD = d.SoHD and b.MaKH = c.MaKH and Daily = 0 and
b.Diachi like "Quang Nam");

j)
k)

l) select a.MaKH, a.HoTen, min(c.Giaban) as GiabanThapNhat


from Khach a, HoaDon b, Chitiet_HD c
where a.MaKH = b.MaKh and b.SoHD = c.SoHD and a.Daily = 1
group by a.MaKH, a.HoTen
having GiaBanThapNhat > 100;

m) select sum(b.Soluong)
from HoaDon a, Chitiet_HD b
where a.SoHD = b.SoHD and a.Ngaylap >= '2008-02-01' and a.Ngaylap <= '2008-29-02';

n) select SoHD, Ngaylap


from Hoadon
group by SoHD, Ngaylap
having giatri >= (select max(Giatri) from Hoadon);

o) select a.Hoten, a.Diachi


from Khach a, HoaDon b
where a.MaKH = b.MaKH and b.Ngaylap >= '2008-02-01' and b.Ngaylap <= '2008-02-29'
group by a.HoTen, a.Diachi
having b.Giatri >= (select max(Giatri) from HoaDon where Ngaylap >= '2008-02-01'
and Ngaylap <= '2008-02-29');

p)select a.MaKH, count(b.SoHD)


from Khach a, Hoadon b
where a.MaKH = b.MaKH and a.Daily = 1
group by a.MaKH

q) select TOP 1 MaHG, TenHG, sum(b.Soluong) as TongSoLuong


from Hang a, Chitiet_HD b
where a.MaHG = b.MaHG
group by MaHG, TenHG
order by TongSoLuong desc

You might also like