Views

SQL · Views & Triggers

Theory

A view is a saved SELECT query with a name. CREATE VIEW high_earners AS SELECT name, salary FROM employees WHERE salary > 80000 stores only the query text, not the rows. Whenever someone runs SELECT * FROM high_earners, the database runs the stored query against the current tables, so the view always shows up-to-date data, and you can filter, join and group it just like a table.

Views hide complex joins behind a simple name, give every team the same definition of a metric, and act as a security layer: users can be granted access to a view that exposes only certain columns or rows. Simple single-table views can often be updated through, but views with joins, GROUP BY or DISTINCT generally cannot. A materialized view is different: it stores the result physically and must be refreshed, trading freshness for speed.

Why we use it

The same joins and filters get rewritten in many reports, often slightly differently, which leads to mismatched numbers. A view defines the logic once, keeps it consistent and lets users query it without knowing the underlying tables.

When to use it

Use views to simplify repeated queries, share business logic and restrict what users can see. When the underlying query is slow and is read very often, consider a materialized view or a summary table instead.

How it works

  1. CREATE VIEW high_earners AS SELECT ...
  2. Database stores the query text, not the rows
  3. User runs SELECT * FROM high_earners
  4. Engine substitutes the view's query into the user's query
  5. Runs it against the current base tables
  6. Returns fresh rows, like a table

Syntax

CREATE VIEW view_name AS
SELECT columns FROM table WHERE condition;

Example

CREATE VIEW high_earners AS
SELECT name, salary FROM employees WHERE salary > 80000;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic