Lesson 38 +10 XP

SQL Views

SQL Views

A view is a saved SQL query that acts like a virtual table. It does not store data itself - it shows the result of its query whenever you use it.

Create a view

CREATE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = 'Brazil';

Use a view like a table

SELECT * FROM [Brazil Customers];

Views stay up to date

When the Customers table changes, the view shows the new data automatically because it re-runs its query.

Update or replace a view

CREATE OR REPLACE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName, City
FROM Customers
WHERE Country = 'Brazil';

Drop a view

DROP VIEW [Brazil Customers];

Why use views?

  • Reuse complex queries without retyping them.
  • Hide columns users shouldn't see.
  • Give a simpler name to a complicated join.

TL;DR

  • A view is a saved query acting like a table.
  • It always reflects the latest data.
  • Great for reusing complex joins and hiding columns.
  • CREATE OR REPLACE updates it; DROP VIEW removes it.