-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay - 17 Subqueries (SELECT and FROM clause).sql
More file actions
49 lines (41 loc) · 1.54 KB
/
Day - 17 Subqueries (SELECT and FROM clause).sql
File metadata and controls
49 lines (41 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
### Day 17 (22/11): Subqueries (SELECT and FROM clause)
**Topics:** Subqueries in SELECT, derived tables, inline views
SELECT
sw.service,
SUM(sw.patients_admitted) AS total_admitted,
SUM(sw.patients_admitted)
- (SELECT AVG(total_admissions)
FROM (
SELECT SUM(patients_admitted) AS total_admissions
FROM services_weekly
GROUP BY service
) AS avg_table
) AS difference_from_average,
CASE
WHEN SUM(sw.patients_admitted) >
(SELECT AVG(total_admissions)
FROM (
SELECT SUM(patients_admitted) AS total_admissions
FROM services_weekly
GROUP BY service
) AS avg_table
) THEN 'Above Average'
WHEN SUM(sw.patients_admitted) =
(SELECT AVG(total_admissions)
FROM (
SELECT SUM(patients_admitted) AS total_admissions
FROM services_weekly
GROUP BY service
) AS avg_table
) THEN 'Average'
ELSE 'Below Average'
END AS rank_indicator
FROM services_weekly sw
GROUP BY sw.service
ORDER BY total_admitted DESC;
📘 What I Learned Today
✅ How subqueries inside FROM create a “mini-table”
✅ How subqueries inside SELECT help compare values
✅ Why derived tables are powerful in reporting
✅ How to calculate averages and compare each service against it
✅ How to turn calculations into meaningful labels