SQL Views


In SQL, a view is a virtual table that is constructed by querying data from one or more tables in the database. Views allow you to simplify complex queries, improve performance, and provide a level of abstraction that hides the complexity of the underlying data. Views are particularly useful for creating reusable query templates, encapsulating logic, and restricting access to sensitive data.


1. What is an SQL View?

An SQL view is essentially a stored query that can be treated as a table. It doesn’t store any data itself; instead, it stores the SQL query that defines the view. When you query a view, the SQL engine executes the underlying query and returns the result as if it were a table.

Key Features of SQL Views:

  • Virtual Table: A view behaves like a table, but it doesn’t physically store data.
  • Simplifies Complex Queries: You can encapsulate complex logic into a view and access it like a table.
  • Security: Views can be used to restrict access to specific columns or rows of data.
  • Reusable: Once created, a view can be reused in multiple queries, reducing repetition.

Basic Syntax for Creating a View:

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
  • view_name: The name of the view you want to create.
  • SELECT statement: The query that defines the data the view will return.

2. Creating and Managing SQL Views

Creating a Simple View

Let’s say you have a table called employees and you frequently need to get the names and salaries of employees who work in a particular department. Instead of writing the query every time, you can create a view for it.

CREATE VIEW employee_salaries AS
SELECT first_name, last_name, salary
FROM employees
WHERE department_id = 3;

Explanation:

  • The view employee_salaries will return the first_name, last_name, and salary of all employees who belong to department 3.

Using a View

Once a view is created, you can query it just like you would a regular table:

SELECT * FROM employee_salaries;

This query will return the list of employees in department 3 along with their salaries, based on the logic defined in the view.


3. Updating and Modifying SQL Views

SQL views can be modified or dropped if needed.

1. Modifying a View

To modify an existing view, you can use the CREATE OR REPLACE statement, which allows you to change the view without dropping it first:

CREATE OR REPLACE VIEW employee_salaries AS
SELECT first_name, last_name, salary, hire_date
FROM employees
WHERE department_id = 3;

This updated view now includes the hire_date column in the result set.

2. Dropping a View

If you no longer need a view, you can remove it using the DROP VIEW statement:

DROP VIEW employee_salaries;

4. Types of SQL Views

SQL views can be categorized based on their usage and behavior. The main types are:

1. Simple Views

A simple view is one that is created using a single table and doesn’t contain complex operations like joins, groupings, or aggregations. It directly maps to the underlying table’s data.

Example:
CREATE VIEW simple_employee_view AS
SELECT first_name, last_name, department_id
FROM employees;

2. Complex Views

A complex view is one that is created by joining multiple tables, using aggregate functions, or applying filters. These views help simplify queries that require multiple operations.

Example:
CREATE VIEW department_salary_summary AS
SELECT d.department_name, AVG(e.salary) AS average_salary
FROM employees e
JOIN departments d ON e.department_id = d.department_id
GROUP BY d.department_name;

Explanation:

  • This view summarizes the average salary for each department.

3. Updatable Views

An updatable view allows you to modify the data of the underlying tables through the view. To be updatable, the view must not contain operations such as aggregation, joins, or subqueries that would make it impossible to update the underlying data.

Example:
CREATE VIEW employee_details AS
SELECT first_name, last_name, salary
FROM employees;

You can update the data in the employees table through the employee_details view:

UPDATE employee_details
SET salary = 80000
WHERE first_name = 'Alice' AND last_name = 'Johnson';

This query will update the salary of the employee named Alice Johnson in the employees table.

4. Non-Updatable Views

A non-updatable view cannot be used to modify the data of the underlying tables. These views often contain complex logic, like joins, aggregation, or filtering, which makes it impossible to determine how changes should be propagated.

Example:
CREATE VIEW department_salary_summary AS
SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id;

This view calculates the average salary per department and cannot be used to update the employees table.


5. Advantages of Using SQL Views

1. Simplify Complex Queries

Views allow you to encapsulate complex logic and present a simplified interface to users or applications. For example, rather than having to write a complicated JOIN query repeatedly, you can create a view and reference it whenever needed.

2. Improve Security

By using views, you can control which columns or rows of a table are accessible to users. You can create a view that exposes only specific columns, keeping sensitive data hidden.

Example:
CREATE VIEW employee_public_info AS
SELECT first_name, last_name, department_id
FROM employees;

In this example, you can allow users to access only the first_name, last_name, and department_id columns through the employee_public_info view, while keeping the salary and hire_date columns hidden.

3. Enhance Performance

Views can help improve performance by allowing the database to store optimized query plans. While views don’t physically store data, some database systems can materialize views, making it faster to access pre-aggregated or pre-joined data.

4. Reusability

Once a view is created, you can reuse it in multiple queries, saving you time and effort. This also ensures consistency in your queries, as you’re reusing the same logic.


6. Limitations of SQL Views

While SQL views are extremely useful, they come with some limitations:

  • Performance: Views are not indexed, so queries involving complex views can be slower than direct queries on tables.
  • Non-Updatable Views: Views that involve complex operations like aggregation, joins, or unions may not be updatable, meaning you can’t use them to modify the underlying data.
  • Limited to Select Statements: Views can only be used to retrieve data; they cannot be used for operations like INSERT, UPDATE, or DELETE unless the view is updatable.