Lesson 53 of 60 – MySQL Views
88%

MySQL Views

A View in MySQL is a virtual table based on the result of a SQL query. A view does not normally store a separate copy of the underlying table data. Instead, it stores the query definition and presents the query result like a table.

Note: Views are useful for simplifying complex queries, hiding unnecessary columns, creating reusable reports, and controlling which data users can access.

1. What is a View?

A View is a named SQL query that can be accessed like a table.

CREATE VIEW student_view AS
SELECT
    name,
    city
FROM students;

After creating the view, you can query it using SELECT:

SELECT *
FROM student_view;

2. Why Use Views?

Views can make database applications easier to manage.

  • Simplify complex SQL queries.
  • Reuse frequently used queries.
  • Hide unnecessary columns.
  • Provide a convenient reporting layer.
  • Restrict access to selected data through appropriate privileges.
  • Make application queries easier to read.

3. Basic CREATE VIEW Syntax

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

Example:

CREATE VIEW active_students AS
SELECT
    student_id,
    name,
    city
FROM students
WHERE status = 'Active';

4. Creating a Simple View

Suppose the students table contains many columns, but an application only needs student name and city.

CREATE VIEW student_basic_info AS
SELECT
    name,
    city
FROM students;

Now you can query:

SELECT *
FROM student_basic_info;

The view provides a simpler interface to the underlying query.

5. View with WHERE

A View can contain filtering conditions.

CREATE VIEW active_students AS
SELECT
    student_id,
    name,
    course_id
FROM students
WHERE status = 'Active';

Query the view:

SELECT *
FROM active_students;

Only rows satisfying the view's WHERE condition are returned.

6. Selecting Data from a View

After creating a View, you can use SELECT just like you would with a table.

SELECT
    student_id,
    name
FROM active_students;

You can also filter the view result:

SELECT
    student_id,
    name
FROM active_students
WHERE city = 'Patna';

7. View with JOIN

Views can be based on JOIN queries.

CREATE VIEW student_course_view AS
SELECT
    s.student_id,
    s.name,
    c.course_name,
    c.fee
FROM students s
INNER JOIN courses c
ON s.course_id = c.id;

Now:

SELECT *
FROM student_course_view;

can display student and course information together.

8. View with Multiple Tables

A View can combine information from several related tables.

CREATE VIEW student_payment_view AS
SELECT
    s.student_id,
    s.name,
    c.course_name,
    p.amount,
    p.payment_date
FROM students s
INNER JOIN courses c
ON s.course_id = c.id
INNER JOIN payments p
ON s.student_id = p.student_id;

This creates a reusable student payment report.

9. View with Calculated Columns

A View can contain calculated expressions.

CREATE VIEW student_due_view AS
SELECT
    s.student_id,
    s.name,
    c.course_name,
    c.fee,
    s.paid_fee,
    c.fee - s.paid_fee AS due_amount
FROM students s
INNER JOIN courses c
ON s.course_id = c.id;

The due amount is calculated when the view query is evaluated.

10. View with Aggregate Functions

Views can be created from aggregate queries.

CREATE VIEW course_student_count AS
SELECT
    c.course_name,
    COUNT(s.student_id) AS total_students
FROM courses c
LEFT JOIN students s
ON c.id = s.course_id
GROUP BY c.id, c.course_name;

Query the view:

SELECT *
FROM course_student_count;

11. View with GROUP BY

CREATE VIEW course_fee_report AS
SELECT
    course_id,
    COUNT(*) AS total_students,
    AVG(fee) AS average_fee
FROM students
GROUP BY course_id;

This View stores the definition of a grouped query.

12. View with HAVING

HAVING can be included in the query used to create a View.

CREATE VIEW popular_courses AS
SELECT
    c.course_name,
    COUNT(s.student_id) AS total_students
FROM courses c
INNER JOIN students s
ON c.id = s.course_id
GROUP BY c.id, c.course_name
HAVING COUNT(s.student_id) > 5;

The view contains courses having more than five matching students.

13. SHOW CREATE VIEW

You can inspect the SQL definition of a View using:

SHOW CREATE VIEW student_course_view;

This displays the SQL statement used to define the View.

Tip: SHOW CREATE VIEW is useful when you need to understand or document an existing View.

14. SHOW FULL TABLES

You can use SHOW FULL TABLES to identify tables and views in a database.

SHOW FULL TABLES;

You can also filter the result to views:

SHOW FULL TABLES
WHERE Table_type = 'VIEW';

15. CREATE OR REPLACE VIEW

If a View already exists, you can replace its definition using:

CREATE OR REPLACE VIEW active_students AS
SELECT
    student_id,
    name,
    city,
    course_id
FROM students
WHERE status = 'Active';

This changes the View definition without first dropping it.

16. ALTER VIEW

MySQL also supports ALTER VIEW for changing a View definition.

ALTER VIEW active_students AS
SELECT
    student_id,
    name,
    city
FROM students
WHERE status = 'Active';

CREATE OR REPLACE VIEW is often convenient when you want to replace the existing definition.

17. DROP VIEW

To remove a View, use DROP VIEW.

DROP VIEW student_basic_info;

The View is removed, but dropping a View does not drop the underlying base tables.

18. DROP VIEW IF EXISTS

To avoid an error when the View may not exist, use:

DROP VIEW IF EXISTS student_basic_info;

This is useful in scripts and deployment operations.

19. View vs Table

Table View
Stores table data Stores a query definition
Has its own rows and columns Presents the result of its underlying query
Can be directly populated with INSERT May or may not be updatable depending on its definition
Usually represents stored data Usually provides a virtual representation of data

20. View vs Temporary Table

View Temporary Table
Stores a query definition Stores temporary result data
Can be referenced by multiple sessions according to normal object privileges Visible only within the session that created it
Normally remains until explicitly dropped Automatically disappears when the session ends

21. Can We INSERT into a View?

Some Views are updatable, meaning INSERT, UPDATE, or DELETE operations may be possible through them.

However, not every View is updatable. Views containing features such as certain joins, aggregate functions, GROUP BY, DISTINCT, or other constructs may not be directly updatable.

CREATE VIEW simple_students AS
SELECT
    student_id,
    name,
    city
FROM students;

A simple View like this may be updatable, depending on its definition and other MySQL rules.

22. Updating Data Through a View

If a View is updatable, changes made through the View can affect the underlying table.

UPDATE simple_students
SET city = 'Patna'
WHERE student_id = 1;

The underlying students table can be updated through the View when the View is updatable.

Important: Do not assume every View can be updated. Check the View definition and MySQL's updatability rules first.

23. View with Security and Selected Columns

A View can expose only selected columns instead of giving users direct access to every column in a base table.

CREATE VIEW public_student_info AS
SELECT
    student_id,
    name,
    city
FROM students;

For example, sensitive columns such as internal notes or other restricted information can be excluded from this View.

Remember: A View alone is not a complete security system. Appropriate MySQL privileges should also be configured.

24. View with JOIN and Payment Report

CREATE VIEW payment_report AS
SELECT
    s.student_id,
    s.name,
    c.course_name,
    p.amount,
    p.payment_date
FROM students s
INNER JOIN courses c
ON s.course_id = c.id
INNER JOIN payments p
ON s.student_id = p.student_id;

Now the application can simply use:

SELECT *
FROM payment_report
ORDER BY payment_date DESC;

This avoids repeating the complete JOIN query every time.

25. View with Date Filtering

A View can be created from date-based conditions.

CREATE VIEW recent_payments AS
SELECT
    student_id,
    amount,
    payment_date
FROM payments
WHERE payment_date >= '2026-01-01';

Query the View:

SELECT *
FROM recent_payments;

26. Common View Mistakes

  • Forgetting to give the View a meaningful name.
  • Creating too many unnecessary Views.
  • Using SELECT * when only a few columns are needed.
  • Assuming every View is updatable.
  • Forgetting that the underlying tables still control the actual data.
  • Dropping a base table without considering dependent Views.
  • Using very complex View definitions that become difficult to maintain.
Tip: Create Views for repeated, meaningful queries and give them clear names such as student_course_view or payment_report.

27. View Performance

A normal MySQL View is generally a stored query definition rather than a separately stored copy of its result.

When you query a View, MySQL processes the View definition together with the outer query according to its optimizer and view algorithm rules.

For this reason, a View does not automatically make a complex query faster.

Tip: For performance, also consider indexes on the underlying tables and the actual query structure.

28. View Query with ORDER BY

A View definition can contain ORDER BY in supported situations, but the final ordering of a SELECT from a View should generally be specified in the outer query when a particular output order is required.

CREATE VIEW student_names AS
SELECT
    student_id,
    name
FROM students;

Then:

SELECT *
FROM student_names
ORDER BY name ASC;

This makes the desired output order explicit.

29. Practical Student Dashboard View

A View can provide a reusable data source for an application dashboard.

CREATE VIEW student_dashboard AS
SELECT
    s.student_id,
    s.name,
    c.course_name,
    c.fee,
    s.paid_fee,
    c.fee - s.paid_fee AS due_amount,
    s.status
FROM students s
LEFT JOIN courses c
ON s.course_id = c.id;

The application can then use:

SELECT *
FROM student_dashboard
WHERE status = 'Active'
ORDER BY name;

This keeps the repeated JOIN and due calculation inside one reusable database object.

30. Complete MySQL Views Example

CREATE TABLE courses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    course_name VARCHAR(100),
    fee DECIMAL(10,2)
);

CREATE TABLE students (
    student_id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    course_id INT,
    city VARCHAR(100),
    paid_fee DECIMAL(10,2),
    status VARCHAR(20)
);

CREATE TABLE payments (
    payment_id INT AUTO_INCREMENT PRIMARY KEY,
    student_id INT,
    amount DECIMAL(10,2),
    payment_date DATE
);

INSERT INTO courses
(course_name, fee)
VALUES
('Python', 15000),
('Java', 18000),
('PHP', 12000),
('SQL', 10000);

INSERT INTO students
(name, course_id, city, paid_fee, status)
VALUES
('Amit Kumar', 1, 'Patna', 10000, 'Active'),
('Priya Singh', 2, 'Gaya', 18000, 'Active'),
('Rahul Sharma', 1, 'Patna', 7000, 'Active'),
('Neha Kumari', 3, 'Patna', 5000, 'Inactive');

INSERT INTO payments
(student_id, amount, payment_date)
VALUES
(1, 5000, '2026-01-10'),
(1, 5000, '2026-02-10'),
(2, 18000, '2026-03-15'),
(3, 7000, '2026-04-20');

-- Create a basic student View
CREATE VIEW active_students AS
SELECT
    student_id,
    name,
    city,
    course_id
FROM students
WHERE status = 'Active';

-- Create a student-course View
CREATE VIEW student_course_view AS
SELECT
    s.student_id,
    s.name,
    s.city,
    c.course_name,
    c.fee,
    s.paid_fee,
    c.fee - s.paid_fee AS due_amount
FROM students s
INNER JOIN courses c
ON s.course_id = c.id;

-- Create a payment report View
CREATE VIEW payment_report AS
SELECT
    s.student_id,
    s.name,
    c.course_name,
    p.amount,
    p.payment_date
FROM students s
INNER JOIN courses c
ON s.course_id = c.id
INNER JOIN payments p
ON s.student_id = p.student_id;

-- Create a course-wise student count View
CREATE VIEW course_student_count AS
SELECT
    c.course_name,
    COUNT(s.student_id) AS total_students
FROM courses c
LEFT JOIN students s
ON c.id = s.course_id
GROUP BY c.id, c.course_name;

-- Use the Views
SELECT *
FROM active_students;

SELECT *
FROM student_course_view
ORDER BY name;

SELECT *
FROM payment_report
ORDER BY payment_date DESC;

SELECT *
FROM course_student_count
ORDER BY total_students DESC;

-- Show View definitions
SHOW CREATE VIEW active_students;

SHOW CREATE VIEW student_course_view;

-- List Views
SHOW FULL TABLES
WHERE Table_type = 'VIEW';

-- Replace a View
CREATE OR REPLACE VIEW active_students AS
SELECT
    student_id,
    name,
    city,
    course_id,
    paid_fee
FROM students
WHERE status = 'Active';

-- Drop a View
DROP VIEW IF EXISTS payment_report;

This complete example demonstrates creating Views, querying Views, JOIN-based Views, aggregate Views, replacing a View, inspecting View definitions, listing Views, and dropping a View.

📌 Key Points

  • A View is a virtual table based on a SQL query.
  • CREATE VIEW is used to create a View.
  • SELECT can be used to retrieve data from a View.
  • Views can contain WHERE, JOIN, GROUP BY, HAVING, and calculated expressions.
  • CREATE OR REPLACE VIEW can replace an existing View definition.
  • ALTER VIEW can modify a View definition.
  • DROP VIEW removes a View without dropping its underlying base tables.
  • SHOW CREATE VIEW displays the View definition.
  • Views can simplify complex and frequently used queries.
  • Views can expose selected columns instead of all base-table columns.
  • Not every View is updatable.
  • A normal View does not automatically make a query faster.
  • Indexes on underlying tables can still be important for performance.
  • Views are commonly useful for reporting and application data-access layers.

🧠 Quick Quiz

Question: What is a MySQL View?