Sub topic 5
Inline Views in the `FROM` Clause
Prerequisites: basic SELECT, WHERE, GROUP BY, aggregate functions, and joins. You should also know the Oracle HR sample tables EMPLOYEES and DEPARTMENTS.
What an inline view is
An inline view is a subquery in the FROM clause. It produces a temporary result set that the outer query treats like a table. The result exists only while the statement runs.
Use an inline view when you need to calculate or filter an intermediate result before using it in the main query.
SELECT d.department_name, s.avg_sal
FROM departments d
JOIN (SELECT department_id, AVG(salary) AS avg_sal
FROM employees
GROUP BY department_id) s
ON s.department_id = d.department_id;The inner query groups employees and calculates one average salary per department. The outer query joins those results to DEPARTMENTS.
The name s is a table alias for the inline view. Use the alias when referring to its columns, such as s.avg_sal.
Filtering an intermediate result
An inline view can make a two-stage operation clear. First calculate each department's average. Then keep only departments whose average exceeds a threshold.
SELECT department_id, avg_sal
FROM (SELECT department_id, AVG(salary) AS avg_sal
FROM employees
GROUP BY department_id)
WHERE avg_sal > 8000;The WHERE clause belongs to the outer query, so it filters the grouped rows produced by the inline view. This is often easier to understand than trying to apply a condition to individual employee rows.
A query block is one SELECT statement, including its clauses and subqueries. Each inline view is a separate query block. Keeping query blocks small helps you identify where filtering and aggregation occur.
Restrictions and common errors
The inline view must return columns that the outer query can use. Give calculated expressions a column alias. Otherwise, referring to that expression from the outer query may be unclear or invalid.
Do not assume that an ORDER BY inside an inline view controls the final result order. The outermost query determines presentation order. If you need the final rows sorted, add ORDER BY to the outer query.
SELECT department_id, avg_sal
FROM (SELECT department_id, AVG(salary) AS avg_sal
FROM employees
GROUP BY department_id)
ORDER BY avg_sal DESC;When an inline view contains ORDER BY for a row-limiting operation, the ordering affects which rows are selected. Oracle Database 12c and later support FETCH FIRST for this purpose.
Inline views compared with joins and `WITH`
A join combines existing row sources directly. An inline view first transforms a row source, such as by grouping or ranking, and then lets the outer query join or filter the transformed result.
A WITH clause can give the same intermediate query a name. Prefer WITH when the intermediate result is long, reused, or central to the statement. Prefer an inline view for a short, one-use transformation.
| Technique | Best fit |
|---|---|
| Inline view | A short intermediate result used once |
| `WITH` query | A named or reused intermediate result |
| Join | Combining related row sources without a separate transformation |
Exercise
Write a query that lists each department name and its total payroll, but returns only departments whose payroll is greater than 20,000.
Show answer
SELECT d.departmentname, p.totalpayroll FROM departments d JOIN (SELECT departmentid, SUM(salary) AS totalpayroll FROM employees GROUP BY departmentid) p ON p.departmentid = d.departmentid WHERE p.totalpayroll > 20000;