目录

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)
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
坊钰21 分钟前
【MySQL 数据库】增删查改操作CRUD(下)
java·前端·数据库·学习·mysql·html
Geek__199227 分钟前
VM虚拟机安装及Ubuntu安装配置
数据库·ubuntu·性能优化
向哆哆1 小时前
Java 测试框架:JUnit 5 的新特性与最佳实践
java·数据库·junit
✿ ༺ ོIT技术༻1 小时前
MySQL:事务隔离级别和一致性
数据库·mysql
Gadus_2 小时前
MySQL:InnoDB
数据库·mysql
孙同学_6 小时前
【MySQL】001.MySQL安装
数据库·mysql·adb
不剪发的Tony老师8 小时前
SQLite + Redis = Redka
数据库·redis·sqlite
樽酒ﻬق8 小时前
PostgreSQL、MariaDB和MySQL的异同及应用:企业级数据库选型指南
数据库·postgresql·mariadb
chat2tomorrow9 小时前
SQL2API是什么?SQL2API与BI为何对数据仓库至关重要?
数据库·数据仓库·低代码·bi·数据中台·sql2api
迷雾骑士10 小时前
CentOS Stream release 9安装 MySQL(一)
linux·mysql·centos