Views in SQL

From the SQL cheat sheet ยท Views & Stored Procedures ยท verified Jul 2026

Views

Save queries as reusable virtual tables.

sql
-- Create view
CREATE VIEW active_users AS
SELECT id, name, email
FROM users WHERE status = 'active';

-- Use it like a table
SELECT * FROM active_users WHERE age > 25;

-- Replace existing view
CREATE OR REPLACE VIEW user_summary AS
SELECT u.name, COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
GROUP BY u.id, u.name;

-- Drop view
DROP VIEW IF EXISTS active_users;
๐Ÿ’ก Views simplify complex queries โ€” define once, use everywhere like a table
โšก Materialized views (PostgreSQL) cache results for faster reads โ€” great for dashboards
๐Ÿ“Œ Regular views re-run the query each time; materialized views store the result
๐ŸŸข Views are great for access control โ€” expose only certain columns to certain users
viewsmaterialized-views

More SQL tasks

Back to the full SQL cheat sheet