Views
A view is a virtual table based on the result of a stored SQL query - it doesn't store data itself, but presents the result of its underlying query as if it were a regular table, and can be queried the same way.
Creating a View
CREATE VIEW high_value_customers AS
SELECT customer_id, first_name, total_spent
FROM customers
WHERE total_spent > 50000;
Querying a View
SELECT * FROM high_value_customers
ORDER BY total_spent DESC;
Why Use Views
- Simplify complex, frequently-used queries into a single reusable object
- Restrict access to sensitive columns by exposing only a limited view of a table
- Present a consistent, simplified structure to reporting tools and analysts
Views vs Temporary Tables
Unlike a temporary table, a view doesn't store its own copy of data - every time it's queried, its underlying SQL runs fresh against the live tables, so results always reflect the current data.
Because a view re-runs its query every time it's accessed, views built on very complex queries can be slower than querying a materialized (pre-computed) table - something to watch for in performance-sensitive reporting.