SQL & Databases - Subqueries
SQL & Databases - Subqueries
with examples
sub-query-2
1 Find a list of students who scored higher than average.
stud_details
1001 John 30
1002 Marcus 45
1003 Robert 60
1004 Luke 70
1005 Ryan 50
1006 Chris 100
What are the rows where the student has scored above-average marks?
stud_details
1001 John 30
1002 Marcus 45
1003 Robert 60
1004 Luke 70
1005 Ryan 50
1006 Chris 100
1003 Robert 60
1004 Luke 70
1006 Chris 100
2 Find the average age of students who have scored above 75 in math.
stud_details stud_marks
Let's first find the student IDs for those who have scored above 75 in math.
stud_marks
1001 math 50
1001 science 80
1002 math 60
1002 science 30
1003 math 80
1003 science 40
1004 math 85
1004 science 100
1005 math 90
1005 science 45
1006 math 95
1006 science 90
Now, let's find the average age for those student IDs who have scored more than 75 in math.
stud_details
1003 Robert 10
1004 Luke 10
1005 Ryan 9
1006 Chris 11
average_age 10
[SUBQUERY]
SELECT stud_id FROM stud_marks WHERE marks > 75 AND subject = 'math'
stud_details
1003 Robert 10
1004 Luke 10
1005 Ryan 9
1006 Chris 11
The query above can be part of an outer query to filter student ids above 75 in math.
stud_details
1003 Robert 10
1004 Luke 10
1005 Ryan 9
1006 Chris 11
average_age 10
3 Find the transactions of the products with the lowest price, within each category.
transaction
1001 X A 45 50
1002 X B 50 60
1003 X A 40 43
1004 Y C 56 60
1005 Y D 70 85
1006 Z D 70 90
category price
X 43
Y 60
Z 90
Let's return the transaction details of those records that have a matching category and price.
transaction
1003 X A 40 43
1004 Y C 56 60
1006 Z D 70 90
Find the transactions with the lowest price for each category.
transaction
1001 X A 45 50
1002 X B 50 60
1003 X A 40 43
1004 Y C 56 60
1005 Y D 70 85
1006 Z D 70 90
[SUBQUERY]
SELECT Category, MIN(Price) as MinPrice FROM Transaction GROUP BY Category
The above query will give me this table:
category price
X 43
Y 60
Z 90
The query above can be part of an outer query to filter unique categories along with their lowest price.
transaction
1003 X A 40 43
1004 Y C 56 60
1006 Z D 70 90
4 What is the average age of students who have scored below average in science?
stud_details stud_marks
stud_marks
1001 math 50
1001 science 80
1002 math 60
1002 science 30
1003 math 80
1003 science 40
1004 math 85
1004 science 100
1005 math 90
1005 science 45
1006 math 95
1006 science 90
64
Now, find the student IDs whose science grades are below the national average.
stud_id marks
1002 30
1003 40
1005 45
Now, let's retrieve the average age of the rows that match the above student IDs.
stud_details
1001 John 9
1002 Marcus 11
1003 Robert 10
1004 Luke 10
1005 Ryan 9
1006 Chris 11
Average_Age 10
What are the rows where the student has scored below-average marks?
stud_details stud_marks
[SUBQUERY-1]
SELECT AVG(marks) FROM stud_marks WHERE subject = 'science'
59
The query above can be part of an outer query to filter for below-average marks in science.
[SUBQUERY-2]
SELECT stud_id FROM stud_marks WHERE marks < ([SUBQUERY-1]) AND subject = 'science'
SELECT stud_id FROM stud_marks WHERE marks < (SELECT AVG(marks) FROM stud_marks WHERE subject = 'science') AND subject = 'science'
stud_id marks
1002 30
1003 40
1005 45
Average_Age 10
5 Find the products where the average cost of each product is more than the maximum cost of product A.
transaction
1001 X A 45 50
1002 X B 50 60
1003 X A 40 43
1004 Y C 56 60
1005 Y D 70 85
1006 Z D 70 90
Let's first find the maximum cost of product A across all categories.
45
Let us now look for a product with an average cost greater than 45.
product avg_cost
B 50
C 56
D 70
[SUBQUERY]
SELECT MAX(cost) FROM transaction WHERE product = 'A';
45
The query above can be part of an outer query to filter for above-average rows.
product avg_cost
B 50
C 56
D 70
6 Find student information and scores for students over the age of nine.
stud_details stud_marks
Let's first find all the students who are more than 9 years old.
1002 Marcus 11
1003 Robert 10
1004 Luke 10
1006 Chris 11
Now, let's join the above data with stud_marks to find their respective marks.
stud_id name age subject marks
[SUBQUERY]
SELECT * FROM stud_details WHERE age > 9
1002 Marcus 11
1003 Robert 10
1004 Luke 10
1006 Chris 11
The query above can be part of an outer query to join with another table.
instructor_details departments
Let's look for the instructor whose salary is more than $50,000.
[SUBQUERY]
SELECT AVG(Budget) FROM Department
50000
The query above can be part of an outer query to join with another table.
stud_details stud_marks
Let's first find all the student IDs who have passed in science.
stud_marks
Now, let's find the student details, which have student IDs as highlighted above.
1001 John 30
1004 Luke 70
1005 Ryan 50
1006 Chris 100
[SUBQUERY]
SELECT stud_id FROM stud_marks WHERE result = 'Pass' and subject = 'science'
This query can be used to check if these student IDs "exist" as part of a table in an outer query.
1001 John 30
1004 Luke 70
1005 Ryan 50
1006 Chris 100