You are on page 1of 4

I-

Phn query to bng

a) To bng phongban create table phongban ( tenphong text(50), maphg text(5) primary key, trphg text(5), ng_nchuc datetime ) b) To bng nhanvien create table nhanvien ( honv text(50), tennv text(50), manv text(5) primary key, ngaysinh datetime, dchi text(50), phai bit, luong int, ma_nql text(5), phg text(5), foreign key (phg) references phongban(maphg) )
Trang 1

c) To bng diadiem create table diadiem ( maphg text(5) primary key, diadiem text(50), foreign key (maphg) references phongban(maphg) ) d) To bng thannhan create table thannhan ( manv text(5), tentn text(50), phai bit, ngaysinh datetime, quanhe text(50), primary key (manv,tentn), foreign key (manv) references nhanvien(manv) ) e) To bng dean create table dean ( tenda text(50), mada text(5) primary key, ddiem_da text(50), phong text(5), foreign key (phong) references phongban(maphg) ) f) To bng phancong create table phancong ( ma_nv text(5), soda text(5), thoigian int, primary key(ma_nv,soda), foreign key(ma_nv) references nhanvien(manv), foreign key(soda) references dean(mada) )
Trang 2

II- Cc cu SELECT query


a. Tm nhng nhn vin lm vic phng s 4. SELECT * FROM nhanvien WHERE phg='4 '; b. Tm nhng nhn vin c mc lng trn 30.000. SELECT * FROM nhanvien WHERE luong>30000; c. Tm cc nhn vin c mc lng > 25.000 phng s 4 hoc cc nhn vin c mc lng > 30.000 phng s 5. SELECT * FROM nhanvien WHERE (luong>25000 and phg='4') or (luong>30000 and phg='5'); d. Cho bit h v tn ca cc nhn vin TPHCM SELECT honv, tennv FROM nhanvien WHERE dchi LIKE 'TP HCM'; e. Cho bit ngy sinh v a ch ca nhn vin Trn Thanh Nam SELECT ngaysinh, dchi FROM nhanvien WHERE honv='Trn Thanh' And tennv='Nam' f. Vi mi phng ban cho bit tn phng ban v a im phng SELECT tenphong, diadiem FROM phongban, diadiem WHERE phongban.maphg=diadiem.maphg; g. Tm nhng ngi trng phng tng phng ban SELECT maphg, tenphong, nhanvien.* FROM phongban, nhanvien WHERE phongban.maphg=nhanvien.phg and manv in (select distinct trphg from phongban); h. Tm tn v a ch ca cc nhn vin phng Nghin cu SELECT nhanvien.* FROM nhanvien, phongban WHERE nhanvien.phg=phongban.maphg and tenphong='Nghin cu'; i. Tm tn nhng n nhn vin v tn ngi thn ca h SELECT tennv FROM nhanvien, thannhan WHERE nhanvien.manv=thannhan.manv And phai='N';

Trang 3

j. Vi mi n H ni lit k cc m s n (MADA), m s phng ban ch tr n (PHONG), h tn trng phng cng nh a ch v ngy sinh ca ngi . SELECT mada, phong, honv & " " & tennv AS hotentrphg, dchi, ngaysinh FROM dean, phongban, nhanvien WHERE dean.phong=phongban.maphg and phongban.trphg=nhanvien.manv and ddiem_da='H Ni'; k. Vi mi nhn vin cho bit h tn nhn vin v h tn ngi qun l trc tip ca nhn vin . SELECT nhanvien.manv, n.honv & " " & n.tennv AS hotenquanly FROM nhanvien, nhanvien AS n WHERE nhanvien.ma_nql=n.manv; l. Vi mi n, lit k tn n v tng s gi lm vic mt tun ca tt c cc nhn vin tham gia n . (Cu ny tm thi cha lm c do bi khng c m t g v thi gian lm vic)

m. Vi mi nhn vin cho bit h tn v s ngi thn ca nhn vin SELECT manv, count(tentn) AS so_ngthan FROM thannhan GROUP BY manv; n. Vi mi phng ban lit k tn phng ban v lng trung bnh ca nhng nhn vin lm vic cho phng ban SELECT tenphong, Avg(luong) AS luongtrungbinh FROM phongban, nhanvien WHERE nhanvien.phg=phongban.maphg GROUP BY tenphong, phg; o. Cho bit danh sch nhng nhn vin (MANV, TENNV) c trn hai thn nhn SELECT thannhan.manv, tennv FROM nhanvien, thannhan WHERE nhanvien.manv=thannhan.manv GROUP BY thannhan.manv, tennv HAVING count(thannhan.manv)>2; p. Cho bit danh sch nhng nhn vin (MANV, TENNV) khng c thn nhn SELECT manv, tennv FROM nhanvien WHERE manv not in (SELECT distinct manv FROM thannhan);
Trang 4