本文主要是介绍mysql 职工表查询20题 -- 035,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
♣题目部分 根据 mysql中的 6张表,回答以下问题?
建表的sql脚本:
链接:https://pan.baidu.com/s/1QngWm6AwIWoN6f0NTm3WyQ 提取码:ib7u ♣答案部分
1、UNION默认去重,不去重用UNION ALL;2、WHERE是在分组前对原始表筛选,HAVING是在分组后对结果表筛选;3、ASC:升序,默认项,DESC:降序;4、分组函数又称为聚合函数、统计函数、组函数,所有分组函数对null值处理为忽略它,而非当做0。5、外键说明保证从表的值必须来自于主表的某一列的值,需在从表中添加外键。主表从表对应的字段类型要一致或兼容主表的字段必须是一个key(一般是主键或唯一)插入数据时,必须先插入主表再插入从表6、主键与唯一的区别主键具有唯一性,不允许为null,一张表最多一个,可以组合使用(即多个字段为组合为一个主键)但不推荐。唯一具有唯一性,允许为null且在mysql5.7中默认可以有多个null,一张表可以有多个,可以组合使用但不推荐7、语句顺序书写顺序:SELECT、DISTINCT、FROM、JOIN ON、WHERE、GROUP BY、HAVING、ORDER BY、LIMIT执行顺序:FROM、 JOIN ON、WHERE、GROUP BY、HAVING、SELECT、DISTINCT、ORDER BY、LIMIT1、查询每个部门的员工数量?SELECT department_id,count(*) FROM employees GROUP by department_id;
2、查询每个部门,每个职务的平均工资?SELECT AVG(salary),department_id,job_id FROM employees GROUP BY department_id,job_id;3、按名字长度分组,并筛选出数量大于5的名字长度?
SELECT COUNT(1) c,LENGTH(last_name) len_name FROM employees GROUP BY len_name HAVING c>5;4、统计表的总行数?
SELECT COUNT(*) FROM employees; --统计表的总行数107SELECT COUNT(1) FROM employees; --统计表的总行数107
5、查询部门编号在[90,150]之间的员工信息?
SELECT * FROM employees WHERE department_id > 90 AND department_id < 150;
6、like、between and、in等的用法
#查询last_name第二个字符是a的员工SELECT * FROM employees WHERE last_name LIKE '_a%';
#查询工资在10000到20000的员工信息,包括10000和20000SELECT * FROM employees WHERE salary BETWEEN 10000 AND 20000;
#查询职位为D_VP和FI_MGR的员工信息SELECT * FROM employees WHERE job_id IN ('D_VP','FI_MGR');
#查询没有奖金的员工信息SELECT * FROM employees WHERE commission_pct IS NULL;LIKE模糊匹配,%任意0个或多个字符; _任意1个字符;无法匹配null;
BETWEEN a AND b,匹配a-b范围内的内容,包括a和b临界;
IN(list),匹配list中的内容;
IS NULL / IS NOT NULL ,判断是否为null,建议使用这个而非<=>。
7、按名字长度降序排序
#按名字长度降序排序SELECT * FROM employees ORDER BY LENGTH(last_name) DESC;8、先按salary降序排序,同样salary的按employee_id升序排序#先按salary降序排序,同样salary的按employee_id升序排序SELECT * FROM employees ORDER BY salary DESC , employee_id ASC;9、查询每个员工的job_title?
SELECT e.last_name,e.job_id,job_title FROM employees e INNER JOIN jobs j ON e.job_id = j.job_id;或SELECT e.last_name,e.job_id,job_title FROM employees e , jobs j where e.job_id = j.job_id;
部分截图
10、查询每个员工的工资等级?
SELECT e.last_name,j.grade_level FROM employees e,job_grades j WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal;或SELECT e.last_name,j.grade_level FROM employees e INNER JOIN job_grades j ON e.salary BETWEEN j.lowest_sal AND j.highest_sal;
部分截图
11、查询每个员工及其对应的领导?SELECT e1.last_name 'employee',e2.last_name 'manager' FROM employees e1,employees e2 WHERE e1.manager_id = e2.employee_id;或SELECT e1.last_name 'employees',e2.last_name 'manager' FROM employees e1 INNER JOIN employees e2 ON e1.manager_id = e2.employee_id;
部分截图
12、查询没有迟到记录的员工名?
SELECT last_name,late.count_late FROM employees LEFT JOIN late ON employees.employee_id = late.employee_id WHERE late.count_late IS NULL;
#右外连接(RIGHT JOIN ON),查询没有迟到记录的员工名SELECT last_name,late.count_late FROM late RIGHT JOIN employees ON employees.employee_id = late.employee_id WHERE late.count_late IS NULL;
#全外连接(FULL JOIN ON),全外连接在mysql中不支持,语法如下:SELECT beauty.name FROM boys FULL OUTER JOIN beauty ON beauty.boyfriend_id = boys.id ;部分截图
13、查询每个部门的信息加部门的人数?
SELECT * ,(SELECT COUNT(1) FROM employees WHERE employees.department_id = departments.department_id) FROM departments;部分截图
14、查询每个部门的平均工资的工资等级?
SELECT department_id,department_name,a , j.grade_level FROM (SELECT e.department_id ,d.department_name, AVG(salary) a FROM employees e LEFT JOIN departments d ON e.department_id=d.department_id GROUP BY e.department_id) tmp INNER JOIN job_grades j ON tmp.a BETWEEN j.lowest_sal AND j.highest_sal;
15、查询工资最少的员工信息?
SELECT * FROM employees WHERE salary = (SELECT MIN(salary) FROM employees);16、查询location_id为1400或1500或2700的部门中的所有员工姓名?
SELECT last_name FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE location_id IN(1400,1500,2700));17、查询编号最小并且工资最高的员工信息?SELECT * FROM employees WHERE (employee_id,salary) = (SELECT MIN(employee_id),MAX(salary) FROM employees);18、查询有员工的部门名?#查询有员工的部门名SELECT department_name FROM departments d WHERE EXISTS(SELECT * FROM employees e WHERE e.department_id = d.department_id);
#用IN代替SELECT department_name FROM departments d WHERE d.department_id IN (SELECT DISTINCT department_id FROM employees);部分截图19、查询有奖金的员工中工资最高的10个员工的信息?#查询有奖金的员工中工资最高的10个员工的信息SELECT * FROM employees WHERE commission_pct IS NOT NULL ORDER BY salary DESC LIMIT 0, 10;20、查询department_id>50和salary>5000的员工信息?#查询department_id>50和salary>5000的员工信息SELECT * FROM employees WHERE department_id > 50 UNION SELECT * FROM employees WHERE salary > 5000 ;
# UNION默认去重,不去重用UNION ALL
About Me:小婷儿
● 本文作者:小婷儿,专注于python、数据分析、数据挖掘、机器学习相关技术,也注重技术的运用
● 作者博客地址:https://blog.csdn.net/u010986753
● 本系列题目来源于作者的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解
● 版权所有,欢迎分享本文,转载请保留出处
● 微信:tinghai87605025 联系我加微信群
● QQ:87605025
● QQ交流群py_data :483766429
● 公众号:python宝 或 DB宝
● 提供OCP、OCM和高可用最实用的技能培训
● 题目解答若有不当之处,还望各位朋友批评指正,共同进步
如果你觉得到文章对您有帮助,点击下方我的头像,赞赏我哦!
有您的支持,小婷儿一定会越来越好!
这篇关于mysql 职工表查询20题 -- 035的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!