MySql练习
-- 1. 查询员工表所有数据,并说明使用*的缺点 select * from employees; -- 使用*查询效率低,可读性不好 -- 2. 查询 email 以 "N " 结尾的员工信息 select * from employees where email like '%N'; -- 3. 查询公司里所有的 manager_id(去除重复数据) select distinct manager_id from employees; -- 4. 按照入职日期由新到旧排列员工信息 select * from employees order by hiredate desc; -- 5. 按照工资由高到低的顺序显示员工信息 select * from employees ORDER BY salary desc; -- 6. 查询职位(JOB_ID)为'ST_CLERK'的员工的工资 select * from employees where job_id='ST_CLERK'; -- 7. 查询 50 号部门的员工姓名以及全年工资. SELECT CONCAT(FIRST_NAME,last_name) as 姓名,salary*12 as 年薪,department_id 部门 from employees where department_id in(50); -- 8. 查询 80 号部门工资大于 7000 的员工的信息. select * from employees where department_id in(80) and salary >7000; -- 9. 查询工资高于 7000 但是没有提成的所有员工. select * from employees where commission_pct is null and salary>7000; -- 10. 查询入职日期在 1992-1-1 到 2012-12-31 之间的所有员工信息 2016-03-03 00:00:00 SELECT * FROM employees; where hiredate BETWEEN STR_TO_DATE('1992-01-01 00:00:00', '%Y-%m-%d %h:%i:%s') AND STR_TO_DATE( '2012-12-31 00:00:00' ,'%Y-%m-%d %h:%i:%s') ; -- 11. 显示first_name中没有'L'字的员工的详细信息 select * from employees where first_name not like ('%L%'); -- 12. 查询电话号码以 6 开头的所有员工信息. select * from employees where phone_number like '6%'; -- 13. 查询 80 号部门中 last_name 以 n 结尾的所有员工信息 select * from employees where department_id in(80) and last_name like '%n'; -- 14. 查询工资大于4000小于8000的员工信息 select * from employees where salary between 4000 and 8000; -- 15. 查询 first_name 中包含"na"的员工信息. select * from employees where first_name like '%na%'; -- 16. 显示公司里所有员工的工资级别 case when -- 级别 工资 -- A <5000 -- B >=5001 and <=8000 -- C >=8001 and <=15000 -- D >15000 select first_name,salary, case when salary>15000 then 'D' when salary>=8001 then 'C' when salary>=5001 then 'B' else 'A' end as 工资级别 from employees; -- 17. 根据入职时间打印出员工级别 -- 员工级别 入职时间 -- 资深员工 2000 前(包含 2000) -- 普通员工 2001~2010(包含 2010) -- 新员工 2010 年后 select first_name 姓名,hiredate 入职日期, case when date_format(hiredate,'%Y')<=2000 then '资深员工' when date_format(hiredate,'%Y')<=2010 then '普通员工' else '新员工' end as 员工等级 from employees; -- 1. 请打印出 1992 年入职的员工 select * from employees where date_format(hiredate,'%Y')=1992; -- 2. 把 hiredate 列看做是员工的生日,求本月过生日的员工(可能没有结果) select * from employees where date_format(hiredate,'%M')=date_format(SYSDATE(),'%M'); -- 3. 查询出员工表中最高工资,最低工资,和平均工资 SELECT max( salary ) AS 最高工资, min( salary ) AS 最低工资, avg( salary ) AS 平均工资 FROM employees;