What is a Row Subquery?
A row subquery returns one row with multiple columns.
Simple rule: Compare multiple values at once!
How Row Subquery Works
Basic Syntax
SELECT *
FROM table
WHERE (col1, col2) = (SELECT val1, val2 FROM ...);Example 1: Find Exact Match
SELECT *
FROM products
WHERE (price, category) = (SELECT MAX(price), 'Electronics' FROM products);Matches: price = MAX AND category = 'Electronics'
Example 2: Find Product with Max Price & Stock
SELECT *
FROM products
WHERE price = (SELECT MAX(price) FROM products)
AND stock = (SELECT MAX(stock) FROM products);Row Subquery Rules
- Returns ONE row - multiple columns
- Compare with = - must match all columns
- Less common - scalar subqueries used more often
Try It Below
Practice finding products with specific combinations!
What Comes Next
Next: Table Subqueries - return a full result set.
Find Max Price + Electronics
Row subquery returns (100, 'Electronics'). Find matching product!
Source Table: students
| id | name | price | category |
|---|---|---|---|
| 1 | Phone | 100 | Electronics |
| 2 | Shirt | 30 | Clothing |
| 3 | Laptop | 80 | Electronics |
| 4 | Pants | 50 | Clothing |
4 rows
SQL Editor
Loading...
Filter by Category
Find all Electronics. Try: SELECT name, price FROM products WHERE category = 'Electronics';
Source Table: students
| id | name | price | category |
|---|---|---|---|
| 1 | Phone | 100 | Electronics |
| 2 | Shirt | 30 | Clothing |
| 3 | Laptop | 80 | Electronics |
| 4 | Pants | 50 | Clothing |
4 rows
SQL Editor
Loading...