day56-其他查询-面试题-重点
作者:互联网
一、重点 面试题 1. 临时表查询: -- 查询高于本部门平均工资的人员 #1.先查询部门人员的平均工资 SELECT dept_id,AVG(salary)as sal from person GROUP BY dept_id; #2.再用人员的工资与部门的平均工资进行比较 SELECT * FROM person as p1, (SELECT dept_id,AVG(salary)as '平均工资' from person GROUP BY dept_id) as p2 where p1.dept_id = p2.dept_id AND p1.salary >p2.`平均工资`; ps:在当前语句中,我们可以把上一次的查询结果当前做一张表来使用.因为p2表不是真是存在的,所以:我们称之为 临时表 临时表:不局限于自身表,任何的查询结果集都可以认为是一个临时表.
2.判断查询: IF关键字语句可以作为字段名:三元运算 根据工资高低,将人员划分为两个级别,分别为 高端人群和低端人群。 -- 显示效果:姓名,年龄,性别,工资,级别 select p.name,p.age,p.sex,p.salary ,IF(salary>10000,'高端人群','低端人群') as '级别' from person; select name,salary,if(salary>10000,'高端人群','低端人群') from person;
3. #语法一: SELECT CASE WHEN STATE = '1' THEN '成功' WHEN STATE = '2' THEN '失败' ELSE '其他' END FROM 表; #语法二: SELECT CASE age WHEN 23 THEN '23岁' WHEN 27 THEN '27岁' WHEN 30 THEN '30岁' ELSE '其他岁' END FROM person; select name, (case when salary > 6000 then '小康' when salary > 3000 and salary <= 6000 then '屌丝' else '穷' end ) as '级别' from person;
-- 根据工资高低,统计每个部门人员收入情况,划分为 富人,小资,平民,吊丝 四个级别, -- 要求统计四个级别分别有多少人 select dname , sum(case when person.salary > 10000 THEN 1 else 0 END) as '富人', sum(case when person.salary BETWEEN 5001 and 10000 THEN 1 else 0 END) as '小资', sum(case when person.salary BETWEEN 3001 and 5000 THEN 1 else 0 END) as '平民', sum(case when person.salary <= 3000 THEN 1 else 0 END) as '屌丝' from dept,person where dept.did = person.dept_id GROUP BY dept.did
标签:salary,面试题,dept,when,查询,person,SELECT,id,day56 来源: https://www.cnblogs.com/python-daxiong/p/12257046.html