0
0
MySQLquery~10 mins

Querying from views in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Querying from views
Create View
Store View Definition
Run Query on View
Database Expands View to Base Tables
Execute Expanded Query
Return Result Set
A view is a saved query. When you query a view, the database runs the saved query behind the scenes and returns the results.
Execution Sample
MySQL
CREATE VIEW active_customers AS
SELECT id, name FROM customers WHERE active = 1;

SELECT * FROM active_customers;
Create a view named active_customers showing only active customers, then select all rows from that view.
Execution Table
StepActionQuery/ConditionResult/Output
1Create viewCREATE VIEW active_customers AS SELECT id, name FROM customers WHERE active = 1;View 'active_customers' saved with definition
2Query viewSELECT * FROM active_customers;Database expands to SELECT id, name FROM customers WHERE active = 1;
3Execute expanded querySELECT id, name FROM customers WHERE active = 1;Returns rows where active=1
4Return resultResult set from expanded query[{"id":1, "name":"Alice"}, {"id":3, "name":"Charlie"}]
5EndNo more stepsQuery complete
💡 Query ends after returning all rows matching the view's condition
Variable Tracker
VariableStartAfter Step 2After Step 3Final
View DefinitionNoneSELECT id, name FROM customers WHERE active = 1SameSame
QuerySELECT * FROM active_customersExpanded to SELECT id, name FROM customers WHERE active = 1ExecutedResult set returned
Result SetEmptyEmpty[{"id":1, "name":"Alice"}, {"id":3, "name":"Charlie"}]Returned to user
Key Moments - 2 Insights
Why does querying a view run a different query behind the scenes?
Because a view stores a saved query definition. When you query the view, the database replaces the view name with its saved query and runs that expanded query (see execution_table step 2).
Does the view store data itself?
No, the view only stores the query definition, not data. Data is fetched fresh from base tables each time you query the view (see variable_tracker 'View Definition' vs 'Result Set').
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what query does the database actually run when you query the view?
ASELECT * FROM active_customers
BCREATE VIEW active_customers AS SELECT id, name FROM customers WHERE active = 1
CSELECT id, name FROM customers WHERE active = 1
DSELECT id, name FROM customers
💡 Hint
Check execution_table step 2 where the view query is expanded
At which step does the database return the actual rows to the user?
AStep 4
BStep 1
CStep 3
DStep 5
💡 Hint
Look at execution_table step 4 where the result set is returned
If the base table 'customers' changes, what happens when you query the view again?
AThe view shows old data stored inside it
BThe view shows updated data from the base table
CThe view query breaks
DThe view caches data and does not update
💡 Hint
Remember views run the saved query fresh each time (see key_moments about data storage)
Concept Snapshot
CREATE VIEW view_name AS SELECT ...;
Query the view like a table: SELECT * FROM view_name;
The database runs the saved query behind the scenes.
Views do not store data, only query definitions.
Results reflect current data in base tables.
Full Transcript
A view is like a saved query. When you create a view, you tell the database to remember a query under a name. Later, when you query the view, the database replaces the view name with the saved query and runs it on the base tables. This means the view always shows current data from the base tables, not stored data. For example, creating a view active_customers that selects customers where active=1 lets you query active_customers as if it were a table. Behind the scenes, the database runs the original query and returns the matching rows. This process involves creating the view, storing its query, expanding the view query when used, executing it, and returning the results. Understanding this helps you use views to simplify complex queries and keep your database organized.