Glossary

View (SQL)

A view is a stored SQL query that looks like a table. Instead of writing a complex JOIN every time, create a view and query it like a regular table. Simplifies queries, but does not always improve performance.

Example

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

-- use it like a table
SELECT * FROM active_users WHERE email LIKE '%@gmail.com';

Types

Benefits and limitations

Views simplify code and hide complexity. But nested views (a view on a view) can be hard for the optimiser to handle. In MySQL, views do not always use indexes efficiently.