-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSub_Query.sql
More file actions
135 lines (123 loc) · 2.86 KB
/
Sub_Query.sql
File metadata and controls
135 lines (123 loc) · 2.86 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
Students(id, name, age)
Marks(id, student_id, subject, score)
-- 1. Subquery in WHERE (Single Row Subquery)
-- Q: Find students who got the highest Math score.
SELECT name
FROM Students
WHERE id = (
SELECT student_id
FROM Marks
WHERE subject = 'Math'
ORDER BY score DESC
LIMIT 1
);
-- 2. Subquery with IN (Multiple Row Subquery)
-- Q: Find students who appeared in the Math exam.
SELECT name
FROM Students
WHERE id IN (
SELECT student_id
FROM Marks
WHERE subject = 'Math'
);
-- 3. Correlated Subquery
-- Q: Find students who scored above average in Math.
SELECT name
FROM Students s
WHERE id IN (
SELECT student_id
FROM Marks
WHERE subject = 'Math' AND score > (
SELECT AVG(score)
FROM Marks
WHERE subject = 'Math'
)
);
-- 4. Subquery in FROM
-- Q: Find the average score per student, then filter those with average > 70.
SELECT name, avg_score
FROM (
SELECT student_id, AVG(score) AS avg_score
FROM Marks
GROUP BY student_id
) AS avg_table
JOIN Students ON avg_table.student_id = Students.id
WHERE avg_score > 70;
-- 5. Subquery in SELECT clause
-- Q: Show each student's name and their average score.
SELECT name,
(SELECT AVG(score)
FROM Marks
WHERE Marks.student_id = Students.id) AS average_score
FROM Students;
-- Problem 1: Students who never took Math
SELECT name
FROM Students
WHERE id NOT IN (
SELECT student_id
FROM Marks
WHERE subject = 'Math'
);
-- Problem 2: Students who took all subjects Riya did
SELECT DISTINCT s.name
FROM Students s
WHERE NOT EXISTS (
SELECT subject
FROM Marks
WHERE student_id = (
SELECT id FROM Students WHERE name = 'Riya'
)
EXCEPT
SELECT subject
FROM Marks m2
WHERE m2.student_id = s.id
);
-- Problem 3: Students whose age is above average age
SELECT name, age
FROM Students
WHERE age > (
SELECT AVG(age) FROM Students
);
-- Problem 4: Top scorer per subject
SELECT subject, student_id, score
FROM Marks m
WHERE score = (
SELECT MAX(score)
FROM Marks
WHERE subject = m.subject
);
-- Problem 5: Students who scored more than Riya's average
SELECT name
FROM Students
WHERE id IN (
SELECT student_id
FROM Marks
GROUP BY student_id
HAVING AVG(score) > (
SELECT AVG(score)
FROM Marks
WHERE student_id = (SELECT id FROM Students WHERE name = 'Riya')
)
);
-- Problem 6: Student with second highest average score
SELECT name
FROM Students
WHERE id = (
SELECT student_id
FROM (
SELECT student_id, AVG(score) AS avg_score
FROM Marks
GROUP BY student_id
ORDER BY avg_score DESC
LIMIT 1 OFFSET 1
) AS second_top
);
-- Problem 7: List each student and number of subjects more than 1
SELECT name
FROM Students
WHERE id IN (
SELECT student_id
FROM Marks
GROUP BY student_id
HAVING COUNT(DISTINCT subject) > 1
);