Views — Saved Queries in SQL
Name a query once, use it like a table forever
A view is a named SELECT stored in the database. CREATE VIEW eng_earners AS SELECT … saves the query; afterwards you SELECT from eng_earners like any table. The query runs fresh at every use, so a view always shows current data. Views hide complexity, standardise business definitions (what counts as an “active customer”), and can restrict access by exposing only some columns.
Save the query, not the rows
CREATE VIEW eng_earners AS SELECT id, name, salary FROM employees WHERE department = 'Engineering' — the definition is stored; no data is copied.
Query it like a table
SELECT * FROM eng_earners — the engine expands the stored query and runs it against the current data, fresh every time.
It stays correct automatically
Insert, update or delete employees and the view reflects it on the next select — there is nothing to refresh.
Views compose
A view may select from other views — building a library of named business definitions layer by layer.
Manage the lifecycle
CREATE OR REPLACE VIEW updates the definition; DROP VIEW removes it. Permissions are granted per view, independently of the base table.
1CREATE VIEW eng_earners AS2 SELECT id, name, salary FROM employees WHERE department = 'Engineering';3SELECT * FROM eng_earners ORDER BY salary DESC;
| id | name | salary |
|---|---|---|
| 3 | Cynthia | 91000 |
| 1 | Alice | 85000 |
| 6 | Farid | 73000 |
The stored query runs at SELECT time, so it reflects every INSERT since the view was created. If Farid is promoted into Engineering tomorrow, he appears in eng_earners automatically — no maintenance.
Create, then select
The saved query runs live at every use.
1CREATE VIEW eng_earners AS2 SELECT id, name, salary FROM employees WHERE department = 'Engineering';3SELECT * FROM eng_earners ORDER BY salary DESC;
| id | name | salary |
|---|---|---|
| 3 | Cynthia | 91000 |
| 1 | Alice | 85000 |
| 6 | Farid | 73000 |
The view tracks its base table
New Engineering hires appear without touching the view.
1INSERT INTO employees (id, name, department, salary, age)2VALUES (7, 'Grace', 'Engineering', 81000, 31);3SELECT name FROM eng_earners;
| name |
|---|
| Alice |
| Cynthia |
| Farid |
| Grace |
Views as a security boundary
Expose the safe columns; hide salary and id.
1CREATE VIEW staff_directory AS2 SELECT name, department FROM employees;3SELECT * FROM staff_directory;4-- GRANT SELECT ON staff_directory TO analyst; -- base table stays hidden
| name | department |
|---|---|
| Alice | Engineering |
| Bob | Sales |
| Cynthia | Engineering |
| David | Marketing |
| Elena | Sales |
| Farid | Engineering |
One name for one business rule
“Active customer” means one thing everywhere.
1CREATE VIEW active_customers AS2 SELECT * FROM customers3 WHERE last_order_at >= NOW() - INTERVAL '90 days';4SELECT COUNT(*) FROM active_customers;
| count |
|---|
| 4217 |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.