Views in SQL: How to Create, Use and Update a View

8 min read

A view in SQL is a saved SELECT query with a name. You create it once with CREATE VIEW, and from then on you can query it exactly like a table, but it stores no data of its own: every time you read from a view, the database runs the query behind it against the current rows in the underlying tables. That is why a view is often called a virtual table.

Analysts use views for three jobs: to save a join or a filter that every report needs, to give a dashboard one stable thing to read, and to hide columns (phone numbers, salaries) from people who should not see them. This lesson covers all three with worked examples, then the parts that trip people up: updating through a view, materialized views, and where the main databases differ.

View syntax

These are the statements you will use. The examples refer to the skincare brand data introduced in the next section.

Statement What it does Syntax Example
CREATE VIEW Saves a query under a name CREATE VIEW name AS SELECT … CREATE VIEW delivered_orders AS SELECT order_id, amount FROM orders WHERE status = ‘Delivered’
Query a view Reads it like a table SELECT … FROM view_name SELECT * FROM delivered_orders WHERE amount > 1000
Replace a view Changes the saved query, keeps the name CREATE OR REPLACE VIEW name AS SELECT … CREATE OR REPLACE VIEW delivered_orders AS SELECT order_id, order_date, amount FROM orders WHERE status = ‘Delivered’
DROP VIEW Deletes the view, not the data DROP VIEW name DROP VIEW delivered_orders
Column names Renames the view’s columns CREATE VIEW name (col1, col2) AS SELECT … CREATE VIEW city_sales (city, revenue) AS SELECT city, SUM(amount) FROM …
WITH CHECK OPTION Blocks writes through the view that the view could not see CREATE VIEW name AS SELECT … WHERE … WITH CHECK OPTION CREATE VIEW big_orders AS SELECT * FROM orders WHERE amount > 1000 WITH CHECK OPTION

CREATE OR REPLACE VIEW works in MySQL and PostgreSQL. SQL Server writes it CREATE OR ALTER VIEW, and SQLite has neither, so there you DROP VIEW and create it again.

The data used throughout

The examples use a Bengaluru D2C skincare brand that sells across India. There are two tables. The customers table holds customer_id, name, city and a phone number; the orders table holds every order with its status. Amounts are in rupees.

customer_id name city
C1 Ananya Rao Bengaluru
C2 Rohit Mehta Mumbai
C3 Sneha Iyer Chennai
C4 Karan Gill Bengaluru
order_id customer_id order_date amount status
9101 C1 2026-10-03 1299 Delivered
9102 C2 2026-10-05 849 Delivered
9103 C3 2026-10-11 1599 Cancelled
9104 C1 2026-10-18 699 Delivered
9105 C4 2026-11-02 2199 Delivered
9106 C2 2026-11-04 1149 Returned
9107 C3 2026-11-07 999 Delivered

Example 1: save the join every report repeats

Every revenue report at this brand needs the same two things: only delivered orders count, and city comes from the customers table. Without a view, every analyst writes that join and that filter into every query, and sooner or later someone forgets the status filter and counts cancelled orders as revenue.

So save it once:

Step SQL
Create the view CREATE VIEW delivered_orders AS
Its query SELECT o.order_id, o.order_date, c.city, o.amount
The join FROM orders o JOIN customers c ON c.customer_id = o.customer_id
The rule everyone must apply WHERE o.status = ‘Delivered’

Now SELECT * FROM delivered_orders returns five rows. The cancelled and returned orders are gone, and city has been attached:

order_id order_date city amount
9101 2026-10-03 Bengaluru 1299
9102 2026-10-05 Mumbai 849
9104 2026-10-18 Bengaluru 699
9105 2026-11-02 Bengaluru 2199
9107 2026-11-07 Chennai 999

Anyone can now write a short query on top of it. SELECT city, COUNT(*) AS orders, SUM(amount) AS revenue FROM delivered_orders GROUP BY city ORDER BY revenue DESC gives:

city orders revenue
Bengaluru 3 4197
Chennai 1 999
Mumbai 1 849

The person writing that query does not need to know the join exists, or that “revenue” at this company excludes returns. The view carries the business rule, and when the rule changes (say, finance decides returned orders should count until the refund clears), you change one view instead of forty queries.

Example 2: a view built on a view, for a dashboard

A Power BI or Tableau dashboard works best when it reads one tidy table. Here the dashboard needs revenue by month and city, so the second view reads from the first:

Step SQL (MySQL)
Create the view CREATE VIEW monthly_city_revenue AS
Month and totals SELECT DATE_FORMAT(order_date, ‘%Y-%m’) AS month, city, COUNT(*) AS orders, SUM(amount) AS revenue
Source FROM delivered_orders
Grouping GROUP BY DATE_FORMAT(order_date, ‘%Y-%m’), city

SELECT * FROM monthly_city_revenue ORDER BY month, city returns:

month city orders revenue
2026-10 Bengaluru 2 1998
2026-10 Mumbai 1 849
2026-11 Bengaluru 1 2199
2026-11 Chennai 1 999

The month expression is the only part that changes between databases: PostgreSQL uses TO_CHAR(order_date, ‘YYYY-MM’), SQL Server uses FORMAT(order_date, ‘yyyy-MM’), and SQLite uses strftime(‘%Y-%m’, order_date). Our lesson on date functions in SQL has the full list.

One level of view on view is fine. Five levels is a known source of pain, covered under mistakes below.

Example 3: a view always shows current data

Because a view stores the query and not the result, new rows appear in it immediately. Insert a delivered order for C4 on 9 November for 1,799 rupees:

Step SQL
New order INSERT INTO orders VALUES (9108, ‘C4’, ‘2026-11-09’, 1799, ‘Delivered’)
Read the view again SELECT * FROM monthly_city_revenue WHERE month = ‘2026-11’ ORDER BY city
month city orders revenue
2026-11 Bengaluru 2 3998
2026-11 Chennai 1 999

Nobody refreshed anything. The flip side is cost: if the query behind a view takes 40 seconds on a large orders table, every read of the view takes 40 seconds too. A view saves typing, not computing.

Using a view to hide columns

The customers table holds phone numbers. The marketing team needs city counts but should never see personal data. Create a view without the sensitive column and give them access to the view only:

Step SQL
A view without phone numbers CREATE VIEW customer_cities AS SELECT customer_id, city FROM customers
Grant read access to the view GRANT SELECT ON customer_cities TO marketing_team

The team can query customer_cities freely and has no permission on customers itself. This column-level and row-level restriction is one of the oldest uses of views, and it matters more in India since the Digital Personal Data Protection Act, 2023, made companies more careful about who can read personal data. SQLite has no users or GRANT, so this part applies to MySQL, PostgreSQL and SQL Server.

Can you update data through a view?

Sometimes. A view is updatable when each row in it maps back to exactly one row in one base table. In practice that means the view’s query has:

  • one base table, or (in MySQL and SQL Server) a join where only one table’s columns are changed
  • no GROUP BY, DISTINCT, aggregate functions or UNION
  • the columns being changed taken directly from the table, not calculated

So UPDATE delivered_orders SET amount = 1349 WHERE order_id = 9101 works in MySQL and SQL Server, and changes the row in orders. PostgreSQL is stricter: a view is automatically updatable only when its FROM clause names a single table, so the joined delivered_orders view needs a trigger there, while a single-table view such as big_orders updates directly. An UPDATE on monthly_city_revenue fails, because a monthly total has no single row to go back to. SQLite views are read-only; writing through one needs an INSTEAD OF trigger.

WITH CHECK OPTION closes a quiet loophole. Without it, you can insert a row through a view that the view itself would never show, such as a Cancelled order inserted through delivered_orders. With it, the database rejects any insert or update through the view that would fall outside its WHERE clause.

Most analysts never write through views, and that is a sensible default. Treat views as read-only and change data in the base tables.

Materialized views: when the query is too slow

A materialized view stores the result, like a table, and refreshes it on command or on a schedule. Reads are fast because nothing is recomputed. The cost is that the data is only as fresh as the last refresh.

Point MySQL PostgreSQL SQL Server
Normal view CREATE VIEW CREATE VIEW CREATE VIEW
Replace a view CREATE OR REPLACE VIEW CREATE OR REPLACE VIEW CREATE OR ALTER VIEW
Materialized view Not supported; use a summary table refreshed by a scheduled event CREATE MATERIALIZED VIEW name AS SELECT … Indexed view: CREATE VIEW … WITH SCHEMABINDING, then a unique clustered index
Refresh Your own job REFRESH MATERIALIZED VIEW name Automatic on every base-table change
ORDER BY inside a view Allowed, but do not rely on it Allowed, but do not rely on it Not allowed without TOP

Cloud warehouses such as BigQuery and Snowflake also offer materialized views, refreshed automatically. For a daily dashboard on millions of rows, a materialized view refreshed overnight is the usual answer.

View vs table vs CTE

Analysts mix these up because all three can be selected from. The difference is what they store and how long they last.

Table View Materialized view CTE
Stores rows Yes No, only the query Yes, a snapshot No
Lasts Until dropped Until dropped Until dropped One query
Data is current Yes Yes, always As of last refresh Yes
Shared with other users Yes Yes Yes No
Typical use Raw data Reusable logic and access control Fast reads of heavy summaries Steps inside one long query

If the logic is for one query you are writing today, use a CTE, which our CTE in SQL lesson covers. If other people or a dashboard will reuse it next month, make it a view.

Mistakes people make with views

**Using SELECT * in a view definition.** MySQL, PostgreSQL and SQL Server all expand the star into a fixed column list when the view is created. Add a column to the table later and the view does not show it, and in SQL Server a dropped or reordered column can make the view return the wrong data. List the columns.

Stacking views five deep. A view on a view on a view is hard to debug, and the optimiser may not simplify it well. When a dashboard is slow and the source is a tower of views, flatten it.

Expecting a view to be faster. A normal view runs its full query every time. If speed is the problem, you need a materialized view, an index, or a summary table.

Dropping a table that views depend on. Most databases let you drop the table, and the view then fails on its next read. Before dropping or renaming anything, check which views read from it.

Putting ORDER BY inside the view. Sort in the query that reads the view. Order inside a view is not guaranteed to survive into the outer result.

Views sit naturally on top of joins, so if the join in Example 1 looked unfamiliar, start with our SQL joins guide. CREATE VIEW is also a DDL statement, alongside the CREATE, ALTER and DROP commands in DDL commands in SQL, and views come up regularly in our SQL interview questions.

Frequently asked questions

What is a view in SQL?

A view is a named, saved SELECT query that you can query like a table. It stores no data itself; each time you read from it, the database runs its query against the underlying tables, so the result is always current. Views are used to reuse complex logic, give reports a stable source, and restrict which columns or rows a user can see.

What is the difference between a view and a table?

A table stores rows on disk. A view stores only a query and produces its rows when you read it. Deleting a view removes nothing from the underlying tables, while dropping a table deletes its data. A view can combine several tables, filter them and calculate columns, all of which a plain table cannot do by itself.

Can we insert or update data through a view?

Yes, if the view is simple enough that each of its rows maps to one row in one base table: no GROUP BY, DISTINCT, aggregates or UNION. MySQL, PostgreSQL and SQL Server all support this, though PostgreSQL only for views over a single table. Adding WITH CHECK OPTION stops inserts or updates that would create rows the view itself cannot see.

What is a materialized view?

A materialized view stores the result of its query physically, so reading it is fast, and it is refreshed on demand or on a schedule. PostgreSQL supports it with CREATE MATERIALIZED VIEW and REFRESH MATERIALIZED VIEW. SQL Server offers indexed views for the same purpose, and MySQL has no native version, so teams use a summary table refreshed by a scheduled job.

Does a view improve query performance?

A normal view does not, because it runs its full query every time it is read. It can make queries easier to write and maintain, but not faster. For performance, use a materialized view, an indexed view in SQL Server, or a summary table, and make sure the base tables are indexed on the columns the view joins and filters on.

How do I change or delete a view?

Use CREATE OR REPLACE VIEW in MySQL and PostgreSQL, or CREATE OR ALTER VIEW in SQL Server, to change the query while keeping the name. ALTER VIEW also works in MySQL and SQL Server. DROP VIEW name deletes the view and leaves the underlying data untouched.

Build the queries other people’s reports run on

Writing a view is easy. Knowing which business rules belong in one, and getting the join underneath it right so a dashboard does not quietly double-count, is the part that takes practice on real data with feedback.

SkilloVilla’s SQL: Beginner to Advanced course covers creating databases, conditional functions, aggregation, joins and advanced SQL statements, taught in live classes with mentor support and practice on SQL playgrounds. If you want SQL inside a full analyst programme with Excel, Python, Power BI and placement support, see the Data Analytics with Python track.

Primary Key, Foreign Key and Constraints in SQL, With…

Primary key and foreign key in SQL explained with examples: how they link tables, composite keys, ON DELETE CASCADE, and UNIQUE, CHECK and NOT...
SkilloVilla Team
6 min read

GROUP BY and HAVING in SQL, With Examples

GROUP BY in SQL explained with examples: grouping by one or more columns, HAVING vs WHERE, the order SQL runs a query in, and...
SkilloVilla Team
6 min read

CTE in SQL: The WITH Clause Explained With Examples

CTE in SQL explained: what a common table expression is, WITH clause syntax, chained CTEs, recursive CTEs, and CTE vs subquery, with worked examples.
SkilloVilla Team
6 min read

Leave a Reply