溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SQL 基礎(chǔ)之子查詢(十一)

發(fā)布時間:2020-08-11 08:55:52 來源:網(wǎng)絡(luò) 閱讀:476 作者:yuri_cto 欄目:數(shù)據(jù)庫

子查詢:類型、語法、和注意事項


使用子查詢能解決哪些問題?

SQL 基礎(chǔ)之子查詢(十一)


子查詢語法:

select select_list from table where expr operator (select select_list from table);

  • 子查詢(內(nèi)查詢)在主查詢(外查詢)之前執(zhí)行。

  • 主查詢使用子查詢結(jié)果。

  • 位置:select,where,from,having


1、查詢誰的工資比Abel高

select last_name, salary from employees 

where salary >

(select salary

from employees

where last_name = 'Abel');

SQL 基礎(chǔ)之子查詢(十一)


使用子查詢注意事項

  • 子查詢要包含在括號內(nèi)。

  • 將子查詢放在比較條件的右側(cè)增強(qiáng)可讀性(子查詢可以出現(xiàn)在比較運(yùn)算符的兩側(cè))

  • 單行操作符對應(yīng)單行子查詢,多行操作符對應(yīng)多行子查詢


單行子查詢:

– 子查詢中的組函數(shù)

– HAVING 子句中的子查詢


  • 只返回一行

  • 使用單行比較操作符


操作符含義
=等于
>大于
>=大于等于
<小于
<=小于等于
<>不等于


select last_name, job_id, salary from employees

where job_id  in  (select job_id from employees

where last_name like  'Taylor')

and salary in

(select salary

from employees

where last_name like 'Taylor');

SQL 基礎(chǔ)之子查詢(十一)


在子查詢中使用組函數(shù)

select last_name, job_id, salary from employees where 

salary = (select min(salary) from employees);

SQL 基礎(chǔ)之子查詢(十一)



子查詢中的HAVING 子句

  • 首先執(zhí)行子查詢

  • 向主查詢中的 HAVING 子句返回結(jié)果

select department_id, min(salary)

from employees

group by department_id

having min(salary) >

(select min(salary)

from employees

where department_id = 50);

SQL 基礎(chǔ)之子查詢(十一)


多行子查詢使用單行比較符,以下為錯誤寫法

select employee_id, last_name

from employees

where salary =

(select min(salary)

from employees

group by department_id);


子查詢中的空值問題

SQL 基礎(chǔ)之子查詢(十一)

select last_name, job_id from employees

where job_id =

(select job_id from employees

where last_name = 'haas');



多行子查詢

– 使用 ALL 或 ANY


  • 返回多行。

  • 使用多行比較操作符。

操作符含義
IN等于列表中的任何一個值
ANY必須在=, !=, >, <, <=, >= 操作符之前使用,與列表中每個值進(jìn)行比較,如果沒有返回任何行,說明計算結(jié)果為FALSE
ALL必須在=, !=, >, <, <=, >=操作符之前使用,與列表中每個值進(jìn)行比較,如果沒有任何行返回,說明計算結(jié)果為TRUE


使用范例:

在多行子查詢中使用 ANY 

select employee_id, last_name, job_id, salary

from employees  where salary < any

(select salary

from employees

where job_id = 'IT_PROG')

and job_id < > 'IT_PROG';

SQL 基礎(chǔ)之子查詢(十一)


在多行子查詢中使用 ALL  操作符

select employee_id, last_name, job_id, salary

from employees

where salary < all

(select salary

from employees

where job_id = 'IT_PROG')

and job_id <> 'IT_PROG';

SQL 基礎(chǔ)之子查詢(十一)


子查詢中的空值

select emp.last_name

from employees emp

where emp.employee_id not in

(select mgr.manager_id

from employees mgr);



1、HR 部門的同事想要你幫忙寫一個 SQL 語句,該 SQL 語句可以傳入一個值(員工的 last_name),然后返回結(jié)果是該員工同一部門同事的 last_name 和 hire_date,且要求該員工不在返回結(jié)果中。

舉個例子,如果用戶輸入”Zlotkey”,結(jié)果就會返回除了 Zlotkey 之外的同一部門的其他同事的

last_name 和 hire_date.


select last_name,hire_date

from employees

where department_id =(select department_id from employees

where last_name= '&&enter_name')

and last_name < > '&enter_name';


2、請查詢出所有高于平均工資的員工的 employee_id,last_name,salary,并將最終結(jié)果根據(jù)salary 降序排列。

select employee_id,last_name,salary 

from employees 

where salary > (select avg(salary) 

            from employees) 

order by salary;


3、請寫一條 SQL 語句,要求查詢出那些同一部門員工 last_name 里面包含字母”u”的員工的employee_id,last_name。

select employee_id,last_name from employees where department_id in (select department_id from employees where last_name like '%u%');



4、請幫助HR部門的同事查出所有部門location_id是1700的員工的last_name,department_id,job_id。

select last_name,department_id,job_id

from employees

where department_id in(select department_id

from departments

where location_id=1700);


讓用戶可以選擇輸入一個 location_id,然后輸出結(jié)果。

select last_name,department_id,job_id

from employees

where department_id in(select department_id

from departments

where location_id=&enter_location);


5、請查出所有需要向 King 匯報的員工的 last_name 以及 salary

select last_name,salary,manager_id

from employees

where manager_id = (select employee_id

from employees

where last_name like 'King' and manager_id is null);


6、請查出所有是執(zhí)行部(Executive)的員工的 department_id,last_name,job_id

select department_id,last_name,job_id

from employees

where department_id in(select department_id

from departments

where department_name like 'Executive');


7、請查出比 department_id 是 60 的任何員工薪水高的所有的員工的 last_name。

select department_id,last_name,salary from employees 

where salary > any 

(select salary from employees

where department_id=60);


8、查詢所有高于平均工資,并且同一部門員工 last_name 里面包含字母”u”的員工的 employee_id,last_name,salary。

select employee_id,last_name,salary

from employees

where department_id in(select department_id

from employees

where last_name like '%u%')

and salary > (select avg(salary) from employees);


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI