Skip to content

Commit a85de15

Browse files
committed
how-to-Use-Columns-From-a-Subselect-in-Where-Clause-in-SQL
1 parent 58d6fad commit a85de15

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
-- Scalar Subquery
2+
-- Find Students With the Highest GPA
3+
4+
SELECT name, gpa
5+
FROM Student
6+
WHERE gpa = (
7+
SELECT MAX(gpa)
8+
FROM Student
9+
);
10+
11+
12+
-- Find Students Who Scored Above the Average GPA
13+
14+
SELECT name, gpa
15+
FROM Student
16+
WHERE gpa > (
17+
SELECT AVG(gpa)
18+
FROM Student
19+
WHERE gpa IS NOT NULL
20+
);
21+
22+
23+
-- IN Subquery
24+
-- Filter Students With Matching GPA of Non-Graduated Students
25+
26+
SELECT name, gpa
27+
FROM Student
28+
WHERE gpa IN (
29+
SELECT gpa
30+
FROM Student
31+
WHERE graduation_date IS NULL
32+
AND gpa IS NOT NULL
33+
);
34+
35+
36+
-- EXISTS Subquery
37+
-- Find Students With Duplicate GPAs
38+
39+
SELECT s1.name, s1.gpa
40+
FROM Student s1
41+
WHERE EXISTS (
42+
SELECT 1
43+
FROM Student s2
44+
WHERE s2.gpa = s1.gpa
45+
AND s2.id <> s1.id
46+
);
47+
48+
49+
-- Filtering With Multiple Columns From a Subquery
50+
51+
SELECT name, gpa, graduation_date
52+
FROM Student
53+
WHERE (gpa, graduation_date) IN (
54+
SELECT gpa, graduation_date
55+
FROM Student
56+
WHERE graduation_date IS NOT NULL
57+
GROUP BY gpa, graduation_date
58+
HAVING COUNT(*) > 1
59+
);
60+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
-- Scalar Subquery
2+
-- Find Students With the Highest GPA
3+
4+
SELECT name, gpa
5+
FROM Student
6+
WHERE gpa = (
7+
SELECT MAX(gpa)
8+
FROM Student
9+
);
10+
11+
12+
-- Find Students Who Scored Above the Average GPA
13+
14+
SELECT name, gpa
15+
FROM Student
16+
WHERE gpa > (
17+
SELECT AVG(gpa)
18+
FROM Student
19+
WHERE gpa IS NOT NULL
20+
);
21+
22+
23+
-- IN Subquery
24+
-- Filter Students With Matching GPA of Non-Graduated Students
25+
26+
SELECT name, gpa
27+
FROM Student
28+
WHERE gpa IN (
29+
SELECT gpa
30+
FROM Student
31+
WHERE graduation_date IS NULL
32+
AND gpa IS NOT NULL
33+
);
34+
35+
36+
-- EXISTS Subquery
37+
-- Find Students With Duplicate GPAs
38+
39+
SELECT s1.name, s1.gpa
40+
FROM Student s1
41+
WHERE EXISTS (
42+
SELECT 1
43+
FROM Student s2
44+
WHERE s2.gpa = s1.gpa
45+
AND s2.id <> s1.id
46+
);
47+
48+
49+
-- Filtering With Multiple Columns From a Subquery
50+
51+
SELECT name, gpa, graduation_date
52+
FROM Student
53+
WHERE (gpa, graduation_date) IN (
54+
SELECT gpa, graduation_date
55+
FROM Student
56+
WHERE graduation_date IS NOT NULL
57+
GROUP BY gpa, graduation_date
58+
HAVING COUNT(*) > 1
59+
);
60+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
-- Scalar Subquery
2+
-- Find Students With the Highest GPA
3+
4+
SELECT name, gpa
5+
FROM Student
6+
WHERE gpa = (
7+
SELECT MAX(gpa)
8+
FROM Student
9+
);
10+
11+
12+
-- Find Students Who Scored Above the Average GPA
13+
14+
SELECT name, gpa
15+
FROM Student
16+
WHERE gpa > (
17+
SELECT AVG(gpa)
18+
FROM Student
19+
WHERE gpa IS NOT NULL
20+
);
21+
22+
23+
-- IN Subquery
24+
-- Filter Students With Matching GPA of Non-Graduated Students
25+
26+
SELECT name, gpa
27+
FROM Student
28+
WHERE gpa IN (
29+
SELECT gpa
30+
FROM Student
31+
WHERE graduation_date IS NULL
32+
AND gpa IS NOT NULL
33+
);
34+
35+
36+
-- EXISTS Subquery
37+
-- Find Students With Duplicate GPAs
38+
39+
SELECT s1.name, s1.gpa
40+
FROM Student s1
41+
WHERE EXISTS (
42+
SELECT 1
43+
FROM Student s2
44+
WHERE s2.gpa = s1.gpa
45+
AND s2.id <> s1.id
46+
);
47+
48+
49+
-- Filtering With Multiple Columns From a Subquery
50+
51+
SELECT s1.name, s1.gpa, s1.graduation_date
52+
FROM Student s1
53+
WHERE EXISTS (
54+
SELECT 1
55+
FROM (
56+
SELECT gpa, graduation_date
57+
FROM Student
58+
WHERE graduation_date IS NOT NULL
59+
GROUP BY gpa, graduation_date
60+
HAVING COUNT(*) > 1
61+
) AS repeated
62+
WHERE s1.gpa = repeated.gpa
63+
AND s1.graduation_date = repeated.graduation_date
64+
);

0 commit comments

Comments
 (0)