sql练习
--建表
create table Regions ( Region_id int not null primary key , Region_name varchar(25) ) go --国籍 create table Countries( Country_id int not null primary key, Country_name varchar(40), Region_id int ) go --地区 create table Location( Location_id int not null primary key, Street_address varchar(40), Postal_code varchar(12), City varchar(30) not null, state_province varchar(25), country_id char(2) ) go --部门 create table Departments( Department_id int primary key not null, Department_name varchar(30) not null, Manager_id int, Location_id int ) go --工资 create table Jobs( Job_id int primary key not null, Job_title varchar(35) not null, Min_salary int , Max_salary int ) --员工 create table Employees( Employee_id int primary key not null, First_name varchar(20), Last_name varchar(25) not null, Email varchar(25) not null, Phone_number varchar(20), Hire_date date not null, Job_id varchar not null, Salary numeric(5,2), Commission_pct numeric(2,2), Manager_id int, Department_id int ) --1. 各个部门平均、最大、最小工资、人数,按照部门号升序排列。 select Departments.Department_name as ‘部门‘, avg(salary)as ‘平均工资‘ , max(salary)as ‘最大工资‘, min(salary)as ‘最小工资‘, count(Employee_id)as ‘人数‘ from Employees inner join Departments on Departments.Department_id = Employees.Department_id group by Departments.Department_name ,Departments.Department_id order by Departments.Department_id asc --2. 各个部门中工资大于5000的员工人数。 select Departments.Department_name as ‘部门‘, count(*) as ‘人数‘ from Employees inner join Departments on Departments.Department_id = Employees.Department_id where Employees.Salary>5000 group by Departments.Department_name --3. 各个部门平均工资和人数,按照部门名字升序排列。 select Departments.Department_name as ‘部门‘, avg(Salary)as ‘平均工资‘ , count(*) as ‘人数‘ from Employees inner join Departments on Departments.Department_id = Employees.Department_id group by Departments.Department_name ,Departments.Department_id order by Departments.Department_name asc -- (问题4) 4. 列出每个部门中有同样工资的员工的统计信息,列出他们的部门号,工资,人数。 (错) select Employees1.Department_id as ‘部门号‘, Employees1.Salary as ‘工资‘, COUNT(1) as ‘人数‘ from Employees Employees1,Employees Employeess2 where Employees1.Department_id =Employeess2.Department_id and Employees1.Salary=Employeess2.Salary group by Employees1.Department_id,Employees1.Salary (正) SELECT COUNT(1) AS ‘COUNT‘, DEPARTMENT_ID, SALARY FROM EMPLOYEES GROUP BY DEPARTMENT_ID,SALARY HAVING COUNT(1)>1 --(不知道)5. 列出同部门中工资高于1000 的员工数量超过2 人的部门,显示部门名字、地区名称。 select employee.Department_id as ‘部门号‘, department.Department_name as ‘部门‘, location1.City as‘地区‘ from Employees employee,Departments department,Location location1 where department.Department_id = employee.Department_id and employee.Salary>1000 group by department.Department_name, employee.Department_id --location1.City having count(1)>=2 order by employee.Department_id --6. 哪些员工的工资,高于整个公司的平均工资,列出员工的名字和工资(降序)。 select e.First_name as ‘名字1‘, e.Last_name as ‘名字2‘, e.Salary as ‘工资‘ from Employees e where e.Salary> (select avg(Employees.Salary)from Employees) order by e.Salary desc --7. 哪些员工的工资,介于1号 和3号部门平均工资之间。 select e.Department_id as‘ID‘, e.First_name as ‘名字1‘, e.Last_name as ‘名字2‘, e.Salary as ‘工资‘ from Employees e where e.Salary between (select avg(Employees.Salary)from Employees where Employees.Department_id =1) and (select avg(Employees.Salary)from Employees where Employees.Department_id =3) -- 8. 所在部门平均工资高于5000 的员工名字。 select Employees1.Department_id as‘部门号‘, Employees1.First_name as‘名字1‘, Employees1.Last_name as‘名字2‘, Employees1.Salary as ‘工资‘ from Employees Employees1 where (select avg(Employees.Salary)from Employees where Employees1.Department_id =Employees.Department_id)>5000 --9. 列出各个部门中工资最高的员工的信息:名字、部门号、工资。 select e1.First_name as‘名字1‘, e1.Last_name as‘名字2‘, e1.Department_id as‘部门号‘, e1.Salary as‘工资‘ from Employees e1 where e1.Salary = ( select max(Employees.Salary) from Employees where e1.Department_id =Employees.Department_id) order by e1.Department_id --(有问题)10. 最高的部门平均工资是多少。 (错) select e1.Department_id as‘部门号‘, max(e1.Salary) as‘工资‘ from Employees e1 where e1.Salary =(select avg(Employees.Salary) from Employees where e1.Department_id = Employees.Department_id) group by e1.Department_id --(有问题)10. 平均工资是最高的部门的工资是多少。 (正) select top 1 Salary from ( select a.Department_id as ‘Department_id‘, avg(a.Salary) as ‘Salary‘ from Employees a group by a.Department_id ) as b order by Salary desc --求平均值 select avg(Employees.Salary) from Employees e1,Employees where e1.Department_id= Employees.Department_id group by e1.Department_id
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。