SQL & Databases - Subqueries
SQL & Databases - Subqueries
with examples
sub-query-2
[email protected]
FIPB5OK4QG
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
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
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'
[email protected]
FIPB5OK4QG 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
average_age 10
[email protected]
FIPB5OK4QG
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
[email protected] price
FIPB5OK4QG
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.
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
[email protected]
FIPB5OK4QG 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
[email protected]
FIPB5OK4QG
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
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
[email protected]
stud_id name age
FIPB5OK4QG
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
[email protected]
The query above can be part of an outer query to filter for below-average marks in science.
FIPB5OK4QG
[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
[email protected]
FIPB5OK4QG
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.
[email protected] 45
FIPB5OK4QG
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
product avg_cost
B 50
C 56
[email protected]
FIPB5OK4QG D 70
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.
[email protected] [SUBQUERY]
FIPB5OK4QG
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
[email protected]
FIPB5OK4QG
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
[email protected]
FIPB5OK4QG
How can I write a query to get this answer programmatically?
[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