第九章 子查询

Posted luckpsyduck

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第九章 子查询相关的知识,希望对你有一定的参考价值。

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;

 

以上是关于第九章 子查询的主要内容,如果未能解决你的问题,请参考以下文章

第九章 SQL查询数据库

通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账

第九章 数据查询基础

第九章习题答案

第九章 关系查询处理和查询优化——关系数据库系统的查询处理

数据库-第九章 数据库编程-9.2 关系数据库系统的查询优化