A subquery is a query nested inside another query — used in WHERE, FROM, SELECT, or with EXISTS. It lets you use the result of one query within another, enabling complex filtering and computations.
A subquery is a query nested inside another query — used in WHERE, FROM, SELECT, or with EXISTS. It lets you use the result of one query within another, enabling complex filtering and computations.
-- find users who have placed an order (their id is in the orders table)
SELECT name FROM users
WHERE id IN (SELECT user_id FROM orders); -- the subquery returns a list of user_ids
-- find products more expensive than the average
SELECT name, price FROM products
WHERE price > (SELECT AVG(price) FROM products); -- subquery returns a single value
A subquery in WHERE provides values to filter against — a list (with IN) or a single value (with comparison operators).
-- for each user, find those with above-average orders FOR THEIR COUNTRY
SELECT name FROM users u
WHERE order_count > (
SELECT AVG(order_count) FROM users
WHERE country = u.country -- references the OUTER query's row (correlated)
);
A correlated subquery references the outer query — it runs once per outer row (potentially slower). An uncorrelated subquery runs once.
-- users who have at least one order (often more efficient than IN)
SELECT name FROM users u
WHERE EXISTS (
SELECT 1 FROM orders o WHERE o.user_id = u.id -- just checks existence
);
EXISTS checks whether a subquery returns any rows — often more efficient than IN for existence checks (it can stop at the first match).
-- use a subquery result as a table
SELECT country, avg_age FROM (
SELECT country, AVG(age) AS avg_age FROM users GROUP BY country
) AS country_stats
WHERE avg_age > 30;
Many subqueries can be rewritten as JOINs (often more efficient/readable).
WHERE id IN (SELECT ...) → can often be a JOIN
Use subqueries for clarity in filtering/aggregation; JOINs for combining data.
CTEs (WITH) are often a cleaner alternative to complex nested subqueries.
సబ్కోరీలు సంక్లిష్ట ప్రశ్నలను అడిగేందుకు ఒక ముఖ్యమైన SQL టెక్నిక్ — ఒక ప్రశ్న ఫలితాన్ని మరొక ప్రశ్నలో ఉపయోగించడం సరళమైన ప్రశ్నలు వ్యక్తపరచలేని ఫిల్టరింగ్ మరియు గణనలను సక్షమ చేస్తుంది, కాబట్టి వాటిని అర్థం చేసుకోవడం వాస్తవ-ప్రపంచ ప్రశ్నలను వ్రాయడానికి విలువైనది.
ఫారమ్లను తెలుసుకోవడం — WHEREలో సబ్కోరీలు (INతో జాబితా ద్వారా ఫిల్టర్ చేయడం లేదా పోల్చడం ఆపరేటర్లతో ఒకే విలువ), సంబంధిత సబ్కోరీలు (బాహ్య ప్రశ్నను సూచించడం, ప్రతి అడ్డు వరుసకు నడుస్తుంది), EXISTS (సమర్థవంతమైన ఉనికి తనిఖీలు), మరియు FROMలో సబ్కోరీలు (ఉత్పన్న పట్టికలు) — సంక్లిష్ట తర్కాన్ని వ్యక్తపరచడానికి సాధారణ నమూనాలను కవర్ చేస్తుంది.
సంబంధిత మరియు సంబంధం లేని సబ్కోరీల మధ్య వ్యత్యాసాన్ని అర్థం చేసుకోవడం పనితీరు కోసం ముఖ్యమైనది (సంబంధిత సబ్కోరీలు ప్రతి బాహ్య అడ్డు వరుసకు చేసి నడుస్తాయి మరియు నెమ్మదిగా ఉండవచ్చు).
సమానంగా విలువైనది JOINలు మరియు CTEలకు సంబంధం: అనేక సబ్కోరీలను JOINలుగా (తరచుగా మరింత సమర్థవంతమైన మరియు పఠనీయమైన) లేదా CTEలుగా (సంక్లిష్ట సందర్భాలకు పరిష్కారమైన) తిరిగి వ్రాయవచ్చు, కాబట్టి ప్రతిదాన్ని ఎప్పుడు ఉపయోగించాలో తెలుసుకోవడం — ఫిల్టర్ చేయడం/సమీకరణ స్పష్టతకు సబ్కోరీలు, డేటాను కలపడానికి JOINలు — ఒక ఆచరణాత్మక నైపుణ్యం.
బహుశా నిజ-ప్రపంచ ప్రశ్నలకు సమ్మేళన (సమీకరణ ఫలితాల ద్వారా ఫిల్టర్ చేయడం, ఉనికి తనిఖీలు, గణించిన విలువలతో పోల్చడం) అవసరం ఉంటుంది, మరియు సబ్కోరీలు, వాటి పనితీరు లక్షణాలను (ముఖ్యంగా సంబంధిత వాటిని) గ్రహించడం, మరియు JOINలు/CTEలకు వాటి సంబంధం సమర్థవంతమైన, సమర్థ సంక్లిష్ట ప్రశ్నలను వ్రాయడానికి ముఖ్యమైనది, సబ్కోరీలను నియంత్రించడం విలువైన, తరచుగా-ఉపయోగించిన జ్ఞానం ప్రాథమిక ప్రశ్న యొక్క బయటకు SQL అయి ఉంటుంది మరియు సంక్లిష్ట డేటా అవసరాలను వ్యక్తపరచే సామర్థ్యాన్ని ప్రదర్శించే సాధారణ साक్ష ఉంపుటపై.