What are views in SQL

· Category: SQL & Databases

Short answer

A view is a stored query that acts as a virtual table. It does not store data itself but presents the result of its underlying query each time it is accessed.

How it works

When you query a view, the database merges the view definition with your query and executes the combined statement. Views can join multiple tables, filter rows, and expose only selected columns.

Example

CREATE VIEW active_customers AS
SELECT id, name, email FROM customers WHERE active = 1;
SELECT * FROM active_customers;

Why it matters

Views abstract complexity, enforce security by hiding sensitive columns, and provide a stable interface when underlying schemas change. Materialized views cache results for expensive aggregations.

Common issues

  • Updatable views have restrictions; not all views support INSERT, UPDATE, or DELETE.
  • Nesting views too deeply can make execution plans opaque and hard to optimize.r