create database sss
--Employee(EmployeeID,Name,Birthday,Sex,Address,Zip,PhoneNumber,EmailAddress,DepartmentID)
--Departments(DepartmentID,DepartmentName,Note)
--Salary(EmployeeID,Income,OutCome)
create table Employee(
EmployeeID char(6) NOT NULL,
Name char(10) not null,
Birthday datetime not null,
Sex bit not null,
Address char(20),
Zip char(6),
PhoneNumber char(12),
EmailAddress char(30),
DepartmentID char(3) not null,
primary key(EmployeeID)
);
create table Department(
DepartmentID char(3) NOT NULL ,
DepartmentName char(20),
Note text,
primary key(DepartmentID)
);
create table Salary(
EmployeeID char(6)not null unique,
Income float(8)not null,
OutCome float(8) not null
);
INSERT INTO Department VALUES('1','财务部','财务部');
INSERT INTO Department VALUES('2','研发部','研发部');
INSERT INTO Department VALUES('3','人力资源部','人力资源部');
INSERT INTO Employee VALUES('1001' ,'李勇', '78-3-12', '0', '河南', '475001', '3880378','[email protected]','1');
INSERT INTO Employee VALUES('1002' ,'王敏', '80-11-2', '1', '河南', '475002', '0378311','[email protected]','1');
INSERT INTO Employee VALUES('1003' ,'刘晨', '78-6-22', '0', '河南', '475003', '0378322','[email protected]','1');
INSERT INTO Employee VALUES('1004' ,'周宏', '83-10-3', '1', '河北', '475004', '7865987','[email protected]','1');
INSERT INTO Employee VALUES('2001' ,'张立', '78-8-1', '0', '河南', '475005', '0378333','[email protected]','2');
INSERT INTO Employee VALUES('2002' ,'刘毅', '82-1-23', '0', '河南', '475006', '0378344','[email protected]','2');
INSERT INTO Employee VALUES('2003' ,'张玫', '81-3-15', '1', '河南', '475007', '0378355','[email protected]','2');
INSERT INTO Employee VALUES('2004' ,'王军', '79-5-12', '0', '山东', '475008', '5687967','[email protected]','2');
INSERT INTO Employee VALUES('3001' ,'徐静', '76-8-12', '1', '河南', '475009', '0378366','[email protected]','3');
INSERT INTO Employee VALUES('3002' ,'赵军', '79-2-19', '0', '河南', '475010', '0378377','[email protected]','3');
INSERT INTO Employee VALUES('3003' ,'王霞', '82-8-18', '1', '湖南', '475011', '7556677','[email protected]','3');
INSERT INTO Salary VALUES('1001','3600','1500');
INSERT INTO Salary VALUES('1002','3300','1000');
INSERT INTO Salary VALUES('1003','3700','1200');
INSERT INTO Salary VALUES('1004','4500','1600');
INSERT INTO Salary VALUES('2001','4000','1600');
INSERT INTO Salary VALUES('2002','3800','1800');
INSERT INTO Salary VALUES('2003','3800','1500');
INSERT INTO Salary VALUES('2004','5100','1800');
INSERT INTO Salary VALUES('3001','4200','2000');
INSERT INTO Salary VALUES('3002','4100','1800');
INSERT INTO Salary VALUES('3003','4600','1400');
1 点击“新建查询”,输入查询语句。
(1)查询每个雇员的所有信息。
(2)查询每个雇员的地址和电话。
(3)查询EmployeeID为1001的雇员的地址和电话。
(4)查询女雇员地址和电话,并用AS子句将结果中各列的标题分别制定为“地址”和“电话”。
(5)计算每个雇员的实际收入。
(6)找出所有姓王的雇员的部门号。
(7)找出所有含有“中山”的雇员的号码和部门号。
2 点击“新建查询”,输入查询语句,练习多表连接查询和嵌套查询。
(1)查询每个雇员的情况及工资情况。
(2)查询财务部工资在2200元以上的雇员姓名及工资情况。
(3)查询研发部在1966年以前出生的雇员姓名及其工资详情。
(4)查询人力资源部雇员的最高和最低工资。
(5)将各雇员的情况按工资由低到高排列。
(6)求各部门的雇员数。
(7)找出所有在财务部和人力资源部工作的雇员的编号
(8)统计人力资源部工资在2500以上雇员的人数。
(9)求财务部雇员的总人数。
(10)求财务部雇员的平均工资。
(11)查找比所有财务部的雇员工资都高的雇员的姓名。
(12)查找财务部年龄不低于研发部所有雇员年龄的雇员的姓名。
(13)查找在财务部工作的雇员的情况。
select *
from Employee
select Address,PhoneNumber
from Employee
select Address,PhoneNumber
from Employee
where EmployeeID = 000001
select Address AS '地址',PhoneNumber AS '电话'
from Employee
where Sex = 1
select Income-OutCome AS '实际收入'
from Salary
select DepartmentID
from Employee
where Name like '王%'
select EmployeeID,DepartmentID
from Employee
where Address like '%中山%'
select *,Income-OutCome AS '工资'
from Employee,Salary
where Employee.EmployeeID = Salary.EmployeeID
select Name,Income-OutCome as '工资'
from Employee,Salary
where Employee.EmployeeID=Salary.EmployeeID and Employee.EmployeeID in
(
select EmployeeID
from Salary
where Income-OutCome>=2200 and EmployeeID in
(
select EmployeeID
from Employee
where DepartmentID in
(
select DepartmentID
from Department
where DepartmentName='财务部'
)
)
)
select Name,Income-OutCome as '工资'
from Employee,Salary
where Employee.EmployeeID=Salary.EmployeeID and Employee.EmployeeID in
(
select EmployeeID
from Salary
where EmployeeID in
(
select EmployeeID
from Employee
where Birthday<1966 and DepartmentID in
(
select DepartmentID
from Department
where DepartmentName='研发部'
)
)
)
select min(Income-OutCome) as '最低工资',max(Income-OutCome) as '最高工资'
from Salary
where EmployeeID in
(
select EmployeeID
from Employee
where DepartmentID in
(
select DepartmentID
from Department
where DepartmentName='人力资源部'
)
)
select Employee.*
from Employee,Salary
where Employee.EmployeeID=Salary.EmployeeID
order by Income asc
select Department.DepartmentID, count(*)
from Employee,Department
where Employee.DepartmentID=Department.DepartmentID
group by Department.DepartmentID
select Employee.EmployeeID
from Employee,Department
where Employee.DepartmentID = Department.DepartmentID
and (Department.DepartmentName = '财务部'
or Department .DepartmentName= '人力资源部')
select count(EmployeeID)as '人数'
from Salary
where Income-OutCome>=2500 and EmployeeID in
(
select EmployeeID
from Employee
where DepartmentID in
(
select DepartmentID
from Department
where DepartmentName='人力资源部'
)
)
select count(Employee.EmployeeID) as '总人数'
from Employee
where DepartmentID in
(
select DepartmentID
from Department
where DepartmentName='人力资源部'
)
select AVG(Income-OutCome) as '平均工资'
from Salary
where EmployeeID in
(
select Employee.EmployeeID
from Employee
where DepartmentID in
(
select DepartmentID
from Department
where DepartmentName='财务部'
)
)
select Name
from Employee,Salary
where Employee.EmployeeID=Salary.EmployeeID and (Income - OutCome)>
(
select max(Income-OutCome)
from Salary
where EmployeeID in
(
select Employee.EmployeeID
from Employee
where DepartmentID in
(
select DepartmentID
from Department
where DepartmentName='财务部'
)
)
)
select Name
from Department ,Employee
where Department.DepartmentID=Employee.DepartmentID and DepartmentName='财务部'
and (2019-Employee.Birthday) >=
(
select max(2019-Employee.Birthday)
from Employee
where DepartmentID in
(
select DepartmentID
from Department
where DepartmentName='研发部'
)
)
select *
from Employee
where DepartmentID in
(
select DepartmentID
from Department
where DepartmentName='财务部'
)