Creating & Querying Views (CREATE VIEW)
Creating & Querying Views (CREATE VIEW)
A View in SQL is a named, stored query definition that acts as a virtual table. It does not store physical rows of data itself (unless indexed or materialized in supporting engines); instead, every time a view is queried, MySQL executes the underlying SELECT query against the base tables and dynamically renders the result set.
Views encapsulate complex SQL logic, provide reusable abstractions, enhance security by exposing only necessary columns/rows, and ensure standard metrics across an entire development team.
The Fundamental Purpose of Views
- 1Query Simplification: Hide multi-table joins, subqueries, and mathematical computations behind a simple table-like interface (
SELECT * FROM active_customer_summary). - 2Access Security & Column Whitelisting: Grant developers or analytics tools
SELECTaccess only to the view, concealing sensitive columns like passwords, social security numbers, or internal cost margins. - 3Data Integrity & Consistency: Guarantee that business logic (such as calculating
net_revenue = gross - tax - discount) is implemented identically across all backend microservices.
Creating a View Syntax
Using OR REPLACE allows you to overwrite an existing view definition without first issuing a DROP VIEW statement.
Practical Walkthrough: Sales Reporting View
Consider an online retail database with customers, orders, and order_items tables. Querying the lifetime value and latest order for every customer requires multiple joins and aggregations.
Querying Views Just Like Tables
Once created, users can filter, sort, and paginate against the view with standard SELECT statements:
Under the hood, MySQL merges the view query with the outer query using its query optimizer (via the MERGE or TEMPTABLE algorithm).
Viewing View Definitions
To inspect the underlying DDL query of a view:
Or query the system catalog in information_schema:
Multiple Choice Questions
1. What is a SQL View?
A. A physical table on disk that duplicates base table data every hour B. A saved virtual query that dynamically retrieves data from base tables upon execution C. A compiled binary procedure that only executes inside stored routines D. A temporary memory cache created exclusively for stored procedures Answer: B Explanation: A SQL view is a virtual table representing the result of a stored SELECT statement. It retrieves fresh data from underlying base tables whenever queried.
2. Which clause safely modifies an existing view definition without dropping it first?
A. ALTER OR INSERT VIEW B. UPDATE VIEW DEFINITION C. CREATE OR REPLACE VIEW D. MODIFY VIEW SCHEMA Answer: C Explanation: The CREATE OR REPLACE VIEW syntax overwrites an existing view definition or creates a new one if it does not already exist.
3. How do views enhance database security?
A. By automatically encrypting table storage on disk using AES-256 B. By exposing a whitelist of safe columns and rows while hiding sensitive columns like passwords or SSNs C. By restricting network ports on the MySQL host operating system D. By disabling all foreign key constraints on the base tables Answer: B Explanation: Views allow database administrators to grant users access to specific views containing safe columns while revoking direct access to the underlying sensitive base tables.
4. When querying a view, what standard SQL clauses can be applied?
A. Only WHERE, without ORDER BY or LIMIT B. Only SELECT , without column projection C. Any valid SQL clauses including WHERE, ORDER BY, GROUP BY, and LIMIT D. None, because view queries are immutable and cannot take outer filters Answer: C Explanation:* Views behave like virtual tables; callers can project specific columns, attach WHERE filters, join views with other tables, and apply sorting or pagination.
5. Where does MySQL store the metadata definition of all created views?
A. In the local mysql.log text file B. In the performance_schema.events_waits_current table C. In information_schema.views and the data dictionary D. In client-side MySQL Workbench configuration files Answer: C Explanation: MySQL records all view definitions, check options, and updatability metadata within the information_schema.views dictionary table.
Updatable Views & WITH CHECK OPTION
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Denormalization Strategies & Performance Trade-offs | Updatable Views & WITH CHECK OPTION |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.