Sub topic 4
Where Subqueries Can Appear
Assume you know basic SELECT, WHERE, GROUP BY, joins, and aggregate functions such as AVG and COUNT. A subquery is a query nested inside another SQL statement. Its position determines how the outer query uses its result.
Subqueries in the WHERE Clause
A subquery in WHERE supplies a value or set of values used to filter rows. For example, this query returns employees whose salary is above the company average:
SELECT first_name, last_name, salary
FROM employees
WHERE salary > (SELECT AVG(salary)
FROM employees);The inner query returns one value, the average salary. Because the subquery returns one row, the outer query can use the single-value operator >.
A subquery that returns several rows needs a set operator such as IN:
SELECT department_name
FROM departments
WHERE department_id IN (SELECT department_id
FROM employees
WHERE salary > 10000);The inner query returns department IDs. IN keeps each department whose ID appears in that result.
Subqueries in the HAVING Clause
Use a subquery in HAVING when you need to filter groups based on an aggregate or another result. This example keeps departments whose average salary exceeds the company-wide average:
SELECT department_id, AVG(salary) AS department_average
FROM employees
GROUP BY department_id
HAVING AVG(salary) > (SELECT AVG(salary)
FROM employees);WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY has created them.
Subqueries in the FROM Clause
A subquery in FROM is called an inline view. It produces a temporary result set that the outer query can treat like a table. Give the inline view an alias:
SELECT d.department_name, s.avg_salary
FROM departments d
JOIN (SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id) s
ON s.department_id = d.department_id;The inline view calculates one average per department. The outer query joins those results to departments.
Subqueries in the SELECT List
A scalar subquery in the SELECT list calculates a value for each output row:
SELECT d.department_name,
(SELECT COUNT(*)
FROM employees e
WHERE e.department_id = d.department_id) AS headcount
FROM departments d;This subquery is correlated because it refers to d.department_id from the outer query. Oracle evaluates it in the context of each department.
A scalar subquery must return one column and no more than one row. If it returns multiple rows, Oracle raises ORA-01427: single-row subquery returns more than one row.
| Placement | Main purpose | Typical result |
|---|---|---|
| `WHERE` | Filter rows | One value or a set of values |
| `HAVING` | Filter groups | One comparison value |
| `FROM` | Build an inline view | A temporary table-like result |
| `SELECT` | Calculate an output value | One value per outer row |
Choosing a Placement
Use WHERE for row conditions and HAVING for grouped conditions. Use FROM when an intermediate result needs its own columns or must be joined. Use SELECT for a related value displayed beside each outer row, but check that the scalar subquery cannot return multiple rows.
Exercise
Which clause should contain a subquery that calculates each department's average salary before joining that result to DEPARTMENTS?
Show answer
Use the FROM clause and write the subquery as an inline view.