Inline Views
intermediatesubqueries
2 min read
What is an Inline View?
An inline view is a subquery in the FROM clause that acts as a temporary, unnamed table. The term "inline view" is often used interchangeably with "derived table."
Inline views let you break down complex logic into stages, computing intermediate results that the outer query can filter, join, or aggregate further.
Syntax
SELECT iv.column, iv.computed
FROM (
SELECT column, expression AS computed
FROM table_name
WHERE condition
) AS iv
WHERE iv.computed > threshold;When to Use
- Simplifying deeply nested queries into logical layers
- Pre-aggregating data before joining
- Computing columns that need to be filtered (since WHERE cannot reference SELECT aliases)
Key Points
- Must Have Alias — Every inline view requires an alias after the closing parenthesis.
- Scoped — Columns are accessible only through the alias.
- CTE Alternative — WITH (CTE) provides a named alternative that can be referenced multiple times.
- Nestable — You can nest inline views, but deeply nested subqueries become hard to read.
- Optimizer — PostgreSQL may flatten inline views into the outer query for performance.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
Inline Views Challenge
Write a query that find grades with more than 3 students.
Expected result
Grades that have more than 3 students, with counts.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What is an Inline View?
Initializing database...Each run starts from fresh sample data.
More Examples
Rank and filter
Get the top 3 most enrolled courses.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
Are inline views and derived tables the same thing?
Yes. Both terms refer to a subquery in the FROM clause that acts as a temporary table.
When should I use a CTE instead of an inline view?
Use a CTE when you need to reference the result multiple times or when readability is a priority. Inline views are fine for simple, one-off uses.