You are on page 1of 25

5

SUBQUERY

1
Outline

 Challenges
 Subquery
 Noncorrelated subquery
 Correlated subquery

2
Challenges
3

 Joins are great but sometimes you need to ask your


database more than one questions
 Or, take the result of one query and use it as the
input to another query
 Solution  subqueries
 Help to avoid duplicate data
 Make your queries more dynamic
Challenges cont.
4
Challenges cont.
5

 Find the best match for the job with below criteria
Challenges cont.
6
Challenges cont.  solved with inner join
7

Find the best match for the job with below


criteria
 Query:
SELECT mc.last_name, mc.first_name, mc.phone
FROM my_contacts AS mc
INNER JOIN job_desired AS jd
ON mc.contact_id= jd.contact_id
WHERE jd.title =‘web develover’
AND jd.salary_low <105000
AND jd.years_exp > 5;
Are there candidates to fulfill the job opening?
Using IN keywords
8

 Query:  Query:
SELECT mc.last_name, mc.first_name, mc.phone, SELECT mc.last_name, mc.first_name,
jc.title mc.phone, jc.title
FROM my_contacts AS mc FROM my_contacts AS mc
INNER JOIN job_current AS jc NATURAL JOIN job_current AS jc
ON mc.contact_id= jc.contact_id WHERE jc.title IN (‘Cook’, ‘Hairdresser’,
WHERE jc.title IN (‘Cook’, ‘Hairdresser’, ‘Waiter’, ‘Waiter’, ‘Web Designer’, ‘Web Developer’)
‘Web Designer’, ‘Web Developer’)

 Result of the first query


Outline 2
9

 Challenges
 Subquery
 Noncorrelated subquery
 Correlated subquery
Subqueries/INNER query
10

 To accomplish what those two queries do with just one query we


need to add a subquery into the query.
 Subquery : is a query that is wrapped within another query.
Anatomy of subquery
11

 One subquery returns a scalar value (one column,


one row), which is then compared against the
columns in the WHERE clause
Subquery – Example
12

 Find last name, first name of people who live in city Memphis and state TN

 Can you solve it with INNER JOIN? Write the syntax


 SUB QUERY?
Subquery – Example cont.
13

 Find last name, first name of people who live in city Memphis and state
TN

 SUB QUERY?
Subquery – Example cont.
14

 Query : Find who makes the most money out all my contacts.
Subquery – Example cont.
15

 Query : Find who makes the most money out all my contacts.
Try this yourself
16

 Write query without using subquery for slide-


15(find who makes the most money)
 Hint : study with LIMIT/TOP
A subquery as a SELECT column
17

 A subquery can be used as one of the columns in a SELECT


statement
 it can only return one value from one column
 Example:

 Remember : subquery may only return one single value, so each time it
runs, a row is return
 RESULT
Outline 2
18

 Challenges
 Subquery
 Noncorrelated subquery
 Correlated subquery
Noncorrelated and Correlated Subquery
19

 Noncorrelated subquery: if the query stands alone and doesn’t


reference anything from the outer query
A noncorrelated subquery with multiple values :
IN, NOT IN
20

 A noncorrelated subquery uses IN or NOT IN to


test if the values returned in the subquery are
members of a set (or not)
 Example :
Outline 2
21

 Challenges
 Subquery
 Noncorrelated subquery
 Correlated subquery
Correlated Subquery
22

 Correlated subquery : the inner query relies on the


outer query before it can be resolved.
 Example:
Correlated subquery with NOT EXISTS
23

 A very common use for correlated subqueries is to find all the


rows in the outer query for which no rows exists in a related
table.
 Example : Find the first and the last names and email adresses
of the people from the my_contacts table who are not
currently listed in the job_current table
SUBQUERY – SUMMARY
24
Thank You 
25

You might also like