0
0
MySQLquery~10 mins

Selecting specific columns in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Selecting specific columns
Start Query
Parse SELECT clause
Identify columns requested
Access table data
Extract only requested columns
Return result set with selected columns
End Query
The database reads the SELECT statement, finds which columns to get, fetches only those columns from the table, and returns them as the result.
Execution Sample
MySQL
SELECT name, age FROM users;
This query fetches only the 'name' and 'age' columns from the 'users' table.
Execution Table
StepActionEvaluationResult
1Start query executionQuery: SELECT name, age FROM users;Ready to parse
2Parse SELECT clauseColumns requested: name, ageColumns identified
3Access table 'users'Table has columns: id, name, age, emailTable accessed
4Extract columnsOnly 'name' and 'age' selectedData subset prepared
5Return result setRows with only 'name' and 'age'[('Alice', 30), ('Bob', 25), ('Carol', 27)]
6End queryAll requested data returnedQuery complete
💡 Query ends after returning only the selected columns from all rows.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
requested_columnsNone['name', 'age']['name', 'age']['name', 'age']
table_columnsNoneNone['id', 'name', 'age', 'email']['id', 'name', 'age', 'email']
result_setNoneNonePartial rows with selected columns[('Alice', 30), ('Bob', 25), ('Carol', 27)]
Key Moments - 2 Insights
Why does the result only show 'name' and 'age' columns, not all columns?
Because in Step 2 and 4 of the execution_table, the query specifically identifies and extracts only the 'name' and 'age' columns, ignoring others.
What happens if you select a column that does not exist in the table?
The query would fail during Step 2 or 3 when parsing or accessing the table, because the requested column is not found in the table_columns list.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what columns are identified in Step 2?
A['name', 'age']
B['email']
C['id', 'name', 'age']
DAll columns
💡 Hint
Check the 'Evaluation' column in Step 2 of the execution_table.
At which step does the query prepare the data subset with only selected columns?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look for the step where 'Extract columns' action happens in the execution_table.
If you add 'email' to the SELECT clause, how would the 'requested_columns' variable change after Step 2?
A['name', 'age']
B['name', 'age', 'email']
C['email']
DNo change
💡 Hint
Refer to variable_tracker 'requested_columns' after Step 2 and imagine adding 'email'.
Concept Snapshot
SELECT column1, column2 FROM table_name;
- Retrieves only specified columns from the table.
- Reduces data returned, improving efficiency.
- Columns must exist in the table.
- Result set includes all rows but only chosen columns.
Full Transcript
This visual execution trace shows how a SQL query selecting specific columns works. The database starts by reading the query, then parses the SELECT clause to find which columns are requested. It accesses the table and identifies all columns available. Then it extracts only the requested columns from each row. Finally, it returns the result set containing only those columns for all rows. Variables like requested_columns and result_set change as the query progresses. Key moments include understanding why only selected columns appear and what happens if a column does not exist. The quiz tests knowledge of steps where columns are identified and extracted, and how variables change if the query changes.