SQL Keywords

SQL VIEW

What is an SQL VIEW?

Creates a virtual table whose rows and columns come from the result set of a stored SELECT statement.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL VIEW: Supported by PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite, Snowflake, BigQuery, Redshift, and most ANSI-compliant databases.

SQL VIEW Full Explanation

A VIEW is a named, stored SELECT query that behaves like a read-only table. When referenced in other queries, the database engine substitutes the view definition and executes the underlying SELECT in real time, returning the current data from the base tables. Views simplify complex joins, encapsulate business logic, enforce security by exposing only specific columns or rows, and promote code reuse. Unless the dialect supports and you explicitly create a materialized view, a standard view stores only its definition, not the data itself. Some systems allow updates through a view if the statement can be unambiguously mapped back to the base tables; otherwise the operation is rejected. Options such as OR REPLACE, TEMPORARY, WITH CHECK OPTION, and RECURSIVE vary by dialect and control replacement behavior, session scope, update constraints, or hierarchical queries. Dropping a view removes only the definition, leaving base tables intact.

SQL VIEW Syntax

-- create
CREATE [OR REPLACE] [TEMP | TEMPORARY] VIEW view_name [ (column1, column2, ...) ]
AS
SELECT ...
[WITH CHECK OPTION];

-- alter (dialect specific)
ALTER VIEW view_name AS SELECT ...;

-- drop
DROP VIEW [IF EXISTS] view_name [CASCADE | RESTRICT];

SQL VIEW Parameters

  • view_name (identifier) - Name of the view to create.
  • column_list (optional list) - Explicit column names if you want to rename or reorder columns.
  • SELECT_statement (query) - Any valid SELECT that returns rows and columns.
  • OR REPLACE (keyword) - Overwrites an existing view with the same name.
  • TEMP (TEMPORARY) - keyword|||Limits the view’s lifetime to the current session.
  • WITH CHECK OPTION (keyword) - Prevents INSERT or UPDATE statements that would make rows invisible to the view.

Example Queries Using SQL VIEW

-- 1. Encapsulate a join
CREATE VIEW active_customers AS
SELECT c.id, c.name, o.last_order_date
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE c.status = 'active';

-- 2. Secure sensitive columns
CREATE VIEW public_users AS
SELECT id, username, created_at
FROM users;

-- 3. Update through a simple view
UPDATE active_customers SET name = 'Acme Corp' WHERE id = 42;

-- 4. Replace an outdated view
CREATE OR REPLACE VIEW active_customers AS
SELECT c.id, c.name, o.last_order_date, c.segment
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE c.status = 'active';

-- 5. Remove a view
DROP VIEW IF EXISTS public_users;

Expected Output Using SQL VIEW

  • CREATE VIEW stores the definition; no rows are returned.
  • Subsequent SELECT * FROM active_customers; returns the live joined data.
  • UPDATE succeeds if the database can map the change to the customers table.
  • OR REPLACE silently swaps the old definition with the new one.
  • DROP VIEW deletes the view so future references raise an error.

Use Cases with SQL VIEW

  • Hide complex joins so application queries stay readable.
  • Expose only approved columns or filtered rows for security and compliance.
  • Provide backward compatibility when schemas evolve.
  • Serve as building blocks for analytics and reporting layers.
  • Enable row level security by granting SELECT on the view instead of the base table.

Common Mistakes with SQL VIEW

  • Assuming a standard view stores data – it stores only the query.
  • Updating a view that includes aggregates, DISTINCT, GROUP BY, or joins that make the update non-deterministic.
  • Forgetting to refresh dependent materialized views after changing a base table.
  • Dropping a base table without checking for dependent views, breaking downstream queries.
  • Naming conflicts when using OR REPLACE without verifying the new definition.

Related Topics

CREATE TABLE, MATERIALIZED VIEW, ALTER VIEW, DROP VIEW, WITH CHECK OPTION, GRANT

First Introduced In

SQL-92 Standard

Frequently Asked Questions

What is a view in SQL?

A view is a saved SELECT statement that behaves like a virtual table. Querying it runs the underlying SELECT and returns up-to-date data.

Does a view improve performance?

A view itself does not cache data, so performance is similar to running the raw SELECT. Only materialized views store results for faster reads.

How do I update data through a view?

You can update, insert, or delete through a simple view that references one table and no aggregates. Complex views are read-only.

What happens if I drop a view?

Dropping a view removes its definition. Base tables remain unchanged, but any dependent queries or applications that reference the view will fail until updated.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!