SyntaxStudy
Sign Up
MySQL Intermediate 9 min read

Views

MySQL Views

A view is a saved query that acts like a virtual table.

Creating a View

CREATE VIEW active_users AS
SELECT id, username, email, created_at
FROM users
WHERE deleted_at IS NULL AND status = 'active';

-- Use like a table
SELECT * FROM active_users WHERE username LIKE 'a%';

View with JOIN

CREATE VIEW order_summary AS
SELECT
    o.id,
    u.username,
    SUM(oi.price * oi.qty) AS total,
    o.created_at
FROM orders o
JOIN users u ON u.id = o.user_id
JOIN order_items oi ON oi.order_id = o.id
GROUP BY o.id;

Dropping

DROP VIEW active_users;
CREATE OR REPLACE VIEW active_users AS ...;
Pro Tip

Views simplify complex queries but don't store data — they always run the underlying query.