MySQL Views

MySQL Tutorial

MySQL Views – Complete Tutorial

In MySQL Views 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

 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.

 Why Use Views?

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

 Syntax to Create a View

 Simple View Example

Table: students

| id | name | marks | city |

Create View

Use View

  •  Acts like a table
  •  Data updates automatically

 View with Multiple Tables (JOIN)

 View with Aggregate Function

 Updating Data Through a View

  •  Possible if view is simple

Example

 Cannot update views with:

  • JOIN

  • GROUP BY

  • DISTINCT

  • Aggregate functions

 ALTER View

Used to modify an existing view.

 Drop View

 Check View Definition

 Views vs Tables (Very Important)

FeatureViewTable
Data storageNoYes
SpeedSlightly slowerFaster
SecurityHighNormal
UpdateLimitedFull
MemoryLessMore

 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...