Oracle練習題(1)
1. 查詢工資大於12000的員工姓名和工資
select last_name, salary
from employees
where salary > 12000
2. 查詢員工號爲176的員工的姓名和部門號
select last_name, department_id
from employees
where employee_id = 176
3. 選擇工資不在5000到12000的員工的姓名和工資
select last_name, salary
from employees
where salary not between 5000 and 12000
4. 選擇僱用時間在1998-02-01到1998-05-01之間的員工姓名,job_id和僱用時間
select last_name, job_id, hire_date
from employees
where hire_date between '1-2月 -1998' and '1-5月 -1998'
5. 選擇在20或50號部門工作的員工姓名和部門號
select last_name, department_id
from employees
where department_id in (20, 50)
6. 選擇在1994年僱用的員工的姓名和僱用時間
select last_name, hire_date
from employees
where hire_date like '% -94'
7. 選擇公司中沒有管理者的員工姓名及job_id
select last_name, job_id
from employees
where manager_id is null
8. 選擇公司中有獎金的員工姓名,工資和獎金級別
select last_name, salary, commission_pct
from employees
where commission_pct is not null
9. 選擇員工姓名的第三個字母是a的員工姓名
select last_name
from employees
where last_name like '__a%'
10. 選擇姓名中有字母a和e的員工姓名
select last_name
from employees
where last_name like '%a%' and last_name like '%e%'
11. 顯示系統時間
select to_char(sysdate, 'yyyy-mm-dd hh:mi:ss') from dual;
12. 查詢員工號,姓名,工資,以及工資提高百分之20%後的結果(new salary)
select employee_id, last_name, salary, salary * 1.2 "new salary"
from employees;
13. 將員工的姓名按首字母排序,並寫出姓名的長度(length)
select last_name, length(last_name)
from employees
order by last_name
14. 查詢各員工的姓名,並顯示出各員工在公司工作的月份數(worked_month)。
select last_name, months_between(sysdate, hire_date) worked_month
from employees
15. 查詢員工的姓名,以及在公司工作的月份數(worked_month),並按月份數降序排列
select last_name, months_between(sysdate, hire_date) worked_month
from employees
order by worked_month desc
16. 做一個查詢,產生下面的結果
<last_name> earns monthly but wants <salary*3>
Dream Salary |
---|
King earns $24000 monthly but wants $72000 |
select last_name || ' earns ' || salary || ' monthly but wants ' || salary * 3
from employees
17. 使用decode函數,按照下面的條件:
job grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
Others F
產生下面的結果
Last_name | Job_id | Grade |
---|---|---|
king | AD_PRES | A |
select last_name, job_id, decode(job_id, 'AD_PRES', 'A',
'ST_MAN', 'B',
'IT_PROG', 'C',
'SA_REP', 'D',
'ST_CLERK', 'E',
'F') GRADE
from employees
18. 將第7題的查詢用case函數再寫一遍。
select last_name, job_id, case job_id when 'AD_PRES' then 'A'
when 'ST_MAN' then 'B'
when 'IT_PROG' then 'C'
when 'SA_REP' then 'D'
when 'ST_CLERK' then 'E'
else 'F'
end
from employees
19. 查詢公司員工工資的最大值,最小值,平均值,總和
Select max(salary), min(salary), avg(salary), sum(salary)
From employees
20. 查詢各job_id的員工工資的最大值,最小值,平均值,總和
Select job_id, max(salary), min(salary), avg(salary), sum(salary)
From employees
Group by job_id
21. 選擇具有各個job_id的員工人數
Select job_id, count(employee_id)
From employees
Group by job_id;
22. 查詢員工最高工資和最低工資的差距(DIFFERENCE)
Select max(salary) – min(salary) difference
From employees
23. 查詢各個管理者手下員工的最低工資,其中最低工資不能低於6000,沒有管理者的員工不計算在內
Select manager_id, min(salary)
From employees
Where manager_id is not null
Group by manager_id
Having min(salary) >= 6000
24. 查詢所有部門的名字,location_id,員工數量和工資平均值
Select department_name, location_id, count(employee_id), avg(salary)
From employees e join departments d
On e.department_id = d.department_id
Group by department_name, location_id