What is creating a view in MySQL?

A view in MySQL is a virtual table that is based on the result set of a SQL query. It contains rows and columns just like a real table and can be used to simplify complex queries, encapsulate logic, and present data in a customized format without storing the data multiple times. When you query a view, MySQL executes the underlying SQL statement to produce the result set on the fly.

Creating a View

To create a view in MySQL, you use the `CREATE VIEW` statement followed by the view name and the SQL query that defines what the view will contain.

Example

CREATE VIEW EmployeeView AS SELECT name, position, salary FROM Employees WHERE department_id = 5;

In this example, we create a view named `EmployeeView` that selects the `name`, `position`, and `salary` columns from the `Employees` table for those employees who belong to department ID 5.


MySQL view CREATE VIEW SQL query virtual table Employees database