-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay - 16 Subqueries.sql
More file actions
36 lines (30 loc) · 975 Bytes
/
Day - 16 Subqueries.sql
File metadata and controls
36 lines (30 loc) · 975 Bytes
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
### Day 16 (21/11): Subqueries (WHERE clause)
**Topics:** Subqueries in WHERE, nested queries, filtering with subqueries
SELECT
p.patient_id,
p.name,
p.service,
p.satisfaction
FROM patients p
WHERE
p.service IN (
SELECT DISTINCT sw.service
FROM services_weekly sw
WHERE sw.patients_refused > 0
)
AND
p.service IN (
SELECT sw.service
FROM services_weekly sw
GROUP BY sw.service
HAVING AVG(sw.patient_satisfaction) < (
SELECT AVG(patient_satisfaction)
FROM services_weekly
)
);
📘 What I Learned Today:
✅ How to use subqueries inside WHERE
✅ How to compare service-level averages with global averages
✅ How to check existence of specific conditions (refusals)
✅ How SQL helps find hidden patterns by combining multiple conditions
✅ Subqueries make filtering smarter and more powerful