Commit 3accd49
committed
feat(sql-subquery): add explanation of SQL Subqueries
What
- Introduced documentation on Subqueries (nested queries inside another SQL statement).
- Covered key types:
- Scalar subquery → returns a single value.
- Row subquery → returns a single row.
- Table subquery → returns multiple rows/columns.
- Explained placement of subqueries:
- In WHERE clause → filtering based on results of another query.
- In FROM clause → using subquery as a derived table.
- In SELECT clause → computing values dynamically.
- Provided examples:
- Find employees earning more than the average salary.
- List departments with more than 5 employees.
- Retrieve top 3 highest-paid employees using nested queries.
Why
- Subqueries allow modular, readable, and powerful SQL by embedding logic instead of writing overly complex joins.
- Frequently used in analytics, reporting, and conditional filtering.
- Helps break down complex queries into smaller, more manageable parts.
How to use
- WHERE with subquery:
```sql
SELECT name FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
• FROM with subquery:
SELECT dept, COUNT(*)
FROM (SELECT * FROM employees WHERE active=1) AS active_emp
GROUP BY dept;
• SELECT with subquery:
SELECT name,
(SELECT MAX(salary) FROM employees) AS max_salary
FROM employees;
Real-life Applications
• HR systems: Find employees above company average salary.
• E-commerce: Identify products priced above category average.
• Finance: Retrieve customers with account balances higher than the median.
• Education: List students scoring above class average.
Notes
• Subqueries can often be replaced with JOINs for performance.
• Correlated subqueries re-evaluate per row → can be expensive; use carefully.
• Aliases are mandatory when subqueries are used in FROM clause.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 8f27469 commit 3accd49
File tree
7 files changed
+11
-12
lines changed- Section28JDBCusingSQLite/SQL (Aggregated Functions & Set Operations)/SubQuery SQL
7 files changed
+11
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
15 | | - | |
| 14 | + | |
16 | 15 | | |
17 | 16 | | |
18 | | - | |
| 17 | + | |
19 | 18 | | |
20 | 19 | | |
21 | 20 | | |
22 | 21 | | |
23 | | - | |
| 22 | + | |
24 | 23 | | |
25 | 24 | | |
26 | 25 | | |
27 | | - | |
| 26 | + | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
| |||
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
20 | | - | |
| 19 | + | |
21 | 20 | | |
22 | 21 | | |
23 | 22 | | |
24 | | - | |
25 | | - | |
| 23 | + | |
| 24 | + | |
26 | 25 | | |
27 | 26 | | |
28 | | - | |
| 27 | + | |
29 | 28 | | |
30 | 29 | | |
31 | 30 | | |
32 | | - | |
| 31 | + | |
0 commit comments