- 查詢所員工的email全名,公司email 統一以 "@zpark.cn" 結尾
select email || ‘@zpark.cn’ from employees; - 按照入職日期由新到舊排列員工信息
select employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,commission_pct,manager_id,department_id from employees order by hire_date; - 查詢80號部門工資大於7000的員工的全名與工資.
select first_name || ’ ‘ || last_name as name, salary from employees where department_id = 80 and salary > 7000; - 查詢所有last_name 由四個以上字母組成的員工信息
select * from employees where last_name like’____%’;
單行函數練習
- 把hiredate列看做是員工的生日,查詢本月過生日的員工(考察知識點:單行函數)
select * from employees where to_char(hire_date, ‘mm’) = to_char(sysdate, ‘mm’); - 請用三種以上的方式查詢2002年入職的員工(考察知識點:單行函數)
①select * from employees where to_char(hire_date, ‘yyyy’) = ‘2002’;
②select * from employees where hire_date between to_date(‘2002-1-1’, ‘yyyy-mm-dd’) and to_date(‘2002-12-30’, ‘yyyy-mm-dd’);
③select * from employees where hire_date >= to_date(‘2002-1-1’, ‘yyyy-mm-dd’) and hire_date <= to_date(‘2002-12-30’, ‘yyyy-mm-dd’); - 查詢2002年下半年入職的員工(考察知識點:單行函數)
select * from employees where hire_date between to_date(‘2002-7-1’, ‘yyyy-mm-dd’) and to_date(‘2002-12-30’, ‘yyyy-mm-dd’); - 打印自己出生了多少天
select to_date(to_char(sysdate, 'yyyy-mm-dd'), 'yyyy-mm-dd') - to_date('1996-3-1', 'yyyy-mm-dd') from dual;
組函數練習
- 求1997年各個月入職的的員工個數(考察知識點:組函數)
select count(*), to_char(hire_date, 'mm') from employees where to_char(hire_date, 'yyyy') = '2002' group by to_char(hire_date, 'mm'); - 查詢50號部門,60號部門,70號部門的平均工資
select avg(salary), department_id from employees where department_id in(50, 60, 70) group by department_id; - 查詢平均工資高於8000元的部門的最高工資.
select max(salary), department_id from employees group by department_id having avg(salary) > 8000; - 統計公司里經理的人數
①select count(count(manager_id)), manager_id from employees where manager_id is not null group by manager_id;
②select count(distinct manager_id) from employees;
分頁查詢練習
查詢工資排名第5到第10的員工信息
select * from (select employee_id, first_name, rownum as rn from (select * from employees order by salary desc)) where rn >= 5 and rn <= 10;
子查詢練習
- 查詢工資大於本部門平均工資的員工基本信息
select * from (select * from employees group by department_id having salary > avg(salary)); - 顯示與30號部門first_name為’Guy’員工工資相同的員工姓名和工資
select first_name, salary from employees where salary = (select salary from employees where department_id = 30 and first_name = ‘Guy’); - 查詢各個職位員工工資大於平均工資(平均工資包括所有員工)的人數和員工職位
select count(*), job_id from employees where salary > (select avg(salary) from employees) group by job_id;
表連接查詢練習
- 顯示所有職員的姓名及其所在部門的名稱和工資
select employees.first_name, department_name, salary from employees left outer join departments on employees.department_id = departments.department_id; - 查詢在研發部('IT')工作員工的編號,姓名,工作部門,工作所在地
select employees.employee_id, employees.first_name ,employees.department_id from (employees left outer join departments on employees.department_id = departments.department_id ) left outer join locations on departments.location_id = locations.location_id - 查詢各個部門的名稱和員工人數
select departments.department_name, count(*) from employees left join departments on employees.department_id = departments.department_id group by departments.department_name; - 查詢員工的基本信息,附加其上級的姓名
select e1.employee_id, e1.first_name, e1.salary, e2.first_name from employees e1 left join employees e2 on e1.manager_id = e2.employee_id - 求入職日期相同(年月日相同)的員工(考察知識點:自連接)(有重復)
select e1.first_name, e2.first_name from employees e1 inner join employees e2 on e1.hire_date = e2.hire_date and e1.first_name != e2.first_name
where e1.employee_id < e2.employee_id; --(去重) - 顯示各個部門經理的基本工資
select employees.employee_id, salary from departments left outer join employees on departments.manager_id = employees.employee_id