0
0
SQLquery~10 mins

Querying through views in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Querying through views
Create View
Store View Definition
Query View
Database Replaces View with Underlying Query
Execute Underlying Query
Return Result Set
A view stores a query. When you query the view, the database runs the stored query and returns its results.
Execution Sample
SQL
CREATE VIEW TopStudents AS
SELECT name, score FROM Students WHERE score > 90;

SELECT * FROM TopStudents;
Create a view named TopStudents for students scoring above 90, then query it to get those students.
Execution Table
StepActionQuery/ConditionResult/Output
1Create viewCREATE VIEW TopStudents AS SELECT name, score FROM Students WHERE score > 90View TopStudents stored with query definition
2Query viewSELECT * FROM TopStudentsDatabase replaces with underlying query
3Execute underlying querySELECT name, score FROM Students WHERE score > 90Rows with score > 90 returned
4Return resultN/A[('Alice', 95), ('Bob', 92)]
💡 Query completes after returning all rows matching score > 90
Variable Tracker
VariableStartAfter Step 2After Step 3Final
View DefinitionNoneStored as SELECT name, score FROM Students WHERE score > 90Used to replace view in queryN/A
Query ResultNoneNone[('Alice', 95), ('Bob', 92)][('Alice', 95), ('Bob', 92)]
Key Moments - 2 Insights
Why does querying a view run the underlying query instead of returning stored data?
The execution_table row 2 shows the database replaces the view with its stored query, so the data is fetched fresh each time.
Can the view store data like a table?
No, as shown in variable_tracker, the view only stores the query definition, not the data itself.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what query is executed?
ASELECT * FROM TopStudents
BCREATE VIEW TopStudents AS SELECT name, score FROM Students WHERE score > 90
CSELECT name, score FROM Students WHERE score > 90
DSELECT name, score FROM Students
💡 Hint
Check execution_table row 3 under 'Query/Condition'
According to variable_tracker, what does the view store after creation?
AThe actual student data
BThe query definition
CNothing
DThe result set
💡 Hint
Look at 'View Definition' row after Step 2 in variable_tracker
At which step does the database replace the view with its underlying query?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
See execution_table row 2 'Action' column
Concept Snapshot
CREATE VIEW view_name AS SELECT ...;
Query the view like a table: SELECT * FROM view_name;
The database runs the stored query each time you query the view.
Views do not store data, only query definitions.
Useful for simplifying complex queries and reusing logic.
Full Transcript
A view in SQL is like a saved query. When you create a view, you store a query under a name. Later, when you query the view, the database runs the stored query and returns the results. This means views do not hold data themselves, but act like virtual tables. For example, creating a view TopStudents for students scoring above 90 lets you query TopStudents to get those students easily. The execution steps show creating the view, querying it, replacing the view with its query, running that query, and returning the results. Variables track the stored query and the result set. Common confusions include thinking views store data or that querying a view returns stored data directly. The quiz checks understanding of these steps and concepts.