Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

SQL & Databases - Subqueries

Uploaded by

pawangs
Copyright
© © All Rights Reserved
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL & Databases - Subqueries

Uploaded by

pawangs
Copyright
© © All Rights Reserved
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Subqueries in SQL - How they work?

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

Let's first find out what the average is.


59

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

How can I write a query to get this answer programmatic

[SUBQUERY]
SELECT AVG(marks) FROM stud_details;
59
The query above can be part of an outer query to filter for above-average rows.

SELECT * FROM stud_details WHERE marks > [SUBQUERY]


SELECT * FROM stud_details WHERE marks > SELECT AVG(marks) FROM stud_det
SELECT * FROM stud_details WHERE marks > (59)

stud_id name marks


1003 Robert 60
1004 Luke 70
1006 Chris 100
erage marks?

above-average rows.

marks) FROM stud_details


2 Find the average age of students who have scored above 75 in math.

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

How can I write a query to get this answer programmatic

[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

SELECT AVG(age) as average_age FROM stud_details WHERE stud_id IN [SUBQUE


SELECT AVG(age) as average_age FROM stud_details WHERE stud_id IN (SELECT s
SELECT AVG(age) as average_age FROM stud_details WHERE stud_id IN (1003,100

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.

have scored more than 75 in math.


subject = 'math'

dent ids above 75 in

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

Let's first find the categories with the lowest prices.

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

SELECT * FROM transaction WHERE (category, price) IN [SUBQUERY]


FROM transaction
GROUP BY category)

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.

ave a matching cate


que categories along with their
4 What is the average age of students who have scored below average in science

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 average marks in 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

How can I write a query to get this answer programmatically?

[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

SELECT AVG(age) as Average_Age FROM stud_details WHERE stud_id IN ([SUBQ


FROM stud_details
WHERE stud_id IN (SELECT stud_id FROM stud_marks WHERE marks <
WHERE marks < (SELECT AVG(marks) FROM stud_marks W
AND subject = 'science')

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

UERY-1]) AND subject = 'science'


T AVG(marks) FROM stud_marks WHERE subject = 'science') AND subject = 'science'

RE stud_id IN ([SUBQUERY-2])

RE marks < ([SUBQUERY-1]) AND subject = 'science'


FROM stud_marks WHERE subject = 'science')
5 Find the products where the average cost of each product is more than the maxi

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's first find the maximum cost of product A across all


45

Let us now look for a product with an average cost greater than 45.

product avg_cost
B 50
C 56
D 70

How can I write a query to get this answer programmatic

[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.

stud_id name age


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


1002 Marcus 11 math 60
1002 Marcus 11 science 30
1003 Robert 10 math 80
1003 Robert 10 science 40
1004 Luke 10 math 85
1004 Luke 10 science 100
1006 Chris 11 math 95
1006 Chris 11 science 90

How can I write a query to get this answer programmatic


[SUBQUERY]
SELECT * FROM stud_details WHERE age > 9

stud_id name age


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.
INNER JOIN ([SUBQUERY]) as age9
ON mrks.stud_id=age9.stud_id
INNER JOIN (SELECT * FROM stud_details WHERE age > 9) as age9
ON mrks.stud_id=age9.stud_id

stud_id name age subject marks


1002 Marcus 11 math 60
1002 Marcus 11 science 30
1003 Robert 10 math 80
1003 Robert 10 science 40
1004 Luke 10 math 85
1004 Luke 10 science 100
1006 Chris 11 math 95
1006 Chris 11 science 90
ud_marks
marks
50
80
60
30
80
40
85
100
90
45
95
90

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 first find the average budget for department.


50000

Let's look for the instructor whose salary is more than $50,000.

id name department_id salary


1003 Rob 3 60000
1006 Luke 3 55000

How can I write a query to get this answer programmatica

[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)

id name department_id salary


1003 Rob 3 60000
1006 Luke 3 55000
erage budget of all departments.

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.

stud_id name marks


1001 John 30
1004 Luke 70
1005 Ryan 50
1006 Chris 100

How can I write a query to get this answer programmatic

[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

SELECT * FROM stud_details WHERE EXISTS [SUBQUERY]


SELECT * FROM stud_details WHERE EXISTS (SELECT stud_id FROM stud_marks W

stud_id name marks


1001 John 30
1004 Luke 70
1005 Ryan 50
1006 Chris 100
ud_marks
result
pass
pass
pass
fail
pass
fail
pass
pass
pass
pass
pass
pass

as highlighted above.
" as part of a table in an outer query.

d FROM stud_marks WHERE result='Pass' and subject = 'science')

You might also like