Meritshot Tutorials

  1. Home
  2. »
  3. SQL Views

SQL Tutorial

SQL Views

A VIEW in SQL is a virtual table that provides a way to present data from one or more tables in a specific format or structure. Views do not store data themselves but are defined by a query that retrieves data from underlying tables. They can be used to simplify complex queries, encapsulate data access, and provide a consistent interface to data.

Syntax

— Creating a view

CREATE VIEW view_name AS

SELECT column1, column2, …

FROM table_name

WHERE condition;

— Updating a view

CREATE OR REPLACE VIEW view_name AS

SELECT column1, column2, …

FROM table_name

WHERE condition;

— Dropping a view

DROP VIEW view_name;

  • view_name: The name of the view to be created, updated, or dropped.
  • column1, column2, …: The columns to be included in the view.
  • table_name: The underlying table(s) from which the view retrieves data.
  • condition: The criteria for filtering data in the view.

Example:

1 Creating a Basic View

Suppose you have a table Employees and you want to create a view that shows only employees from the Sales department:

CREATE VIEW SalesEmployees AS

SELECT EmployeeID, FullName, Position

FROM Employees

WHERE Department = ‘Sales’;

This view, SalesEmployees, will display only employees who work in the Sales department.

2 Updating an Existing View

If you want to update the SalesEmployees view to include the HireDate column:

CREATE OR REPLACE VIEW SalesEmployees AS

SELECT EmployeeID, FullName, Position, HireDate

FROM Employees

WHERE Department = ‘Sales’;

3 Updating an Existing View

If you want to update the SalesEmployees view to include the HireDate column:

CREATE OR REPLACE VIEW SalesEmployees AS

SELECT EmployeeID, FullName, Position, HireDate

FROM Employees

WHERE Department = ‘Sales’;

4 Creating a View with Joins

Suppose you have two tables, Orders and Customers, and you want to create a view that combines order details with customer information:

CREATE VIEW CustomerOrders AS

SELECT o.OrderID, o.OrderDate, c.CustomerName, c.ContactNumber

FROM Orders o

JOIN Customers c ON o.CustomerID = c.CustomerID;

This view, CustomerOrders, will show order details along with the customer names and contact numbers.

Tips to Remember

  1. Views Are Read-Only: In many database systems, views are read-only. To update data, you may need to perform updates on the underlying tables directly.
  2. Use for Security: Views can help in restricting access to sensitive data by exposing only specific columns or rows.
  3. Performance Considerations: While views simplify query writing, be aware that complex views or views based on other views can affect query performance.

Frequently Asked Questions

Q1: Can a view be based on another view?
A1: Yes, views can be nested. A view can be created based on another view, but be cautious of performance impacts and complexity.

Q2: Can I use views to enforce data integrity?
A2: Views themselves do not enforce data integrity, but they can provide a simplified and controlled way to access data, reducing the risk of erroneous data manipulation.

Q3: How do I refresh a view if the underlying data changes?
A3: Views are updated automatically with the latest data from the underlying tables. No action is needed to refresh the view.

Q4: Can I include aggregated data in a view?
A4: Yes, you can include aggregated data using functions like SUM(), COUNT(), AVG(), etc., in a view.

Q5: Are there any limitations to using views?
A5: Limitations may vary by database system. Common limitations include restrictions on updating views directly and potential performance impacts for complex views.