自学内容网 自学内容网

MySQL基础练习题:习题31-End

这部分主要是为了帮助大家回忆回忆MySQL的基本语法,数据库来自于MySQL的官方简化版,题目也是网上非常流行的35题。这些基础习题基本可以涵盖面试中需要现场写SQL的问题。

列出薪水等于部门30中员工的薪水的其他员工的姓名和薪水

(root@localhost) [employees]>select ename,sal from emp where sal in (select sal from emp where deptno=30) and deptno<>30;
Empty set (0.05 sec)

(root@localhost) [employees]>

求出员工领导的薪水超过3000的员工姓名和领导姓名

select a.ename as emname,e.ename as leader,e.sal from emp e join emp a on e.empno = a.mgr and e.sal>3000;
+--------+--------+---------+
| emname | leader | sal     |
+--------+--------+---------+
| JONES  | KING   | 5000.00 |
| BLAKE  | KING   | 5000.00 |
| CLARK  | KING   | 5000.00 |
+--------+--------+---------+

求出部门名称中,带有‘s’字符的部门员工的工资合计,部门人数

mysql> select 
        d.deptno, d.dname, ifnull(sum(e.sal),0) as sumsal, ifnull(count(e.ename),0) as totalemp 
        from 
        dept d 
        left join 
        emp e 
        on 
        d.deptno=e.deptno 
        where 
        d.dname like '%s%' 
        group by 
        d.deptno;
+--------+-------------+----------+----------+
| deptno | dname       | sumsal   | totalemp |
+--------+-------------+----------+----------+
|     20 | RESEARCHING | 10875.00 |        5 |
|     30 | SALES       |  9400.00 |        6 |
|     40 | OPERATIONS  |     0.00 |        0 |
+--------+-------------+----------+----------+

给任职时间超过30年的员工加薪10%

update emp set sal=sal*1.1 where (to_days(now())-to_days(hiredate))/365>30;

总结

其实MySQL的基础面试题真的不是很复杂,基本思想就是大化小。有的时候更像是脑筋急转弯。之前还遇到过一个面试题,是学生成绩表,要求查出每门成绩都高于90的同学。直接查就会相当麻烦,所以我们逆向思考,查出哪些有成绩低于90的,然后再查询not in

select distinct name from score where name not in (select distinct name from score where
score<=90)

原文地址:https://blog.csdn.net/weixin_45700531/article/details/137727779

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!