0
0
SQLquery~10 mins

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

Choose your learning style9 modes available
Concept Flow - ORDER BY single column
Start Query
Fetch Rows
Sort Rows by Column
Return Sorted Rows
End Query
The query fetches rows from a table, sorts them by one column, then returns the sorted rows.
Execution Sample
SQL
SELECT name, age FROM people ORDER BY age;
This query selects names and ages from the people table and sorts the results by age in ascending order.
Execution Table
StepActionRows BeforeSort ColumnRows AfterNotes
1Start query executionN/AN/AN/AQuery begins
2Fetch all rows from people[{"name":"Alice","age":30},{"name":"Bob","age":25},{"name":"Carol","age":35}]age[{"name":"Alice","age":30},{"name":"Bob","age":25},{"name":"Carol","age":35}]Rows fetched unsorted
3Sort rows by age ascending[{"name":"Alice","age":30},{"name":"Bob","age":25},{"name":"Carol","age":35}]age[{"name":"Bob","age":25},{"name":"Alice","age":30},{"name":"Carol","age":35}]Rows sorted by age
4Return sorted rows[{"name":"Bob","age":25},{"name":"Alice","age":30},{"name":"Carol","age":35}]age[{"name":"Bob","age":25},{"name":"Alice","age":30},{"name":"Carol","age":35}]Query ends with sorted output
💡 All rows sorted by age ascending, query completes.
Variable Tracker
VariableStartAfter FetchAfter SortFinal
rowsN/A[{"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 do the rows change order only after the sorting step?
Because initially rows are fetched in the order stored in the table (see step 2). The ORDER BY clause sorts them afterward (step 3), changing their order before returning.
Does ORDER BY change the data in the table?
No, ORDER BY only changes the order of rows in the query result, not the actual data stored in the table. This is shown by rows before and after sorting in the execution table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the order of rows after step 3?
A[{"name":"Carol","age":35},{"name":"Alice","age":30},{"name":"Bob","age":25}]
B[{"name":"Alice","age":30},{"name":"Bob","age":25},{"name":"Carol","age":35}]
C[{"name":"Bob","age":25},{"name":"Alice","age":30},{"name":"Carol","age":35}]
D[{"name":"Alice","age":30},{"name":"Carol","age":35},{"name":"Bob","age":25}]
💡 Hint
Check the 'Rows After' column in step 3 of the execution table.
At which step does the query actually return the sorted rows?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the 'Action' column describing when rows are returned.
If the ORDER BY clause was removed, how would the 'Rows After' in step 3 change?
ARows would be sorted by age ascending
BRows would remain unsorted as in step 2
CRows would be sorted by age descending
DRows would be empty
💡 Hint
Without ORDER BY, rows keep their original order after fetching.
Concept Snapshot
ORDER BY single column syntax:
SELECT columns FROM table ORDER BY column_name [ASC|DESC];
By default, ORDER BY sorts ascending (ASC).
It sorts the query result rows by the specified column.
Does not change table data, only output order.
Useful to organize results for reading or further processing.
Full Transcript
This visual execution shows how an SQL query with ORDER BY on a single column works. First, the query starts and fetches all rows from the table unsorted. Then, it sorts these rows by the specified column in ascending order. Finally, it returns the sorted rows as the query result. The variable 'rows' changes from unsorted to sorted after the sorting step. ORDER BY affects only the output order, not the stored data. This step-by-step trace helps beginners see exactly when and how sorting happens in the query execution.