SqlServer数据库数据查询30题及答案

1. 查询平均工资最高的部门的部门号、部门名称和平均工资(考虑并列情况)
select top 1 WITH TIES departments.DEPARTMENT_ID,departments.DEPARTMENT_NAME,avg(salary)avgs
from employees,departments WHERE employees.DEPARTMENT_ID=departments.DEPARTMENT_ID group by departments.DEPARTMENT_ID,departments.DEPARTMENT_NAME order by avg(salary) desc
2. 查询平均工资在公司第三多的部门的部门号、部门名称和平均工资
select top 1 * from(select top 3 WITH TIES departments.DEPARTMENT_ID,departments.DEPARTMENT_NAME,avg(salary)avgs
from employees,departments WHERE employees.DEPARTMENT_ID=departments.DEPARTMENT_ID group by departments.DEPARTMENT_ID,departments.DEPARTMENT_NAME order by avg(salary) desc) tb1 order by avgs
3.查询公司总人数、10部门的人数、20部门的人数、50部门的人数、60部门的人数、80部门的人数、90部门的人数、110部门的人数共计8列
select count()人数 from employees union all SELECT COUNT(EMPLOYEE_ID)
FROM employees,departments WHERE employees.DEPARTMENT_ID=departments.DEPARTMENT_ID group by departments.DEPARTMENT_ID
4. 查询公司中工资最高的两个人的员工号、lastname和工资,要求并列的情况也要显示出来
select top 2 employees.EMPLOYEE_ID,employees.LAST_NAME,employees.SALARY
from employees group by employees.EMPLOYEE_ID,employees.LAST_NAME,employees.SALARY
5.用相关子查询查询所有工资大于其所在部门平均工资的人,显示他们的名字和工资
select ee.FIRST_NAME,ee.SALARY from employees ee
where SALARY>(select AVG(SALARY) from employees em where em.DEPARTMENT_ID=ee.DEPARTMENT_ID) order by ee.DEPARTMENT_ID
6.不使用相关子查询查询所有工资大于其所在部门平均工资的人,显示他们的名字、工龄和工资
select ee.FIRST_NAME,ee.SALARY from employees ee where SALARY>(select AVG(SALARY)
from employees em where em.DEPARTMENT_ID=ee.DEPARTMENT_ID) order by ee.DEPARTMENT_ID
7. 查询所有进入公司之后就没有换过岗位的人,显示他们的员工号、姓名和岗位
select j.EMPLOYEE_ID,e.LAST_NAME,jobs.JOB_TITLE from job_history j,employees e,jobs where e.EMPLOYEE_ID=j.EMPLOYEE_ID and e.JOB_ID=jobs.JOB_ID group by LAST_NAME ,jobs.JOB_TITLE,j.EMPLOYEE_ID having count(
)=1
8.查询工资总和大于公司总工资1/8的部门,显示部门名称和部门总工资
select DEPARTMENT_NAME,sum(SALARY) from employees,departments where employees.DEPARTMENT_ID=departments.DEPARTMENT_ID group by departments.DEPARTMENT_NAME having sum(SALARY)>(select sum(SALARY) from employees)/8
9.查询部门所在地为1700的员工的姓名、部门号和职务
select FIRST_NAME+LAST_NAME 姓名,departments.DEPARTMENT_ID 部门号,JOB_TITLE 职务
from employees,jobs,departments where employees.DEPARTMENT_ID=departments.DEPARTMENT_ID and departments.LOCATION_ID=‘1700’ and employees.JOB_ID=jobs.JOB_ID
10. 查询公司中最高工资与最低工资之差
select MAX(SALARY)-MIN(SALARY) from employees
11. 写一个查询显示每种职务的名称、工资总和、20、50、80和90部门的工资总和,分别给列别名为job、total、dept20、dept50、dept80、dept90
select JOB_TITLE job,sum(SALARY)total,(select sum(SALARY) from employees where DEPARTMENT_ID=‘20’)dept20,(select sum(SALARY) from employees where DEPARTMENT_ID=‘50’)dept50,(select sum(SALARY) from employees where DEPARTMENT_ID=‘80’)dept80,(select sum(SALARY) from employees where DEPARTMENT_ID=‘90’)dept90 from jobs,employees where jobs.JOB_ID=employees.JOB_ID
group by JOB_TITLE
12. 查询显示所有工资和任务完成量都与Kochhar一样的人的姓名、入职日期和工资
select LAST_NAME,HIRE_DATE,SALARY from employees where(SALARY=(select SALARY from employees where(LAST_NAME=‘Kochhar’)) and COMMISSION_PCT=(select COMMISSION_PCT from employees where(LAST_NAME=‘Kochhar’)))
13. 根据工资数额查询每个员工的岗位级别
select ,
case
when SALARY between 1000 and 2999 then ‘A’
when SALARY between 3000 and 5999 then ‘B’
when SALARY between 6000 and 9999 then ‘C’
when SALARY between 10000 and 14999 then ‘D’
when SALARY between 15000 and 24999 then ‘E’
when SALARY between 25000 and 40000 then ‘F’
end 岗位级别 from employees
14. 查询每一个部门的人数,包括没有员工的部门(显示0)
select DEPARTMENT_ID, count(EMPLOYEE_ID)人数 from employees group by DEPARTMENT_ID
15. 查询每一个员工的姓名以及其领导的姓名
select LAST_NAME 员工,(
select LAST_NAME from employees e2 where e2.EMPLOYEE_ID=e1.MANAGER_ID
) 领导 from employees e1
16. 查询每种岗位的人数
SELECT e.JOB_ID,COUNT(
) 人数 FROM employees e, jobs WHERE e.JOB_ID=jobs.JOB_ID
GROUP BY e.JOB_ID
17. 查询每个员工在公司工作的周数
select * ,(datediff(day,hire_date,getdate())/7) 工作周数 from employees
18. 员工的年收入为工资+提成,其中提成为年工资额乘以任务完成百分比,求所有员工年收入
select LAST_NAME 姓名 ,SALARY12+(SALARY12* COMMISSION_PCT)年收入 from employees
19.查询工资比公司平局工资还高的员工
select * from employees where employees.SALARY > (select avg(SALARY) from employees)
20. 查询部门位置编号为1700的员工信息
SELECT * FROM employees,departments WHERE employees.DEPARTMENT_ID=departments.DEPARTMENT_ID and departments.LOCATION_ID=‘1700’
21. 查询工资最高的五名员工
select top 5 * from employees order by SALARY desc
22. 查询工资第6到第10高的五名员工
select top 5 * from (select top 10 * from employees order by SALARY desc) t order by SALARY
23. 若公司中工资8000及以上者为高收入,低于8000为低收入,请查询高低收入各有多少人
select sum(case when SALARY>=8000 then 1 else 0 end) as 高收入,sum(case when SALARY<8000 then 1 else 0 end) as 低收入 from employees
24. 查询每个员工在公司工作的月数,并进行四舍五入,精确到小数点后1位
SELECT HIRE_DATE,datediff( month, HIRE_DATE, GETDATE())from employees
25.求每个员工firstname和lastname的长度之差,并在查询时去掉无用的前导和后继空格字符
SELECT FIRST_NAME,LAST_NAME,LEN(LTRIM(RTRIM(FIRST_NAME)))-LEN(LTRIM(RTRIM(LAST_NAME))) FROM employees
26. 写一个查询显示公司总人数、95、96、97和98年入职的人数,分别给列别名为total、1995、1996、1997、1998,共计5列
SELECT count( EMPLOYEE_ID) 人数,year(HIRE_DATE)年份 FROM employees
WHERE YEAR( HIRE_DATE) in (‘1995’,‘1996’,‘1997’,‘1998’) GROUP BY YEAR(HIRE_DATE)
27. 查询所有没当领导的人
select * from employees where EMPLOYEE_ID=MANAGER_ID
28. 查询每个部门的平均工龄(以年为单位)
select AVG(datediff(year,HIRE_DATE,getdate())), DEPARTMENT_ID from employees group by all DEPARTMENT_ID
29. 查询20和50部门的人员信息,结果按照lastname排序
select * from employess where DEPARTMENT_ID in(20,50) order by LAST_NAME
30. 查询所有lastname中带小写字母“a”的员工信息
select * from employees where LAST_NAME LIKE ‘%a%’

如有写的不对的地方,还希望大家评论区指正

  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值