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)
相关推荐
AAA修煤气灶刘哥4 小时前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
程序新视界4 小时前
学习MySQL绕不开的两个基础概念:聚集索引与非聚集索引
mysql
RestCloud7 小时前
跨境数据传输:ETL如何处理时区与日期格式差异
mysql·api
RestCloud8 小时前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术11 小时前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
xiaok12 小时前
mysql中怎么创建一个可控权限数据库账号密码给到开发者
mysql
可涵不会debug15 小时前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom15 小时前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
玉衡子15 小时前
九、MySQL配置参数优化总结
java·mysql
麦兜*15 小时前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud