Overview
Scenarios
Product Architecture
Instance Types
Compatibility Notes
SELECTc_name,c_acctbal,(SELECT AVG(c_acctbal) FROM customer) AS avg_balanceFROM customerLIMIT 5;
FROM clause, serving as a temporary table used in subsequent queries. Their key characteristic is that the subquery must be enclosed in parentheses and given an alias.SELECT seg.c_mktsegment, seg.cntFROM (SELECT c_mktsegment, COUNT(*) AS cntFROM customerGROUP BY c_mktsegment) segORDER BY seg.cnt DESC;
EXISTS, NOT EXISTS, IN, and NOT IN, it determines whether the subquery returns data, resulting in a Boolean value. Its key characteristic is that it does not concern itself with the specific values returned by the subquery, only whether any rows exist.-- EXISTS: Checks whether matching rows existSELECT c_name FROM customer cWHERE EXISTS (SELECT 1 FROM orders o WHERE o.o_custkey = c.c_custkey);-- IN: Checks whether a value exists within the result setSELECT c_name FROM customerWHERE c_nationkey IN (SELECT n_nationkey FROM nation WHERE n_name = 'CHINA');
ANY, ALL, and SOME to compare a value with the result set returned by a subquery. Its key characteristic is the combined use of comparison operators (=, >, <, and so on) with ANY/ALL.-- = ANY is equivalent to INSELECT c_name, c_acctbal FROM customerWHERE c_acctbal > ANY (SELECT o_totalprice FROM orders WHERE o_orderstatus = 'F');-- > ALL: means greater than all values returned by the subquerySELECT c_name, c_acctbal FROM customerWHERE c_acctbal > ALL (SELECT AVG(c_acctbal) FROM customer GROUP BY c_mktsegment);
>, <, =, >=, <=, <>). Their key characteristic is that the subquery must return a single row and single column (that is, scalar value). This differs from scalar subqueries in that it appears within comparison conditions of WHERE/HAVING clauses.SELECT o_orderkey, o_totalpriceFROM ordersWHERE o_totalprice > (SELECT AVG(o_totalprice) FROM orders);
SELECT c_custkey, c_name, c_acctbalFROM customerWHERE c_acctbal > (SELECT AVG(c_acctbal) FROM customer);
SELECT AVG(c_acctbal) FROM customer;
4990.51, then the outer query is equivalent to:SELECT c_custkey, c_name, c_acctbalFROM customerWHERE c_acctbal > 4990.51;
SELECT c_custkey, c_name, c_mktsegmentFROM customerWHERE c_custkey IN (SELECT DISTINCT o_custkey FROM ordersWHERE o_orderdate >= '1995-01-01');
o_custkey values; the outer query matches against this set.SELECT o_orderkey, o_custkey, o_totalprice, o_orderdateFROM orders o1WHERE o_totalprice = (SELECT MAX(o2.o_totalprice)FROM orders o2WHERE o2.o_custkey = o1.o_custkey);
o1.o_custkey. For each row in the outer query, the subquery calculates the maximum order amount for that customer, then retains only the rows where the amount equals this maximum value.SELECT c1.c_custkey, c1.c_name, c1.c_mktsegment, c1.c_acctbalFROM customer c1WHERE c1.c_acctbal > (SELECT AVG(c2.c_acctbal)FROM customer c2WHERE c2.c_mktsegment = c1.c_mktsegment);
SELECT c1.c_custkey, c1.c_name, c1.c_mktsegment, c1.c_acctbalFROM customer c1INNER JOIN (SELECT c_mktsegment, AVG(c_acctbal) AS avg_acctbalFROM customerGROUP BY c_mktsegment) c2 ON c1.c_mktsegment = c2.c_mktsegmentWHERE c1.c_acctbal > c2.avg_acctbal;
customer table twice (once for aggregation and once for joining), rather than executing a subquery for each customer, resulting in significantly improved performance.EXISTS is used to determine whether the subquery returns at least one row of data, commonly employed for existence checks.SELECT c.c_custkey, c.c_name, c.c_acctbalFROM customer cWHERE EXISTS (SELECT 1 FROM orders oWHERE o.o_custkey = c.c_custkeyAND o.o_totalprice > 300000);
SELECT c.c_custkey, c.c_name, c.c_phoneFROM customer cWHERE NOT EXISTS (SELECT 1 FROM orders oWHERE o.o_custkey = c.c_custkey);
NOT EXISTS is semantically equivalent to LEFT JOIN ... WHERE ... IS NULL, but their execution efficiency may differ in certain scenarios; this can be compared using EXPLAIN.IN is used to determine whether a value exists within the result set returned by a subquery.SELECT c_custkey, c_name, c_nationkeyFROM customerWHERE c_nationkey IN (SELECT n_nationkey FROM nationWHERE n_regionkey IN (SELECT r_regionkey FROM regionWHERE r_name = 'ASIA'));
IN and EXISTS is negligible; when the outer table is small and the subquery result set is large, EXISTS is generally more efficient.SELECT list, WHERE clause, or other locations.SELECTo_orderkey,o_totalprice,o_orderdate,(SELECT c_name FROM customer WHERE c_custkey = o_custkey) AS customer_nameFROM ordersWHERE o_orderdate = '1995-03-15';
SELECTo.o_orderkey,o.o_totalprice,o.o_orderdate,c.c_name AS customer_nameFROM orders oINNER JOIN customer c ON o.o_custkey = c.c_custkeyWHERE o.o_orderdate = '1995-03-15';
FROM clause are used as temporary tables (derived tables).SELECT c.c_custkey, c.c_name, c.c_mktsegment, c.c_acctbalFROM customer cINNER JOIN (SELECT c_mktsegment, MAX(c_acctbal) AS max_acctbalFROM customerGROUP BY c_mktsegment) seg_max ON c.c_mktsegment = seg_max.c_mktsegmentAND c.c_acctbal = seg_max.max_acctbal;
SELECT *FROM (SELECTo_orderpriority,COUNT(*) AS order_count,AVG(o_totalprice) AS avg_amountFROM ordersGROUP BY o_orderpriority) summaryWHERE order_count > 10000ORDER BY order_count DESC;
피드백