Skip to content

how-to-Use-Columns-From-a-Subselect-in-Where-Clause-in-SQL #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
-- Scalar Subquery
-- Find Students With the Highest GPA

SELECT name, gpa
FROM Student
WHERE gpa = (
SELECT MAX(gpa)
FROM Student
);


-- Find Students Who Scored Above the Average GPA

SELECT name, gpa
FROM Student
WHERE gpa > (
SELECT AVG(gpa)
FROM Student
WHERE gpa IS NOT NULL
);


-- IN Subquery
-- Filter Students With Matching GPA of Non-Graduated Students

SELECT name, gpa
FROM Student
WHERE gpa IN (
SELECT gpa
FROM Student
WHERE graduation_date IS NULL
AND gpa IS NOT NULL
);


-- EXISTS Subquery
-- Find Students With Duplicate GPAs

SELECT s1.name, s1.gpa
FROM Student s1
WHERE EXISTS (
SELECT 1
FROM Student s2
WHERE s2.gpa = s1.gpa
AND s2.id <> s1.id
);


-- Filtering With Multiple Columns From a Subquery

SELECT name, gpa, graduation_date
FROM Student
WHERE (gpa, graduation_date) IN (
SELECT gpa, graduation_date
FROM Student
WHERE graduation_date IS NOT NULL
GROUP BY gpa, graduation_date
HAVING COUNT(*) > 1
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
-- Scalar Subquery
-- Find Students With the Highest GPA

SELECT name, gpa
FROM Student
WHERE gpa = (
SELECT MAX(gpa)
FROM Student
);


-- Find Students Who Scored Above the Average GPA

SELECT name, gpa
FROM Student
WHERE gpa > (
SELECT AVG(gpa)
FROM Student
WHERE gpa IS NOT NULL
);


-- IN Subquery
-- Filter Students With Matching GPA of Non-Graduated Students

SELECT name, gpa
FROM Student
WHERE gpa IN (
SELECT gpa
FROM Student
WHERE graduation_date IS NULL
AND gpa IS NOT NULL
);


-- EXISTS Subquery
-- Find Students With Duplicate GPAs

SELECT s1.name, s1.gpa
FROM Student s1
WHERE EXISTS (
SELECT 1
FROM Student s2
WHERE s2.gpa = s1.gpa
AND s2.id <> s1.id
);


-- Filtering With Multiple Columns From a Subquery

SELECT name, gpa, graduation_date
FROM Student
WHERE (gpa, graduation_date) IN (
SELECT gpa, graduation_date
FROM Student
WHERE graduation_date IS NOT NULL
GROUP BY gpa, graduation_date
HAVING COUNT(*) > 1
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
-- Scalar Subquery
-- Find Students With the Highest GPA

SELECT name, gpa
FROM Student
WHERE gpa = (
SELECT MAX(gpa)
FROM Student
);


-- Find Students Who Scored Above the Average GPA

SELECT name, gpa
FROM Student
WHERE gpa > (
SELECT AVG(gpa)
FROM Student
WHERE gpa IS NOT NULL
);


-- IN Subquery
-- Filter Students With Matching GPA of Non-Graduated Students

SELECT name, gpa
FROM Student
WHERE gpa IN (
SELECT gpa
FROM Student
WHERE graduation_date IS NULL
AND gpa IS NOT NULL
);


-- EXISTS Subquery
-- Find Students With Duplicate GPAs

SELECT s1.name, s1.gpa
FROM Student s1
WHERE EXISTS (
SELECT 1
FROM Student s2
WHERE s2.gpa = s1.gpa
AND s2.id <> s1.id
);


-- Filtering With Multiple Columns From a Subquery

SELECT s1.name, s1.gpa, s1.graduation_date
FROM Student s1
WHERE EXISTS (
SELECT 1
FROM (
SELECT gpa, graduation_date
FROM Student
WHERE graduation_date IS NOT NULL
GROUP BY gpa, graduation_date
HAVING COUNT(*) > 1
) AS repeated
WHERE s1.gpa = repeated.gpa
AND s1.graduation_date = repeated.graduation_date
);