SQL入门测试

学了数据库这么久,你的sql是否已经入门,那么试试下面的一道试题吧!

一道关于员工与部门查询的SQL笔试题(文章最后附上建表sql语句及数据,建的表不同,答案可能略有不同)

注:所有语句均在MS SQL SERVER2008中测试通过

建表

DEPARTMENTS:
DEPARTMENT_ID(primary key),
DEPARTMENT_NAME,
LOCATION

EMPLOYEES:
EMPLOYEE_ID(primary key),
EMPLOYEE_NAME,
EMPLOYEE_JOB,
MANAGER,
SALARY,
DEPARTMENT_ID

--1.列出EMPLOYEES表中各部门的部门号,最高工资,最低工资

select department_id,min(salary) as '最高工资',max(salary) as '最低工资'
from employees
group by department_id

 

--2.列出EMPLOYEES表中各部门EMPLOYEE_JOB为'CLERK'的员工的最低工资,最高工资

select department_id,min(salary) as '最高工资',max(salary) as '最低工资'
from employees
where employee_job = 'clerk'
group by department_id

 

--3.对于EMPLOYEES中最低工资小于5000的部门,列出EMPLOYEE_JOB为'CLERK'的员工的部门号,最低工资,最高工资

select department_id as '部门号'MIN(salary) as '最低工资',MAX(salary) as '最高工资'
from employees as a
where employee_job='clerk' and 4000>
(
    select MIN(salary)
    from employees as b
    where a.department_id=b.department_id
     
)
group by department_id

--4.根据部门号由高而低,工资有低而高列出每个员工的姓名,部门号,工资

select employee_name ,department_id ,salary
from employees
order by employee_id desc,salary asc

 

--5.写出对上题的另一解决方法

这里,我也没想到另一种解决方法,如果有谁想到,可以在下面留言告知,感谢

 

--6.列出'张三'所在部门中每个员工的姓名与部门号

select employee_name,department_id
from employees
where department_id =
(
    select department_id
    from employees
    where employee_name='张三'
)

 

--7.列出每个员工的姓名,工作,部门号,部门名

select a.employee_name,a.employee_job,a.department_id,b.department_name
from employees as a,departments as b
where a.department_id=b.department_id

 

--8.列出EMPLOYEES中工作为'CLERK'的员工的姓名,工作,部门号,部门名

select a.employee_name,a.employee_job,a.department_id,b.department_name
from employees as a,departments as b
where a.department_id=b.department_id and a.employee_job='clerk'

 

--9.对于EMPLOYEES中有管理者的员工,列出姓名,管理者姓名(管理者外键为MANAGER)

select a.employee_name as 姓名,b.employee_name as 管理者
from EMPLOYEES as a,EMPLOYEES as b
where a.MANAGER is not null and a.MANAGER = b.EMPLOYEE_name

 

--10.对于DEPARTMENTS表中,列出所有部门名,部门号,同时列出各部门工作为'CLERK'的员工名与工作

select a.department_id,a.department_name,b.employee_name,b.employee_job
from departments as a,employees as b
where a.department_id = b.department_id and b.employee_job = 'clerk'

 

--11.对于工资高于本部门平均水平的员工,列出部门号,姓名,工资,按部门号排序

第一种方法:

select a.department_id,a.employee_name,a.salary
from employees as a,
(
    select department_id,AVG(salary) as '平均工资'
    from employees
    group by department_id
)as b
where a.department_id = b.department_id and a.salary>b.平均工资
order by a.department_id asc

第二种方法:

select a.department_id,a.employee_name,a.salary
from employees as a
where a.salary>
(
    select AVG(salary)
    from employees as b
    where a.department_id = b.department_id
)
order by a.department_id asc

 

--12.对于EMPLOYEES,列出各个部门中平均工资高于本部门平均水平的员工数和部门号,按部门号排序

select COUNT(a.salary) as '人数',a.department_id
from employees as a
where a.salary>
(
    select AVG(salary)
    from employees as b
    where a.department_id = b.department_id
)
group by a.department_id
order by a.department_id desc

 

--13.对于EMPLOYEES中工资高于本部门平均水平,人数多与1人的,列出部门号,人数,按部门号排序

第一种方法:

select c.人数,c.department_id
from
(
    select COUNT(a.salary) as '人数',a.department_id
    from employees as a
    where a.salary>
    (
        select AVG(salary)
        from employees as b
        where a.department_id = b.department_id
    )
    group by a.department_id
as c
where c.人数>1
order by c.department_id desc

第二种方法:

select count(a.employee_id) as 员工数,a.department_id as 部门号,avg(salary) as 平均工资
from employees as a
where (
        select count(c.employee_id)
        from employees as c
        where c.department_id = a.department_id and c.salary>
        (
            select avg(salary)
            from employees as b
            where c.department_id = b.department_id
        )
    )>1
group by a.department_id order by a.department_id

 

--14.对于EMPLOYEES中低于自己工资至少5人的员工,列出其部门号,姓名,工资,以及工资少于自己的人数

select a.department_id,a.employee_name,a.salary,
(
    select count(b.employee_name)
    from 

你可能感兴趣的:(sql,sql,工作,数据库,server,manager,测试)