MySQL Views

👁️ MySQL Views – Complete Tutorial

A View in MySQL is a virtual table based on the result of an SQL query.
It does not store data physically; it stores only the query definition.

👉 Views are mainly used for security, simplicity, and reusability.

MySQL


1️⃣ What is a View in MySQL?

A View:

  • Is created using a SELECT query

  • Looks like a table

  • Always shows latest data from base tables

📌 Think of a view as a saved query.


2️⃣ Why Use Views?

✔ Hide complex SQL queries
✔ Improve security (show limited columns)
✔ Reuse SQL logic
✔ Simplify reporting queries
✔ Improve readability


3️⃣ Syntax to Create a View



 


4️⃣ Simple View Example

Table: students

| id | name | marks | city |

Create View



 

Use View

SELECT * FROM passed_students;

✔ Acts like a table
✔ Data updates automatically


5️⃣ View with Multiple Tables (JOIN)



 


6️⃣ View with Aggregate Function



 


7️⃣ Updating Data Through a View

✔ Possible if view is simple

Example

UPDATE passed_students
SET marks = 60
WHERE id = 3;

❌ Cannot update views with:

  • JOIN

  • GROUP BY

  • DISTINCT

  • Aggregate functions


8️⃣ ALTER View

Used to modify an existing view.



 


9️⃣ Drop View

DROP VIEW passed_students;

🔟 Check View Definition

SHOW CREATE VIEW passed_students;

1️⃣1️⃣ Views vs Tables ⭐ (Very Important)

Feature View Table
Data storage No Yes
Speed Slightly slower Faster
Security High Normal
Update Limited Full
Memory Less More

📌 Interview Questions & MCQs

Q1. What is a view in MySQL?

Answer:
A view is a virtual table created from a SELECT query.


Q2. Does a view store data?

A) Yes
B) No

Answer: B


Q3. Which command creates a view?

A) MAKE VIEW
B) CREATE VIEW
C) ADD VIEW
D) INSERT VIEW

Answer: B


Q4. Can we update data through a view?

A) Always
B) Never
C) Sometimes
D) Only SELECT

Answer: C


Q5. Which clause makes a view non-updatable?

A) WHERE
B) JOIN
C) GROUP BY
D) DISTINCT

Answer: C
(Also JOIN, DISTINCT, aggregates)


Q6. How to delete a view?

DROP VIEW view_name;

Q7. What is the main advantage of views?

A) Faster than tables
B) Reduce memory
C) Security & simplicity
D) Data backup

Answer: C


🔥 Exam & Interview Tips

✔ Remember View = Virtual Table
✔ Practice CREATE, ALTER, DROP VIEW
✔ Understand updatable vs non-updatable views
✔ Very common in SQL interviews


✅ Summary

  • View is a saved SELECT query

  • Does not store data

  • Used for security & simplicity

  • Can be updated only in limited cases

  • Important topic for exams & interviews

You may also like...