0
0
SQLquery~10 mins

CREATE VIEW syntax in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - CREATE VIEW syntax
Write SELECT query
Use CREATE VIEW name AS
Database stores view definition
Querying view runs stored SELECT
Return result set as if table
You write a SELECT query, then create a view with that query. The database stores it. When you query the view, it runs the stored SELECT and returns results like a table.
Execution Sample
SQL
CREATE VIEW active_users AS
SELECT id, name FROM users WHERE active = 1;
This creates a view named active_users that shows only users who are active.
Execution Table
StepActionQuery/CommandResult/State
1Write SELECT querySELECT id, name FROM users WHERE active = 1Query ready to define view
2Create view with queryCREATE VIEW active_users AS SELECT id, name FROM users WHERE active = 1View 'active_users' stored in database
3Query the viewSELECT * FROM active_usersRuns stored SELECT, returns active users
4View resultid | nameRows of active users from users table
5ExitN/AView acts like a virtual table
💡 View creation ends after storing the SELECT query; querying the view runs that query.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
View DefinitionNoneSELECT id, name FROM users WHERE active = 1Same stored querySame stored query
Query ResultNoneNoneRows where active=1Rows where active=1
Key Moments - 2 Insights
Why does the CREATE VIEW command not return rows immediately?
Because CREATE VIEW only stores the SELECT query as a definition (see execution_table step 2). It does not run the query until you select from the view (step 3).
Can you update data directly in a view?
Usually no, because a view is a virtual table showing results of a SELECT (execution_table step 4). You update the underlying tables instead.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 2?
AThe view runs the SELECT query and returns rows
BThe view definition is stored in the database
CThe view is deleted
DThe underlying table is modified
💡 Hint
Check execution_table row with Step 2 describing storing the view definition
At which step does querying the view actually run the stored SELECT query?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at execution_table step 3 where the view is queried
If the SELECT query inside the view changes, what must you do to update the view?
ARun CREATE VIEW again with new query
BJust query the view again
CUpdate the underlying table only
DDrop the view and do nothing
💡 Hint
View stores the SELECT query at creation (see variable_tracker 'View Definition')
Concept Snapshot
CREATE VIEW view_name AS SELECT ...;
- Defines a virtual table storing a SELECT query
- Does not run query until view is queried
- Querying view runs stored SELECT and returns results
- Views simplify complex queries and reuse
- Views do not store data themselves
Full Transcript
The CREATE VIEW syntax lets you save a SELECT query as a virtual table called a view. First, you write the SELECT query you want. Then you use CREATE VIEW view_name AS followed by that query. The database stores this query as the view's definition. When you later query the view, the database runs the stored SELECT query and returns the results as if it were a table. The view itself does not store data, it just shows data from the underlying tables. This helps reuse queries and simplify complex data access. You cannot update data directly in a view; you update the original tables instead. To change the view's query, you recreate the view with a new SELECT statement.