SQL & Databases - Subqueries
SQL & Databases - Subqueries
with examples
Write queries within queries to find answers to complex questions!
Examples Concept
main query Example-1 Single Row Subquery
Example-2 Multiple Row Subquery
Example-3 Multiple Column Subquery
Example-4 Nested Subquery
sub-query Example-5 Subquery using Aggregare Functi
Example-6 Subquery with Joins
Example-7 Subquery in FROM Clause
sub-query-1 Example-8 Subquery using EXISTS
sub-query-2
1 Find a list of students who scored higher than average.
stud_details
stud_id name marks
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
stud_id name marks
1001 John 30
1002 Marcus 45
1003 Robert 60
1004 Luke 70
1005 Ryan 50
1006 Chris 100
[SUBQUERY]
SELECT AVG(marks) FROM stud_details;
59
The query above can be part of an outer query to filter for above-average rows.
above-average rows.
stud_details stud_marks
stud_id name age stud_id subject
1001 John 9 1001 math
1002 Marcus 11 1001 science
1003 Robert 10 1002 math
1004 Luke 10 1002 science
1005 Ryan 9 1003 math
1006 Chris 11 1003 science
1004 math
1004 science
1005 math
1005 science
1006 math
1006 science
Let's first find the student IDs for those who have scored above 75 in math.
stud_marks
stud_id subject 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
stud_details
stud_id name age
1003 Robert 10
1004 Luke 10
1005 Ryan 9
1006 Chris 11
average_ag 10
[SUBQUERY]
SELECT stud_id FROM stud_marks WHERE marks > 75 AND subject = 'math'
stud_details
stud_id name age
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
stud_details
stud_id name age
1003 Robert 10
1004 Luke 10
1005 Ryan 9
1006 Chris 11
average_age 10
75 in math.
ud_marks
marks
50
80
60
30
80
40
85
100
90
45
95
90
above 75 in math.
RE stud_id IN (SELECT stud_id FROM stud_marks WHERE marks > 75 AND subject = 'math')
RE stud_id IN (1003,1004,1005,1006)
3 Find the transactions of the products with the lowest price, within each category
transaction
id category product cost price
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 cate
transaction
id category product cost price
1003 X A 40 43
1004 Y C 56 60
1006 Z D 70 90
Find the transactions with the lowest price for each cate
transaction
id category product cost price
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
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 w
transaction
id category product cost price
1003 X A 40 43
1004 Y C 56 60
1006 Z D 70 90
e, within each category.
stud_details stud_marks
stud_id name age stud_id subject
1001 John 9 1001 math
1002 Marcus 11 1001 science
1003 Robert 10 1002 math
1004 Luke 10 1002 science
1005 Ryan 9 1003 math
1006 Chris 11 1003 science
1004 math
1004 science
1005 math
1005 science
1006 math
1006 science
stud_marks
stud_id subject 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 I
stud_details
stud_id name age
1001 John 9
1002 Marcus 11
1003 Robert 10
1004 Luke 10
1005 Ryan 9
1006 Chris 11
Average_Ag 10
What are the rows where the student has scored below-average marks?
stud_details stud_marks
stud_id name Age stud_id subject
1001 John 9 1001 math
1002 Marcus 11 1001 science
1003 Robert 10 1002 math
1004 Luke 10 1002 science
1005 Ryan 9 1003 math
1006 Chris 11 1003 science
1004 math
1004 science
1005 math
1005 science
1006 math
1006 science
[SUBQUERY-1]
SELECT AVG(marks) FROM stud_marks WHERE subject = '
59
The query above can be part of an outer query to filter for below-average mark
[SUBQUERY-2]
SELECT stud_id FROM stud_marks WHERE marks < ([SUBQUERY-1]) AND subjec
SELECT stud_id FROM stud_marks WHERE marks < (SELECT AVG(marks) FROM s
stud_id marks
1002 30
1003 40
1005 45
Average_Ag 10
w average in science?
ud_marks
marks
50
80
60
30
80
40
85
100
90
45
95
90
the above student IDs.
erage marks?
ud_marks
marks
50
80
60
30
80
40
85
100
90
45
95
90
below-average marks
RE stud_id IN ([SUBQUERY-2])
transaction
id category product cost price
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 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.
GROUP BY product
HAVING AVG(cost) > [SUBQUERY]
GROUP BY product
HAVING AVG(cost) > (SELECT MAX(cost) FROM transaction WHERE product='A'
product avg_cost
B 50
C 56
D 70
is more than the maximum cost of product A.
r than 45.
above-average rows.
WHERE product='A')
6 Find student information and scores for students over the age of nine.
stud_details stud_marks
stud_id name age stud_id subject
1001 John 9 1001 math
1002 Marcus 11 1001 science
1003 Robert 10 1002 math
1004 Luke 10 1002 science
1005 Ryan 9 1003 math
1006 Chris 11 1003 science
1004 math
1004 science
1005 math
1005 science
1006 math
1006 science
Let's first find all the students who are more than 9 years old.
Now, let's join the above data with stud_marks to find their respective marks.
r respective marks.
another table.
> 9) as age9
7 Find all the instructors whose salary is greater than the average budget of all de
instructor_details departments
id name department_id salary id
1001 Richard 1 45000 1
1002 Michael 1 50000 2
1003 Rob 3 60000 3
1004 Marcus 2 45000
1005 Mason 2 40000
1006 Luke 3 55000
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.
FROM [SUBQUERY], instructor as ins
WHERE ins.salary > SUBQ
FROM (SELECT AVG(Budget) FROM Department) as B, Instructor as I
WHERE I.Salary > B.AVG(Budget)
departments
department budget
Business 50000
Arts 45000
Science 55000
another table.
8 Find all the details for students who passed in science.
stud_details stud_marks
stud_id name marks stud_id subject
1001 John 30 1001 math
1002 Marcus 45 1001 science
1003 Robert 60 1002 math
1004 Luke 70 1002 science
1005 Ryan 50 1003 math
1006 Chris 100 1003 science
1004 math
1004 science
1005 math
1005 science
1006 math
1006 science
Let's first find all the student IDs who have passed in science.
stud_marks
stud_id subject result
1001 math pass
1001 science pass
1002 math pass
1002 science fail
1003 math pass
1003 science fail
1004 math pass
1004 science pass
1005 math pass
1005 science pass
1006 math pass
1006 science pass
Now, let's find the student details, which have student IDs as highlighted above.
[SUBQUERY]
SELECT stud_id FROM stud_marks WHERE result = 'Pass' and subject
This query can be used to check if these student IDs "exist" as part of a table in a
as highlighted above.
" as part of a table in an outer query.