0
0
MySQLquery~10 mins

ORDER BY single column in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ORDER BY single column
Start Query
Fetch Rows from Table
Sort Rows by Column
Return Sorted Rows
End Query
The query fetches all rows, sorts them by one column, then returns the sorted list.
Execution Sample
MySQL
SELECT name, age FROM users ORDER BY age;
This query selects names and ages from users and sorts the results by age in ascending order.
Execution Table
StepActionData StateResult
1Fetch all rows from users[{name: 'Alice', age: 30}, {name: 'Bob', age: 25}, {name: 'Carol', age: 35}]All rows fetched unsorted
2Sort rows by age ascending[{name: 'Bob', age: 25}, {name: 'Alice', age: 30}, {name: 'Carol', age: 35}]Rows sorted by age
3Return sorted rows[{name: 'Bob', age: 25}, {name: 'Alice', age: 30}, {name: 'Carol', age: 35}]Query result returned
4End queryN/AExecution complete
💡 All rows sorted by age ascending, query ends
Variable Tracker
VariableStartAfter Step 1After Step 2Final
rowsempty[{name: 'Alice', age: 30}, {name: 'Bob', age: 25}, {name: 'Carol', age: 35}][{name: 'Bob', age: 25}, {name: 'Alice', age: 30}, {name: 'Carol', age: 35}][{name: 'Bob', age: 25}, {name: 'Alice', age: 30}, {name: 'Carol', age: 35}]
Key Moments - 2 Insights
Why does the order of rows change after step 2?
Because the ORDER BY clause sorts the rows by the specified column (age), changing their order from the original fetch.
What if two rows have the same age? How does ORDER BY handle that?
Rows with the same age keep their relative order from the original fetch (stable sort), unless another column is specified for sorting.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the order of rows after step 2?
A[{Bob,25}, {Alice,30}, {Carol,35}]
B[{Alice,30}, {Bob,25}, {Carol,35}]
C[{Carol,35}, {Alice,30}, {Bob,25}]
D[{Bob,25}, {Carol,35}, {Alice,30}]
💡 Hint
Check the 'Data State' column in step 2 of the execution table.
At which step does the query return the sorted rows?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step where 'Return sorted rows' is the action.
If we change ORDER BY age to ORDER BY age DESC, how would the order after step 2 change?
A[{Bob,25}, {Alice,30}, {Carol,35}]
B[{Carol,35}, {Alice,30}, {Bob,25}]
C[{Alice,30}, {Bob,25}, {Carol,35}]
D[{Carol,35}, {Bob,25}, {Alice,30}]
💡 Hint
Descending order means highest age first; check the sorted list in step 2.
Concept Snapshot
ORDER BY single column:
- Syntax: SELECT columns FROM table ORDER BY column_name;
- Sorts rows by one column in ascending order by default.
- Use DESC for descending order.
- Returns all rows sorted by that column.
- Sorting happens after fetching rows.
Full Transcript
This visual execution shows how a SQL query with ORDER BY on a single column works. First, all rows are fetched from the table. Then, the rows are sorted by the specified column in ascending order by default. Finally, the sorted rows are returned as the query result. The variable 'rows' changes from unsorted to sorted after the ORDER BY step. This helps beginners see how the sorting affects the data step-by-step.