Oracle 簡單子查詢 顧名思義,簡單子查詢是嵌套在 SQL 語句中的另一個SELECT 語句,並且子查詢只返回一列數據 1,單行子查詢: 子查詢 (內查詢) 在主查詢之前一次執行完成。子查詢的結果被主查詢(外查詢)使用 ,單行子查詢,一個子查詢語句只返回一行結果,不能返回空值 可以使用>,<,< ...
Oracle 簡單子查詢
顧名思義,簡單子查詢是嵌套在 SQL 語句中的另一個SELECT 語句,並且子查詢只返回一列數據
1,單行子查詢:
子查詢 (內查詢) 在主查詢之前一次執行完成。子查詢的結果被主查詢(外查詢)使用 ,單行子查詢,一個子查詢語句只返回一行結果,不能返回空值
可以使用>,<,<>(!=),=,<=,>=
select select_list from table
where expr operation --operation為條件語句表達式,
(select select_list
from table
where expr);
例1:查詢工資比Abel高的人
select first_name||' '||last_name name,salary
from employees
where salary >
(select salary from employees --該子查詢只返回salary一個值
where lower(last_name) = 'abel');
NAME SALARY
---------------------- ----------
Steven King 24000
Neena Kochhar 17000
Lex De Haan 17000
Nancy Greenberg 12000
John Russell 14000
Karen Partners 13500
Alberto Errazuriz 12000
Lisa Ozer 11500
Michael Hartstein 13000
Shelley Higgins 12000
例2:查詢最低工資大於50號部門最低工資的部門id和其最低工資
select nvl(department_id,1) department_id,min(salary)
from employees
group by department_id
having min(salary) >
(select min(salary)
from employees
where department_id = 50);
DEPARTMENT_ID MIN(SALARY)
------------- -----------
100 6900
30 2500
1 7000
90 17000
20 6000
70 10000
110 8300
80 6100
40 6500
60 4200
10 4400
例3,單行子查詢中使用單行函數
顯式員工的employee_id,last_name和location。其中,若員工department_id與location_id為1800的department_id相同,則location為’Canada’,其餘則 為’USA’。
select employee_id,last_name,department_id,
(case when department_id = (select department_id
from departments
where location_id = 1800
)
then 'USA'
else 'Canada' end) location
from employees
order by department_id;
或
select employee_id,last_name,department_id,
(case department_id when (select department_id
from departments
where location_id = 1800
)
then 'USA'
else 'Canada' end) location
from employees
order by department_id;
或
select employee_id,last_name,department_id,
decode(department_id,(select department_id
from departments
where location_id = 1800
),'USA',
'Canada') location
from employees
order by department_id;
2,多行子查詢:
子查詢返回多行值,可使用多行比較符:in(not in)(等於/不等於返回值的任意一個),any(和返回值的某一個值比較),all(和返回值的所有值比較)
例1:查詢job_id不為為‘IT_PROG’,並且工資比任意一個job_id為‘IT_PROG’的人都低的員工信息;
或者工資比所有job_id為‘IT_PROG’的人都低的員工信息;
或者工資等於任意一個job_id為‘IT_PROG’的人都低的員工信息;
SQL> select employee_id,first_name||' '||last_name name,job_id,salary
from employees
where salary < any(
select salary
from employees
where job_id = 'IT_PROG')
and job_id <> 'IT_PROG';
EMPLOYEE_ID NAME JOB_ID SALARY
----------- --------------------- ---------- ----------
132 TJ Olson ST_CLERK 2100
136 Hazel Philtanker ST_CLERK 2200
128 Steven Markle ST_CLERK 2200
135 Ki Gee ST_CLERK 2400
127 James Landry ST_CLERK 2400
191 Randall Perkins SH_CLERK 2500
182 Martha Sullivan SH_CLERK 2500
144 Peter Vargas ST_CLERK 2500
...(省略部分)
SQL> select employee_id,first_name||' '||last_name name,job_id,salary
from employees
where salary < all(
select salary
from employees
where job_id = 'IT_PROG')
and job_id <> 'IT_PROG';
EMPLOYEE_ID NAME JOB_ID SALARY
----------- ----------------- -------------------- ----------
185 Alexis Bull SH_CLERK 4100
192 Sarah Bell SH_CLERK 4000
193 Britney Everett SH_CLERK 3900
188 Kelly Chung SH_CLERK 3800
137 Renske Ladwig ST_CLERK 3600
189 Jennifer Dilly SH_CLERK 3600
141 Trenna Rajs ST_CLERK 3500
186 Julia Dellinger SH_CLERK 3400
133 Jason Mallin ST_CLERK 3300
...(省略部分)
SQL> select employee_id,first_name||' '||last_name name,job_id,salary
from employees
where salary in (
select salary
from employees
where job_id = 'IT_PROG')
and job_id <> 'IT_PROG';
EMPLOYEE_ID NAME JOB_ID SALARY
----------- -------------------- -------------------- ----------
158 Allan McEwen SA_REP 9000
152 Peter Hall SA_REP 9000
109 Daniel Faviet FI_ACCOUNT 9000
202 Pat Fay MK_REP 6000
184 Nandita Sarchand SH_CLERK 4200
更多例題:
1. 查詢工資最低的員工信息: last_name, salary
select last_name,salary
from employees
where salary = (
select min(salary)
from employees);
2. 查詢平均工資最低的部門信息
SQL> select d.*,city
from departments d,locations ll
where ll.location_id = d.location_id
and d.department_id = (
select department_id
from employees
group by department_id
having avg(salary) = ( select min(avg(salary))
from employees
group by department_id));
3*. 查詢平均工資最低的部門信息和該部門的平均工資
select d.*,
(select min(avg(salary)) from employees group by department_id) avg_salary,
city
from departments d,locations ll
where ll.location_id = d.location_id
and d.department_id = (
select department_id
from employees
group by department_id
having avg(salary) = ( select min(avg(salary))
from employees
group by department_id));
4. 查詢平均工資最高的 job 信息
select * from jobs
where job_id in (
select job_id from employees
group by job_id
having avg(salary) = (
select max(avg(salary))
from employees
group by job_id));
5. 查詢平均工資高於公司平均工資的部門有哪些?
select department_id from employees
group by department_id
having avg(salary) >
(select avg(salary) from employees);
6. 查詢出公司中所有 manager 的詳細信息.
select * from employees
where employee_id in (
select distinct(manager_id)
from employees);
7. 查詢各個部門中的最高工資,找出其中中最低的最高工資是那個部門,並查詢其最低工資是多少
select department_id,min(salary)
from employees
where department_id in (
select department_id
from employees
group by department_id
having max(salary) in (
select min(max(salary))
from employees
group by department_id))
group by department_id;
8. 查詢平均工資最高的部門的 manager 的詳細信息: last_name, department_id, email, salary
select last_name,department_id,email,salary --根據manager_id查詢相關信息
from employees
where employee_id in (
select distinct(manager_id) --根據平均工資最高的部門ID,查詢這個部門的有哪些manager
from employees
where department_id = (
select department_id --查詢平均工資等於最高的部門的ID
from employees
group by department_id
having avg(salary) = (
select max(avg(salary)) --查詢平均工資最高的
from employees
group by department_id)));
9. 查詢 1999 年來公司的人所有員工的最高工資的那個員工的信息.
select *
from employees
where salary in (
select max(salary)
from employees
where employee_id in (
select employee_id
from employees
where to_char(hire_date,'YYYY') = '1999'))
and to_char(hire_date,'YYYY') = '1999';
本博文系學習尚矽谷網易雲課堂課程整理而成。