ASC and DESC direction in MySQL - Time & Space Complexity
When we sort data in a database using ASC (ascending) or DESC (descending), it takes time to arrange the rows.
We want to understand how the time needed grows as the amount of data grows.
Analyze the time complexity of the following SQL query that sorts data.
SELECT *
FROM employees
ORDER BY salary ASC;
This query fetches all employees and sorts them by their salary from lowest to highest.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting the list of employees by salary.
- How many times: The sorting algorithm compares and rearranges items multiple times depending on the number of employees.
As the number of employees grows, the sorting work grows faster than just adding more rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 to 40 comparisons |
| 100 | About 700 to 800 comparisons |
| 1000 | About 10,000 to 12,000 comparisons |
Pattern observation: The work grows faster than the number of rows, roughly multiplying by n log n.
Time Complexity: O(n log n)
This means that if you double the number of rows, the sorting work grows a bit more than double, but not as much as square.
[X] Wrong: "Sorting with ASC or DESC takes the same time as just reading the rows once."
[OK] Correct: Sorting requires comparing and rearranging many rows, which takes more time than just reading them.
Understanding how sorting time grows helps you explain database performance clearly and shows you know how data size affects queries.
"What if we added an index on the salary column? How would the time complexity of sorting change?"
Practice
ASC keyword do in a MySQL ORDER BY clause?Solution
Step 1: Understand the meaning of ASC in ORDER BY
ASC stands for ascending order, which means sorting from smallest to largest or alphabetically from A to Z.Step 2: Compare with other options
DESC is for descending order, filtering and grouping are different SQL operations unrelated to ASC.Final Answer:
Sorts the data from smallest to largest or A to Z -> Option DQuick Check:
ASC = ascending order = smallest to largest [OK]
- Confusing ASC with DESC
- Thinking ASC filters data
- Mixing sorting with grouping
employees by the salary column in descending order?Solution
Step 1: Identify correct ORDER BY syntax
The correct syntax to sort results is using ORDER BY followed by the column name and then ASC or DESC.Step 2: Check each option
SELECT * FROM employees ORDER BY salary ASC; uses ASC, not DESC. SELECT * FROM employees ORDER salary DESC; misses BY keyword. SELECT * FROM employees SORT BY salary DESC; uses SORT BY which is invalid in MySQL. SELECT * FROM employees ORDER BY salary DESC; is correct.Final Answer:
SELECT * FROM employees ORDER BY salary DESC; -> Option AQuick Check:
ORDER BY + column + DESC is correct syntax [OK]
- Omitting BY after ORDER
- Using SORT BY instead of ORDER BY
- Mixing ASC and DESC keywords
products with column price, what will be the order of prices returned by this query?SELECT price FROM products ORDER BY price DESC;
Solution
Step 1: Analyze ORDER BY with DESC
ORDER BY price DESC sorts the prices from the largest value to the smallest value.Step 2: Eliminate other options
Prices sorted from lowest to highest is ascending order, Prices sorted randomly is incorrect because ORDER BY sorts results, Prices grouped by price value is grouping which is unrelated.Final Answer:
Prices sorted from highest to lowest -> Option CQuick Check:
DESC means descending order = highest to lowest [OK]
- Confusing DESC with ASC
- Expecting random order without ORDER BY
- Thinking ORDER BY groups data
students by age in descending order:SELECT * FROM students ORDER age DESC;
Solution
Step 1: Check ORDER BY syntax
The correct syntax requires ORDER BY, not just ORDER, before the column name.Step 2: Validate other parts
DESC is valid with ORDER BY. SORT BY is not valid in MySQL. Table name is valid as given.Final Answer:
Missing BY keyword after ORDER -> Option BQuick Check:
ORDER BY is required, not just ORDER [OK]
- Omitting BY after ORDER
- Using SORT BY instead of ORDER BY
- Misunderstanding DESC usage
orders sorted first by order_date ascending, then by total_amount descending. Which query correctly achieves this?Solution
Step 1: Understand multi-column ORDER BY
ORDER BY can sort by multiple columns in sequence: first column sorts first, then second column breaks ties.Step 2: Match sorting directions
We want order_date ascending first, then total_amount descending. SELECT * FROM orders ORDER BY order_date ASC, total_amount DESC; matches this order and directions exactly.Final Answer:
SELECT * FROM orders ORDER BY order_date ASC, total_amount DESC; -> Option AQuick Check:
First ASC on order_date, then DESC on total_amount [OK]
- Swapping column order in ORDER BY
- Mixing ASC and DESC directions incorrectly
- Using DESC when ASC is needed and vice versa
Ready to prove it?
Pass the test and this pattern moves unknown
