SQL CREATE VIEW Statement

The CREATE VIEW statement in SQL creates a view. A view is a virtual table based on the result set of a query. It doesn't store data itself; instead, it provides a customized way to access data from one or more underlying tables.



CREATE VIEW: Definition and Usage

Views are incredibly useful for simplifying complex queries, providing customized perspectives on your data, or for security reasons (restricting access to certain columns or rows). They act like stored queries that you can reuse. When you query a view, SQL generates the result set dynamically based on the underlying query definition.

Syntax

Syntax

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
      

Example: Creating a View

This creates a view named 'Brazil Customers' showing customer names and contact names for customers from Brazil. (Assumes a 'Customers' table exists with 'CustomerName', 'ContactName', and 'Country' columns.)

Syntax

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

A view named 'Brazil Customers' is created. This view doesn't store data but acts as a stored query. When you query this view, it will execute the SELECT statement and show only the specified data for Brazilian customers.

Querying the View

Once created, you can query the view just like a regular table:

Syntax

SELECT * FROM [Brazil Customers];
      
Output

CustomerName | ContactName
--------------------------
(Brazilian customers' names and contact names will be displayed here)
      

**Note:** The example output assumes that the `Customers` table contains data for customers located in Brazil. If there are no Brazilian customers, the query on the view will return an empty dataset.