0
0
MySQLquery~10 mins

ASC and DESC direction in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ASC and DESC direction
Start with unsorted data
Apply ORDER BY clause
Check sorting direction
Sort data low->high
Return sorted result
Data is sorted by a column using ORDER BY, then direction ASC sorts from low to high, DESC sorts from high to low.
Execution Sample
MySQL
SELECT name, age FROM people ORDER BY age ASC;
SELECT name, age FROM people ORDER BY age DESC;
These queries sort people by age in ascending and descending order respectively.
Execution Table
StepQueryData Before SortSort DirectionData After Sort
1SELECT name, age FROM people ORDER BY age ASC;[{"name": "Anna", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Cara", "age": 35}]ASC[{"name": "Bob", "age": 25}, {"name": "Anna", "age": 30}, {"name": "Cara", "age": 35}]
2SELECT name, age FROM people ORDER BY age DESC;[{"name": "Anna", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Cara", "age": 35}]DESC[{"name": "Cara", "age": 35}, {"name": "Anna", "age": 30}, {"name": "Bob", "age": 25}]
3End of queriesExecution complete
💡 Queries finish after sorting data according to ASC or DESC direction.
Variable Tracker
VariableStartAfter Query 1After Query 2Final
people[{"name": "Anna", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Cara", "age": 35}][{"name": "Bob", "age": 25}, {"name": "Anna", "age": 30}, {"name": "Cara", "age": 35}][{"name": "Cara", "age": 35}, {"name": "Anna", "age": 30}, {"name": "Bob", "age": 25}]No change to original data
Key Moments - 3 Insights
Why does ASC sort put smaller numbers first?
ASC means ascending order, so the database arranges values from smallest to largest as shown in execution_table row 1.
Does DESC change the original data order permanently?
No, DESC only changes the order of the result returned by the query, original data stays the same as shown in variable_tracker.
What happens if ORDER BY is omitted?
Without ORDER BY, the database returns data in any order, not guaranteed sorted, unlike rows 1 and 2 where sorting direction is applied.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 1, what is the first person in the sorted list for ASC?
ABob
BAnna
CCara
DCannot tell
💡 Hint
Check the 'Data After Sort' column in row 1 for ASC sorting result.
At which step does the data get sorted from highest to lowest age?
AStep 3
BStep 1
CStep 2
DNone
💡 Hint
Look at the 'Sort Direction' column in execution_table row 2.
If we remove ORDER BY, how would the variable_tracker change?
AData after queries would be sorted ascending
BData order would remain as original
CData after queries would be sorted descending
DData would be empty
💡 Hint
Without ORDER BY, the original data order stays unchanged as shown in variable_tracker.
Concept Snapshot
ORDER BY sorts query results by a column.
ASC means ascending order (smallest to largest).
DESC means descending order (largest to smallest).
Without ORDER BY, data order is not guaranteed.
Sorting affects only query output, not stored data.
Full Transcript
This lesson shows how the ORDER BY clause sorts data in SQL queries. When you use ORDER BY with ASC, the database arranges the results from smallest to largest value in the chosen column. Using DESC sorts from largest to smallest. The original data in the table does not change; only the order of the returned results changes. If you do not use ORDER BY, the database may return data in any order. The execution table shows two queries sorting a list of people by age ascending and descending. The variable tracker shows how the data order changes only in the query result, not the original data. Key moments clarify common confusions about sorting direction and data permanence. The quiz tests understanding of sorting results and effects of ORDER BY.