The employees-departments JOIN from Module 4 has been written out in full, by hand, in dozens of lessons since. A VIEW saves exactly that query under a name, once, so every future report just queries the name instead of rebuilding the JOIN.
Learning Objectives
- Define a VIEW precisely, and explain what it actually saves (the query, not the data).
- Distinguish a regular view from a materialized view, and know which databases in this course support the latter.
- Create, query, and drop a view on every database in this course.
Complex JOIN Query
employees + departments
Saved as a VIEW
Given a name, not re-run yet
Queried Like a Table
SELECT * FROM the_view
Underlying Query Runs
Every time, unless materialized
What a VIEW Actually Is
A VIEW is a saved SELECT statement with a name, not a copy of data. Querying a view runs the underlying SELECT fresh, every single time, so a regular view's results are always current, there's nothing to go stale. This is exactly why a view isn't a performance optimization by itself, it's a convenience and an abstraction: the complex JOIN gets written once, and reused by name everywhere else.
- + Hides a complex JOIN behind a simple, reusable name.
- + Can restrict which columns or rows a user is allowed to see, without a separate permissions system per table.
- + Always reflects the current data, no refresh step needed.
- − No faster than running the underlying query directly, a regular view adds no caching.
- − Not every view can be updated through (complex joins, aggregates, and DISTINCT usually make a view read-only).
- − Changing the underlying tables' structure can silently break a view that depends on them.
Creating and Querying a View
CREATE VIEW ... AS SELECT ... is identical, word for word, across every database in this course, and so is querying it, a view is used exactly like a table in a FROM clause, JOINs and WHERE clauses included.
CREATE VIEW active_employees_by_department AS SELECT e.id, e.first_name, e.last_name, d.name AS department_name FROM employees e JOIN departments d ON e.department_id = d.id; SELECT * FROM active_employees_by_department WHERE department_name = 'Engineering';
Dropping a View
| Database | Drop a View That Might Not Exist |
|---|---|
| MySQL / MariaDB / PostgreSQL / SQLite / SQL Server (2016+) | DROP VIEW IF EXISTS active_employees_by_department; |
| Oracle | DROP VIEW active_employees_by_department; (no IF EXISTS, errors if it doesn't exist) |
Oracle is the one database here with no IF EXISTS option on DROP VIEW at all, dropping a view that doesn't exist simply raises an error, there's no quiet no-op the way the other five allow.
Regular Views vs Materialized Views
| Regular View | Materialized View | |
|---|---|---|
| What's stored | Nothing, just the query text | The query's actual result, as real data |
| Freshness | Always current, re-runs every query | Can go stale until refreshed |
| Speed | No faster than the underlying query | Fast, it's reading pre-computed data |
| Support in this course | All six databases | Only PostgreSQL and Oracle natively |
MySQL, MariaDB, SQL Server, and SQLite have no native materialized view feature at all, the usual workaround is a real table that a scheduled job refreshes periodically, managed entirely by hand. PostgreSQL's CREATE MATERIALIZED VIEW ... AS SELECT ... and REFRESH MATERIALIZED VIEW name; and Oracle's CREATE MATERIALIZED VIEW ... are the only genuinely native implementations here.
Try It Yourself
Create a view named engineering_roster showing only employees in the Engineering department, with their full name and salary, on MySQL.
CREATE VIEW engineering_roster AS SELECT first_name, last_name, salary FROM employees e JOIN departments d ON e.department_id = d.id WHERE d.name = 'Engineering';
Frequently Asked Questions
Can a view be updated through, the way a table can with INSERT/UPDATE?
Sometimes, a simple view over a single table with no JOIN, aggregate, or DISTINCT is usually updatable on most of these databases. active_employees_by_department, with its JOIN, is a textbook example of a view that generally cannot be updated through directly, the database can't unambiguously tell which underlying table a given change belongs to.
Does querying a view cost anything extra compared to writing the JOIN directly?
No, a regular view is purely syntactic sugar, the database expands it back into the underlying query (the JOIN and all) before running it. Performance is identical to writing that same JOIN by hand every time.
Common Beginner Mistakes
Assuming a view caches or stores data
A regular view stores nothing but the query text itself, every SELECT against it re-runs the full underlying query. Only a materialized view (PostgreSQL, Oracle) actually stores a result, and even that goes stale until explicitly refreshed.
Expecting DROP VIEW IF EXISTS to work unchanged on Oracle
Oracle's DROP VIEW has no IF EXISTS clause, attempting to drop a view that doesn't exist simply raises an error there. A conditional drop on Oracle needs a PL/SQL block that catches the specific exception instead.
Best Practices
- Use a view to hide a repeated complex JOIN behind a simple, descriptive name, not as a performance optimization.
- Reach for a materialized view, where the database supports one, when a complex query's result is read far more often than the underlying data changes.
- Don't build deep chains of views on top of other views, each layer makes the final query harder to reason about and tune.
Interview Questions
What's the real difference between a regular view and a materialized view?
A regular view stores only the query text, every SELECT against it re-runs the underlying query fresh, so it's always current but never faster than the query it wraps. A materialized view stores the query's actual result as real data, making reads fast, but that data can go stale until something explicitly refreshes it (REFRESH MATERIALIZED VIEW on PostgreSQL, Oracle's equivalent refresh mechanism). Only PostgreSQL and Oracle, among the six databases in this course, support materialized views natively.
Why might active_employees_by_department not be updatable through directly?
It's defined with a JOIN across employees and departments, so an UPDATE against the view can't be unambiguously mapped back to one underlying table, the database doesn't know whether a given column change belongs to employees or departments. Simple single-table views with no JOIN, aggregate, or DISTINCT are the ones most databases will actually allow updates through.
Summary
A VIEW saves a query under a name, not its data, every query against a regular view re-runs the underlying SELECT fresh. A materialized view is the real exception, storing an actual result that goes stale until refreshed, and only PostgreSQL and Oracle support one natively among the six databases in this course.
What's Next?
SQL Injection is next: what actually goes wrong when user input gets concatenated directly into a query's text, instead of kept separate from it.
Ready to Practice SQL?
Write and run real SQL queries online, right in your browser, no install or signup required. Pick a playground based on which database you want to practice on.
Both open a third-party SQL playground in a new tab. External site terms and privacy policy apply.