SQL View

Introduction

A view is created based on the result of a SELECT statement from one or more tables. This SELECT statement forms the definition of the view. Once the view is created, it can be queried and treated just like any other table in the database. Views can include joins, aggregate functions, filtering conditions, and more, allowing users to abstract the complexity of underlying data structures and provide a simplified interface. Enhance data security, simplify complex queries, and improve performance with views.

Syntax

To create a view in MySQL, you use the CREATE VIEW statement followed by the view name and the AS keyword, which specifies the SELECT statement that defines the view.

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

Let’s start with a simple example to understand how to create a basic view in MySQL. Suppose we have two tables in our database: “employees” and “departments,” with the following structures:

employees

emp_idemp_namedepartment_id
1John101
2Jane102
3Alex101
4Lisa103
employees

departments

department_iddepartment_name
101HR
102Finance
103Marketing
departments

We want to create a view that combines employee information with their respective department names.

CREATE VIEW employee_details AS
SELECT e.emp_id, e.emp_name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id;

In this example, we’ve created a view named “employee_details” that fetches data from both the “employees” and “departments” tables and presents it as a single virtual table.

Querying a View

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

SELECT * FROM employee_details;

This query will display the following result:

emp_idemp_namedepartment_name
1JohnHR
2JaneFinance
3AlexHR
4LisaMarketing

Types of Views in MySQL

MySQL supports different types of views based on their updateability and whether they reference a single table or multiple tables. Let’s explore the common types of views:

Simple View

A simple view is based on a single table and does not include any GROUP BY, HAVING, or aggregate functions in its definition. They are the most straightforward type of views and are generally updatable.

Example of a simple view:

CREATE VIEW simple_view AS
SELECT column1, column2
FROM table_name
[WHERE condition];

Updatable View

An updatable view allows modifications to the underlying tables through the view. The view must meet certain criteria to be updatable, such as not including aggregate functions or the DISTINCT keyword and referencing only one table in the SELECT statement. Additionally, the view’s columns must map directly to the columns of the underlying table.

Example of an updatable view:

CREATE VIEW updatable_view AS 
SELECT column1, column2 
FROM table_name 
[WHERE condition];

Complex View

A complex view involves multiple tables, joins, or aggregate functions in its definition. Complex views may or may not be updatable, depending on the complexity of the query and adherence to the updatable view criteria.

Example of a complex view:

CREATE VIEW complex_view AS
SELECT t1.column1, t2.column2
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.id
[WHERE condition];

Benefits of Views

  1. Data Abstraction: Views help abstract the complexity of underlying table structures, allowing users to interact with a simplified representation of data.
  2. Data Security: Views can act as a security layer by providing access to specific columns or rows of a table, hiding sensitive information from users.
  3. Simplified Querying: Views encapsulate complex queries, making it easier for users to retrieve data without dealing with the joins, filtering, and aggregation.
  4. Performance Optimization: Views can cache the results of queries, improving performance for frequently executed queries.
  5. Ease of Maintenance: If the underlying table schema changes, views can act as a buffer, and users don’t need to modify their queries as long as the view’s definition remains the same.

A views offer a powerful and flexible way to interact with data, providing a simplified interface to the underlying tables. Understand the select statement along with the examples.

Interview Questions

Basic View SQL Interview questions

  1. Can views be indexed in MySQL?
  2. Can you update data from multiple tables through a view?
  3. How do you drop a view in MySQL?
  4. What are the scenarios where using views is not recommended?
  5. How can you check if a view is updatable in MySQL?

Intermediate View SQL Interview questions

  1. Explain the role of the “WITH CHECK OPTION” when creating an updatable view.
  2. Can views be used to improve the performance of complex queries involving multiple tables? Provide an example.
  3. How can you modify data in a view that involves multiple tables with the “INSERT INTO” statement?

Advanced View SQL Interview questions

  1. Discuss the pros and cons of using views for security purposes.
  2. Explain the difference between a materialized view and a regular view in MySQL.

Quiz

Click on this link to solve few MCQ questions on View.

Happy offline learning… Lets meet in next session. Follow this link for more content.

Leave a Comment