Subqueries
Subqueries in the SELECT Clause
Senior Data Analyst
February 21, 2026
5 min read
The Inline Calculation
For each order, I needed to show the customer's total historical spend. A JOIN would duplicate rows. A SELECT subquery was cleaner.
The Implementation
SELECT
o.order_id,
o.order_amount,
(SELECT SUM(amount) FROM orders WHERE customer_id = o.customer_id) AS lifetime_spend
FROM orders o;
When to Use This
Pro Tip
This is essentially a correlated subquery—it runs once per row. For large tables, consider using a CTE or Window Function for better performance.
*Day 53: The Power of ANY and ALL.*