您好,登錄后才能下訂單哦!
#where或having后面:
#標量子查詢(單行子查詢)
#列子查詢(多行子查詢)
#行子查詢(多行多列)
特點:子查詢放在小括號內,一般放在條件的右側,標量子查詢一般配備單行操作符使用
單行操作符:<> >= <= < >
列子查詢:一般搭配著多行操作符使用
多行操作符:in、any、some、all
#標量子查詢
#案例:誰的工資比ABEL高的員工信息
SELECT *
FROM employees
WHERE salary>(
SELECT salary
FROM employees
WHERE last_name='Abel'
);
#案例:返回job_id與141號員工相同,salary比143號員工多的員工的姓名、job_id和工資
SELECT
last_name,job_id,salary
FROM employees
WHERE job_id=(
SELECT job_id
FROM employees
WHERE employee_id=141
)
AND salary>(
SELECT salary
FROM employees
WHERE employee_id=143
);
#案例:返回工資最少的員工的last_name、job_id和salary
SELECT last_name,job_id,salary
FROM employees
WHERE salary=(
SELECT MIN(salary)
FROM employees
);
#案例:查詢最低工資大于50號部門最低工資的部門id和其最低工資
SELECT department_id,MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT MIN(salary)
FROM employees
WHERE department_id=50
);
#非法使用標量子查詢
SELECT department_id,MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT salary #單行操作符只能搭配標量子查詢,而這是列子查詢
FROM employees
WHERE department_id=50
);
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。