Why Learn It?
  Working with Sub-queries
Previous previous|next Next Page

A query is an operation that retrieves data from one or more tables or views. In this reference, a top-level SELECT statement is called a query, and a query nested within another SQL statement is called a subquery

A subquery answers multiple-part questions. For example, to determine who works in Blake’s department, you can first use a subquery to determine the Blake’s employee number. You can then answer the original question with the parent SELECT statement. A subquery in the FROM clause of a SELECT statement is also called an inline view. A subquery in the WHERE clause of a SELECT statement is also called a nested subquery.

Example

SELECT FIRST_NAME, LAST_NAME, EMPLOYEE_ID FROM EMPLOYEES
	WHERE MANAGER_ID = (SELECT MANAGER_ID 
	FROM EMPLOYEES WHERE LAST_NAME = 'Greenberg')