You are on page 1of 8

drop database QLNhanVien10 drop table NhanVien drop table DonVi DROP TABLE BacLuong create database QLNhanVien10

use QLNhanVien10 create table DonVi( MaDV char(8)primary key, TenDV char(30), constraint check_MaDV check(MaDV like '[0-9][0-9][0-9][0-9]') )

create table NhanVien( MaNV char(8)primary key , TenNV char(30), CongViec char(50), Luong int, MaPT char(8), Madv char(8), constraint check_MaNV check(MaNV like'[A-Z][A-Z][0-9][0-9][0-9]'), constraint PK_NV_DV foreign key(Madv) references DonVi(MaDV) )

create table BacLuong( MaBac char(8)primary key, BacCao int , BacThap int, )

insert into DonVi(MaDV,TenDV) values('0001','DHTL') insert into DonVi(MaDV,TenDV) values('0002','DHQG') insert into DonVi(MaDV,TenDV) values('0003','DHTN') insert into NhanVien(MaNV,TenNV,CongViec,Luong,Madv) values('NV001','Phamm THi Nhn','Thu Ky','500','0001') insert into NhanVien(MaNV,TenNV,CongViec,Luong,Madv) values('NV002','Hong THanh Vn','Giao vien','600','0001') insert into NhanVien(MaNV,TenNV,CongViec,Luong,Madv) values('NV003','Hoang Thi Lan','Giao Vien','200','0002') insert into NhanVien(MaNV,TenNV,CongViec,Luong,Madv) values('DV004',' Trung Dung','Thu Ky','700','0003')

insert into BacLuong(MaBac,BacCao,BacThap) values('N01','500','400')

insert into BacLuong(MaBac,BacCao,BacThap) values('N02','600','501') insert into BacLuong(MaBac,BacCao,BacThap) values('N03','800','601') select MaNV,TenNV,CongViec,Luong,Madv,MaPT from NhanVien select MaDV,TenDV from DonVi select MaBac,BacCao,BacThap from BacLuong 29 select TenNV,CongViec,Luong,TenDV from NhanVien nv,DonVi dv where nv.MaDV=dv.MaDV and Luong>(select Max(Luong) from Nhanvien where MaDV=(select MaDV from DonVi where TenDV='DHTL')) select *from nhanvien select *from donvi select *from bacluong /*=======a ra 1 s? c?t=======*/ select tennv,luong from nhanvien where luong>=500 /*=======Trnh cc gi tr? l?p=======*/ select distinct congviec,luong from nhanvien /*====a ra lng 3 thang=====*/ select tenNV,luong*3 as LuongNam from nhanvien /*========S?p x?p lng theo th? t? tng d?n======*/ select tennv,luong from nhanvien order by luong asc /*=======a r Lng c?a cng vic======*/ select tennv,luong from nhanvien where (luong>=300) and (congviec='giao vien') /*====Lng trong kho?ng====*/ select tennv,luong from nhanvien where luong between 400 and 1000 /*=====Lng c gi tr? l=====*/ select tennv,luong from nhanvien where luong in (300,400) /*====Nhan Vin c ho tn b?t ?u=====*/ select tennv,congviec from nhanvien where tennv like 'Hoang' /*===Luong TB Max, Min====*/ select AVG(Luong) as LuongTB, max(Luong) as LuongMAX, min(Luong)as LuongMin, count (MaNV) as TongNV from Nhanvien

/*===LuongTB tung loai ====*/ select CongViec,avg(Luong) as LuongTB from NhanVien where luong>'200' group by CongVIec /*===== Tong Luongtung nhom ====*/ select MaDV,CongViec, sum(luong) as TongLuong from nhanvien group by MaDV,CongViec /*===== Cau 20 ====*/ select COngViec, avg(Luong) as LuongTB from nhanvien group by CongViec having (avg(Luong) > 300) /*==== cau 21 ====*/ select MaDV, max(luong) as LuongMax from nhanvien group by MaDV having max(luong) > 300 /*==== Cau 22 =====*/ select MaNV, CongViec, TenDV from nhanvien,Donvi where nhanvien.MaDV = donvi.MaDV /*==== Cau 24 ====*/ select MaNV,TenNV, CongViec, MaBAc from NhanVien, bacluong where (nhanvien.Luong between bacluong.bacthap and bacluong.baccao) and Mabac='N 01' /*=== cau 25 ====*/ select TenNV, CongViec, TenDV, Luong from nhanvien,donvi where (nhanvien.MaDV=DOnvi.MaDV) and (luong) >=500 /*==== cau 28====*/ select tenNV, CongViec,tenDV, Luong from nhanvien nv,donvi dv where (nv.MaDV=Dv.MaDV) and (luong > (select avg(luong) from nhanvien)) /*====cau 29 ====*/ select tenNV, CongViec,tenDV, Luong from nhanvien nv,donvi dv where (nv.MaDV=Dv.MaDV) and (Luong >(select max(Luong) from nhanvien where MaDV = (select MaDV from donvi where TenDV= 'DHTL'))) /*==== Cau 30 ====*/ select TenNV,MaDV,Luong from nhanvien where MaDV,Luong in (select MaDV,min(Luong) from NhanVien group by MaDV) 31 select MaDV,tenNV,Luong,CongViec from Nhanvien where NhanVien.Luong in (select min(NhanVien.luong) as minofLuong from NhanVien group by NhanVien.MaDV ) 32 select MaDV,Luong,TenNV from NhanVien

where NhanVien.luong in(select max(NhanVien.luong) from NhanVien where MaDV ='0002') 33 select MaDV,avg(Luong) from NhanVien group by NhanVien.MaDV having avg(Luong)> (select min(Luong) from NhanVien where MaDV='0002')

select NV.MaNV,NV.TenNV,PT.MaNV,PT.TenNV from NhanVien NV, NhanVien PT where (NV.MaNV=PT.MaPT) and (NV.Luong>PT.Luong) select CongViec from NhanVien where MaDV='0001' union select COngViec from NhanVien where MaDV='0002' select TenNV,TenDV,CongViec,Luong from NhanVien NV, DonVi Dv where ( NV.MADV=DV.MaDV ) and (Luong> (select avg(Luong) from NhanVien)) select TenNV,Luong,CongViec,TenDV from NhaNVien as NV, DonVi as DV where (NV.MaDV=DV.MaDV) and (Luong>(select max(Luong) from NhanVien where MaDV=(select MaDV from DonVi where TenDV='DHTL'))) select *from Donvi select *from nhanvien select TenNV,MaNV, Luong from NhanVien where Luong in (select maX(Luong) from NhanVien group by MaDV) select NhanVien.MaNV,NhanVien.tenNV,NhanVien.luong,MaDV from NhanVien where NhanVien.luong >All(select Luong from NhanVien where MaDV='0002') select MaDV,avg(luong) as AVGofLuong from NhanVien group by NhanVien.MaDV having avg(luong)>(select min(Luong) from NhanVien where MaDV='0003') 30 select TenNV,MaDV,Luong

from NhanVien where Luong in(select Min(Luong) From Nhanvien group by MaDV) 33 select MaDV,avg(Luong) From NhanVien group by MaDV having Avg(Luong)>(select min(Luong) from NhanVien where MaDV='0001') 32 select TenNV,Luong,MaDV from NhanVien where Luong in (select Max(Luong) from Nhanvien where MaDV='0002') 6 select *from NHANVIEN 7 select TenNV,CongViec,Luong from NHANVIEN 8 select distinct CongViec from NHANVIEN 9 Select TenNV,Luong*3 as LuongQuy from NHANVIEN 10 select TenNV,Luong from NHANVIEN order by Luong asc 11 select TenNV,Luong from NHANVien where Luong>300 12 select TenNV,Luong from NHANVien where Luong>300 and CongViec='Giao Vien' 13 select TenNV,Luong from NHANVIEN where Luong in (500,600,700) 14 select TenNV,Luong from NHANVIEN where Luong between 300 and 600 15 select TenNV,CongViec from NHANVIEN where TenNV like 'Hoang%' 16 select avg(Luong) as LuongTB, min(Luong) as LuongMIN, max(Luong) as LuongMAX from NHANVIEN 17 select CongViec, avg(Luong) as LuongTB from NHANVIEN

group by CongViec 18 select CongViec, avg(Luong) from NHANVIEN where Luong>200 group by CongViec 19 select max(Luong) as TongLuong from NHANVIEN group by CongViec 20 select CongViec, avg(Luong) as LuongTB from NHANVIEN group by CongViec having avg(Luong)>300 21 select MaDV,max(Luong) as MaxLuong from NHANVIEN group by MaDV having max(Luong)>300 22 select TenNV,CongViec,TenDV from NHANVIEN NV, DONVI DV where NV.MaDV=DV.MaDV 24 select TenNV,CongViec,MaBac from NHANVIEN NV, BACLuong BL where NV.Luong between BL.BacCao and BL.BacThap 25 select TenNV,CongViec,TenNV,Luong from NHANVIEN NV,DONVI DV where NV.MaDV=DV.MaDV and Luong>500 27 select CongViec from NHANVIEN where MaDV=0001 union select CongViec from NHANVIEN where MaDV=0002 28 select TenNV,TenDV,CongViec,Luong from NHANVIEN NV,DONVI DV where NV.MaDV=DV.MaDV and (Luong > (select avg(Luong) from NHANVIEN)) 29 select TenNV,Luong from NHANVIEN where Luong > (select max(Luong) from NHANVIEN NV,DONVI DV where NV.MaDV=DV.MaDV and TenDV='DHTL') 30 select TenNV,MaDV,Luong from NHANVIEN NV where (luong in(select min(Luong) from NHANVIEN NV group by MaDV)) 32 select TenNV,Luong from NHANVIEN

where Luong in(select Max(Luong) from NHANVIEN where MaDV=0002) 33 select MaDV,AVG(Luong) from NHANVIEN group by MaDv having avG(Luong)>(select min(Luong) from NHANVIEN where MaDV=0002) drop procedure insert_NHANVIEN create procedure insert_NHANVIEN5 @MaNV char(8), @TenNV char(30), @CongViec char(30), @Luong int, @Madv char(8) as begin insert into NHANVIEN(MaNV,TenNV,CongViec,Luong,Madv) values(@MaNV,@TenNV,@CongViec,@Luong,@Madv) end execute insert_NHANVIEN5 'NV007','Hoang Van F','Giam DOc','0','0001' select *from NHANVIEN --insert create trigger check_Luong10 on NHANVIEN for insert as begin declare @Luong int if @@rowcount =1 begin select @Luong = Luong from inserted if @Luong > 1 begin print ' THANH CONG' commit end else begin print 'THAt BAi' rollback end end end select *from NHANVIEN --update create trigger check_Luong on NHANVIEN for update as

begin declare @Luong int if update(Luong) begin select @Luong = Luong from inserted if @Luong > 0 begin print ' THANH CONG' commit end else begin print 'THAt BAi' rollback end end end update NHANVIEN set Luong='100' where MaNV='NV001' select *from NHANVIEN