0
0
SQLquery~10 mins

ORDER BY with ASC and DESC in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ORDER BY with ASC and DESC
Start with table data
Apply ORDER BY clause
Check sorting direction
Sort ascending
Return sorted result
The ORDER BY clause sorts table rows by specified columns in ascending (ASC) or descending (DESC) order, then returns the sorted result.
Execution Sample
SQL
SELECT name, age FROM people ORDER BY age ASC;
This query sorts the people table by age in ascending order and returns the name and age columns.
Execution Table
StepActionData StateResult
1Start with unsorted data[{name: 'Alice', age: 30}, {name: 'Bob', age: 25}, {name: 'Carol', age: 35}]Data as is
2Apply ORDER BY age ASCSame dataSort rows by age ascending
3Compare ages25 < 30 < 35Order: Bob, Alice, Carol
4Return sorted resultSorted data[{name: 'Bob', age: 25}, {name: 'Alice', age: 30}, {name: 'Carol', age: 35}]
5Query endsSorted dataResult returned
💡 All rows sorted by age ascending, query completes
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Data[{name: 'Alice', age: 30}, {name: 'Bob', age: 25}, {name: 'Carol', age: 35}][{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 change after applying ORDER BY?
Because ORDER BY sorts the rows based on the specified column and direction, as shown in execution_table step 3 where ages are compared and rows reordered.
What happens if DESC is used instead of ASC?
The rows would be sorted from highest to lowest value, reversing the order seen in step 3, so Carol would come first, then Alice, then Bob.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the order of names after step 3?
ABob, Alice, Carol
BAlice, Bob, Carol
CCarol, Alice, Bob
DCarol, Bob, Alice
💡 Hint
Check the 'Result' column in step 3 of execution_table
At which step does the query return the sorted result?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'Return sorted result' action in execution_table
If ORDER BY age DESC was used, who would be the first row in the result?
AAlice
BCarol
CBob
DCannot tell
💡 Hint
Refer to variable_tracker final data and reverse the order by age
Concept Snapshot
ORDER BY sorts query results by one or more columns.
Use ASC for ascending order (smallest to largest).
Use DESC for descending order (largest to smallest).
Default is ASC if not specified.
Multiple columns can be ordered by listing them separated by commas.
Example: ORDER BY age DESC, name ASC
Full Transcript
The ORDER BY clause in SQL sorts the rows returned by a query. You specify which column to sort by and whether to sort ascending (ASC) or descending (DESC). The process starts with the original table data. Then the database compares the values in the specified column and rearranges the rows accordingly. For example, sorting by age ascending puts the youngest person first. If DESC is used, the oldest person comes first. The sorted rows are then returned as the query result. This visual trace showed step-by-step how the data changes from unsorted to sorted by age ascending.