oracle數據庫的關於分頁查詢的sql語句練習
- 查詢工資最高的3名員工信息
select *
from (select * from employees
order by salary desc
)
where rownum<=3;
- 查詢工資排名第5到第10的員工信息
select * from
(select e.*,rownum r
from employees e
order by salary desc
)
where r between 5 and 10;
- 查詢first_name是以大寫D開頭的第3到第5個員工信息
select * from
(select e.*,rownum r
from employees e
where first_name like ‘D%’
)
where r between 3 and 5 ;
子查詢練習
- 顯示工資比’Allan’(first_name)高的所有員工的姓名和工資
select *
from employees
where salary > (select salary from employees where first_name = ‘Allan’);
- 顯示與’Allan’(first_name)從事相同工作的員工的詳細信息
select *
from employees
where job_id = (select job_id from employees where first_name = ‘Allan’);
- 顯示與30號部門first_name爲’Guy’員工工資相同的員工姓名和工資
select *
from employees
where department_id=30 and
salary=(select salary from employees where first_name=‘Guy’);
- 查詢所有工資高於平均工資(平均工資包括所有員工)的銷售人員(‘SA_REP’)(job_id)
select *
from employees
where job_id=‘SA_REP’ and
salary > (select avg(salary) from employees) ;
表連接查詢練習
//查詢員工信息 employees
//查詢部門信息 departments
//查詢位置信息 locations
select * from employees
select * from departments
select * from locations manger
- 顯示所有職員的姓名及其所在部門的名稱和工資
select e.first_name,salary,d.department_name r
from employees e inner join departments d state province
on e.department_id =d.department_id
- 查詢在研發部(‘IT’)工作員工的編號,姓名,工作部門,工作所在地
select e.first_name,e.manager_id,department_name r,l.street_address a
from employees e inner join departments d
on e.department_id=d.department_id and d.department_name=‘IT’
inner join locations l
on d.location_id = l.location_id;
-
查詢各個部門的名稱和員工人數
select d.department_name,count(*)
from employees e inner join departments d
on e.department_id=d.department_id
group by d.department_name -
查詢員工的基本信息,附加其上級的姓名
select e.*, e2.first_name
from employees e, employees e2 where e.manager_id = e2.employee_id;