码迷,mamicode.com
首页 > 其他好文 > 详细

第九章 子查询

时间:2019-11-26 10:44:31      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:where   col   name   dual   customer   讲解   man   _id   employees   

1. 基本描述

  本章主要讲解各种子查询。

2. 基本样例

SELECT account_id, product_cd, cust_id, avail_balance FROM account
WHERE account_id = (SELECT MAX(account_id) FROM account);

SELECT account_id, product_cd, cust_id, avail_balance FROM account
WHERE open_emp_id <> (SELECT e.emp_id FROM employee e INNER JOIN branch b ON e.assigned_branch_id = b.branch_id
WHERE e.title = "Head Teller" AND b.city = "Woburn");

SELECT branch_id, name, city FROM branch WHERE name IN ("Headquarters", "Quincy Branch");

SELECT branch_id, name, city FROM branch WHERE name = "Headquarters" OR name = "Quincy Branch";

SELECT emp_id, fname, lname, title FROM employee WHERE emp_id IN (SELECT superior_emp_id FROM employee);

SELECT emp_id, fname, lname, title FROM employee 
WHERE emp_id NOT IN (SELECT superior_emp_id FROM employee WHERE superior_emp_id IS NOT NULL);

SELECT emp_id, fname, lname, title FROM employee 
WHERE emp_id <> ALL (SELECT superior_emp_id FROM employee WHERE superior_emp_id IS NOT NULL);

SELECT emp_id, fname, lname, title FROM employee WHERE emp_id NOT IN (1, 2, NULL);

SELECT account_id, cust_id, product_cd, avail_balance FROM account
WHERE avail_balance < ALL (SELECT a.avail_balance FROM account a INNER JOIN individual i ON
a.cust_id = i.cust_id WHERE i.fname = Frank AND i.lname = Tucker);

SELECT account_id, cust_id, product_cd, avail_balance FROM account 
WHERE avail_balance > ANY (SELECT a.avail_balance FROM account a INNER JOIN individual i ON a.cust_id = i.cust_id
WHERE i.fname = Frank AND i.lname = Tucker);

SELECT account_id, product_cd, cust_id FROM account
WHERE open_branch_id = (SELECT branch_id FROM branch WHERE name = Woburn Branch)
AND open_emp_id IN (SELECT emp_id FROM employee WHERE title = Teller OR title = Head Teller);

SELECT c.cust_id, c.cust_type_cd, c.city FROM customer c
WHERE 2 = (SELECT COUNT(*) FROM account a WHERE a.cust_id = c.cust_id);

SELECT c.cust_id, c.cust_type_cd, c.city FROM customer c
WHERE (SELECT SUM(a.avail_balance) FROM account a WHERE a.cust_id = c.cust_id) BETWEEN 5000 AND 10000;

SELECT a.account_id, a.product_cd, a.cust_id FROM account a WHERE
NOT EXISTS (SELECT 1 FROM business b WHERE b.cust_id = a.cust_id);

SELECT d.dept_id, d.name, e_cnt.how_many num_employees FROM 
department d INNER JOIN (SELECT dept_id, COUNT(*) how_many FROM employee GROUP BY dept_id) e_cnt
ON d.dept_id = e_cnt.dept_id;

SELECT open_emp_id, COUNT(*) how_many FROM account GROUP BY open_emp_id
HAVING COUNT(*) = (SELECT MAX(emp_cnt.how_many) FROM 
(SELECT COUNT(*) how_many FROM account GROUP BY open_emp_id) emp_cnt);

SELECT emp.emp_id, CONCAT(emp.fname, " ", emp.lname) emp_name, 
(SELECT CONCAT(boss.fname,  , boss.lname) FROM employee boss WHERE boss.emp_id = emp.superior_emp_id) boss_name
FROM employee emp WHERE emp.superior_emp_id IS NOT NULL ORDER BY (SELECT boss.lname FROM employee boss 
WHERE boss.emp_id = emp.superior_emp_id), emp.lname;

 

第九章 子查询

标签:where   col   name   dual   customer   讲解   man   _id   employees   

原文地址:https://www.cnblogs.com/LuckPsyduck/p/11933802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!