You are on page 1of 22

5

SUBQUERY

2
Outline

▰ Challenges
▰ Subquery
▰ Noncorrelated subquery
▰ Correlated subquery

3
Challenges
• 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

4
• Now we want to write a query to identify all students
who get better marks than that of the student who's
StudentID is 'V002', but we do not know the marks of
'V002'.
• Using the result of this query, here we have written
another query to identify the students who get
better marks than 80.

5
Outline 2
• Challenges
• Subquery
• Noncorrelated subquery
• Correlated subquery

6
Subqueries/INNER query
• 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.

7
• Above two queries identified students who get the
better number than the student who's StudentID is
'V002' (Abhay)

8
• Contoh penerapannya adalah misalnya kita akan menampilkan nama
mahasiswa yang mempunyai nilai matakuliah tertinggi pada
matakuliah basis data dengan kode Matakuliah mk003, berikut ini
tabel nya :

9
10
• Obtain the names of employees who never received an award

11
1. Write a query to display the name ( first name and last name ) for those
employees who gets more salary than the employee whose ID is 163.
2. Write a query to display the name ( first name and last name ), salary,
department id, job id for those employees who works in the same
designation as the employee works whose id is 169.
3. Write a query to display the name ( first name and last name ), salary,
department id for those employees who earn such amount of salary which
is the smallest salary of any of the departments.
4. Write a query to display the employee id, employee name (first name and
last name ) for all employees who earn more than the average salary.

12
A subquery as a SELECT column
• 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

13
Outline 2
• Challenges
• Subquery
• Noncorrelated subquery
• Correlated subquery

14
Noncorrelated and Correlated Subquery

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


anything from the outer query

15
A noncorrelated subquery with multiple values : IN, NOT IN

• 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 :

16
▰ Let’s see the query “Find all stocks not from
Japan ”

17
Outline 2
• Challenges
• Subquery
• Noncorrelated subquery
• Correlated subquery

18
Correlated Subquery
• Correlated subquery : the inner query relies on the outer query
before it can be resolved.
• Example:

19
Correlated subquery with NOT EXISTS
• 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

20
SUBQUERY – SUMMARY

21
Thank You 

22

You might also like