SQL Subquery
SQL Subquery
What is Subquery?
• A Subquery or Inner query or a Nested query is a query within
another SQL query (main query)
• Usually located in WHERE or HAVING clause
• A subquery is used to return data or set of data that will be used in
the main query as a condition to further restrict the data to be
retrieved
• Subqueries can be used with the following SQL statements along with
the comparison operators like !=, <>, =, <, >, >=, <=, IN, NOT IN
What is Subquery?
MULTIPLE-COLUMN SUBQUERY
Where do we start? FINAL RESULT
• Subquery will be execute first, 4 R3 MAIN
QUERY
result from subquery will be
passed to the main query for 3 R2
further process
• When reading or writing SQL R1
subqueries, you should start from 2 SUBQUERIES
the bottom upwards, working out
which data is to be passed to the 1
next query up.
Questions
• Display employee number, first name, last name and salary for any
employees whose salary is greater than average salary?
• Display department number and department name that do not
involve in any projects.
• Display employee number, first name, last name, sex, education level,
and department number who has similar sex and education level with
employee number 000280
ROLLBACK VS COMMIT
TYPE THESE CODE