A Redshift subquery is a SELECT statement nested inside another query to filter, compute, or supply data.
A subquery is a SELECT placed inside another query’s SELECT, FROM, or WHERE clause to provide a value set, single value, or virtual table the outer query can use.
Use a subquery when the nested logic depends on row-by-row evaluation, requires aggregation before filtering, or when simplifying complex JOINs improves readability.
Wrap the inner SELECT in parentheses. The outer query can treat the result as a table, list, or scalar value depending on context.
Return each customer with total orders computed inline.
Filter customers who bought out-of-stock products using EXISTS.
Create an inline view that aggregates order revenue, then join to Customers for reporting.
Rewrite correlated subqueries as JOINs when possible, ensure inner queries have selective predicates, and avoid returning unnecessary columns.
1️⃣ Keep subqueries small and indexed by distribution/sort keys. 2️⃣ Use LIMIT for testing. 3️⃣ Materialize heavy subqueries into temporary tables for reuse.
Yes, but Redshift ignores ORDER BY unless you also use LIMIT. Sort the result in the outer query if presentation order matters.
Often yes, because they re-execute per outer row. Rewrite as a JOIN or pre-aggregate in a CTE for better performance.
Redshift supports up to 32 levels of nesting, but readability and performance decline long before that limit.