A MySQL VIEW is a virtual table that consists of a stored query. It allows you to represent the result of a SELECT query as a table, simplifying complex queries and presenting data in a more understandable form. Views are useful for hiding the complexity of the underlying data structure while providing a simple interface for querying.
The CREATE VIEW statement stores a SELECT query under a name. Once created, it can be queried like a normal table.
-- General syntax for creating a view
CREATE VIEW view_name AS
SELECT column1, column2
FROM table_name
WHERE condition;
-- Creating a view for Sales department employees
CREATE VIEW employee_view AS
SELECT first_name, last_name, email
FROM employees
WHERE department = 'Sales';
-- Querying data from a view like a table
SELECT * FROM employee_view;
-- Updating underlying table data through a simple view
UPDATE employee_view
SET email = 'new_email@example.com'
WHERE first_name = 'John';
-- Removing an existing view
DROP VIEW employee_view;
Views always reflect the current data in the underlying tables. When the base table changes, the view output changes automatically.
Think of a view as a saved SQL filter. Instead of writing complex joins repeatedly, you query a clean, reusable virtual table.