sql 面试题

--select * from emp;
--select * from dept;
--统计部门表里的每个部门的员工人数,并打印出部门编号、名称
--思路:因为有些部门刚建立可能没有员工,要以部门表为主,使用右连接
select d.deptno as "部门编号",
       d.dname as "部门名称",
       count(e.deptno) as "部门人数"
  from emp e
 right join dept d
    on e.deptno = d.deptno
 group by d.deptno, d.dname;
 
 --统计至少有一个员工的部门
--思路1:如果在员工表里有对应的部门编号就说明该部门至少有一个员工
select d.deptno,d.dname from dept d where deptno in(select distinct deptno from emp);
--思路2:先从员工表里查询出部门人数大于等于1的部门编号,然后再从部门表里查
select d.deptno, d.dname
  from dept d
 where deptno in
       (select deptno from emp group by deptno having count(deptno) >= 1);
--思路三:原理和思路一差不多,只是写法不一样
select dname from dept d where exists(select null from emp  e where e.deptno=d.deptno);   


--找出没有选修过“李明”老师授课的学生姓名
--第一种方法:
--第一步:在课程表中找出“李明”老师讲授的所有课程
select cno from  Course where teacher="李明";
--第二步:在选修表中找出对应的学生号条件是课程号不在第一步中的
select sno from SC where cno not in
     (select cno from  Course where teacher='李明')
--第三步:根据学生号找出对应的学生姓名
select sname from S where sno in
   (select sno from SC where cno not in
     (select cno from  Course where teacher='李明'))
--第二种方法:
/*
exists (sql 返回结果集为真)  
not exists (sql 不返回结果集为真)
not exists 里是一个子查询,当数据库在查询时,查询到teacher='李明'时
子查询就有数据,有数据就返回false ,整个查询语句就变成了
select sname from s where false;--不执行
相反当查询到teacher<>'李明'时,返回true ,就把对应学生名字查出来了
*/
select sname from s
 where not exists 
 (select * from sc, c where sc.cno = c.cno  and c.cteacher = '李明' and sc.sno = s.sno)
          
         
/*
表A有三个字段
years   a     b 
2001    1     1.1
2001    2     1.2
2001    3     1.3
2001    4     1.4
2002    1     1.1
2002    2     1.2
2002    3     1.3
2002    4     1.4
用一条SQL语句写,得出如下结果:
years    a1       a2     a3     a4 
2001     1.1  	1.2    1.3    1.4    
2002     1.1  	1.2   1.3    1.4
*/          
select years,
      sum(case a when 1 then b end)as a1,
      sum(case a when 2 then b end)as a2,
      sum(case a when 3 then b end)as a3,
      sum(case a when 4 then b end)as a4
from A 
group by years;


### SQL 面试题及答案 以下是几道经典的 SQL 面试题,涵盖从基础到高级的难度,适合不同层次的求职者准备面试时参考。 #### 1. 查询员工中工资最高的前三名员工 假设有一个名为 `employees` 的,包含以下字段:`id`, `name`, `salary`。需要查询出工资最高的前三名员工。 ```sql SELECT id, name, salary FROM employees ORDER BY salary DESC LIMIT 3; ``` 此查询使用了 `ORDER BY` 和 `LIMIT` 来限制结果集的大小[^1]。 #### 2. 查询部门平均工资高于公司整体平均工资的部门 假设有一个名为 `departments` 的,包含字段 `department_id`, `department_name`,以及一个名为 `employees` 的,包含字段 `employee_id`, `department_id`, `salary`。 ```sql SELECT d.department_name, AVG(e.salary) AS avg_salary FROM departments d JOIN employees e ON d.department_id = e.department_id GROUP BY d.department_name HAVING AVG(e.salary) > (SELECT AVG(salary) FROM employees); ``` 这通过子查询计算公司的整体平均工资,并与每个部门的平均工资进行比较[^2]。 #### 3. 查询每个部门中工资最高的员工 假设同样的结构,要求查询每个部门中工资最高的员工信息。 ```sql SELECT e1.department_id, e1.name, e1.salary FROM employees e1 WHERE e1.salary = ( SELECT MAX(e2.salary) FROM employees e2 WHERE e1.department_id = e2.department_id ); ``` 这段代码利用了子查询和 `MAX()` 函数来获取每个部门的最高工资,并匹配相应的员工记录。 #### 4. 使用窗口函数查询每个部门工资排名前两名的员工 在现代 SQL 中,可以使用窗口函数实现更复杂的排序需求。 ```sql SELECT department_id, name, salary FROM ( SELECT department_id, name, salary, RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) as rank_num FROM employees ) temp WHERE rank_num <= 2; ``` 这使用了 `RANK()` 窗口函数对每个部门的员工按工资降序排列,并选取排名前两名的员工[^2]。 #### 5. 处理空值问题:查询所有员工及其对应的部门名称 假设有些员工没有分配到部门,即 `department_id` 为 `NULL`,需要显示这些员工的信息。 ```sql SELECT e.name AS employee_name, d.department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.department_id; ``` 通过使用 `LEFT JOIN`,即使某些员工没有对应的部门信息,也可以正确显示他们的记录。 ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值