SQL Views

A View is a virtual table based on the result of a SQL SELECT statement.

A View is a virtual table created from the result of a SELECT query. A view does not store data itself. Instead, it shows data from one or more tables when the view is queried.

Syntax

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

Example: Create a View, Create a view to show expensive products only.

CREATE VIEW ExpensiveOrders AS
SELECT OrderID, ProductName, Price
FROM Orders
WHERE Price > 1000;

Example: Use the View

SELECT * FROM ExpensiveOrders;

-- The view filters data automatically.

Example: View from Multiple Tables

CREATE VIEW CustomerOrders AS
SELECT Customers.CustomerName, Orders.ProductName, Orders.Price
FROM Customers
JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;

-- This view combines two tables into one virtual table.

Example: Updating a View

ALTER VIEW ExpensiveOrders AS
SELECT OrderID, ProductName, Price
FROM Orders
WHERE Price > 500;

Example:

DROP VIEW ExpensiveOrders;


Advantages of Views
✔ Simplifies complex queries
✔ Improves security (hide sensitive columns)
✔ Reusable query logic
✔ Shows data from multiple tables easily

Disadvantages of Views
❌ Some views cannot be updated
❌ Can slow performance if queries are complex

Exam/Interview Definition:
A view in SQL Server is a virtual table created from a SELECT query that displays data from one or more tables without storing the data itself.


Topics