Why ordering organizes results in MySQL - Performance Analysis
When we ask a database to sort data, it takes some time to arrange the rows in order.
We want to understand how the time needed grows as the amount of data grows.
Analyze the time complexity of the following code snippet.
SELECT *
FROM employees
ORDER BY last_name ASC;
This query fetches all employees and sorts them by their last name in alphabetical order.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting the list of employee rows by last_name.
- How many times: The sorting process compares and rearranges rows multiple times depending on the number of rows.
As the number of employees grows, the sorting work grows faster than just counting rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 comparisons and swaps |
| 100 | About 700 comparisons and swaps |
| 1000 | About 10,000 comparisons and swaps |
Pattern observation: The work grows faster than the number of rows, roughly multiplying by a bit more than n each time.
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 fast as squaring.
[X] Wrong: "Sorting takes the same time no matter how many rows there are."
[OK] Correct: Sorting needs to compare and arrange rows, so more rows mean more work, not a fixed time.
Understanding how sorting time grows helps you explain how databases handle ordering efficiently, a useful skill in many real tasks.
"What if we added an index on last_name? How would the time complexity of ordering change?"
Practice
ORDER BY clause do in a MySQL query?Solution
Step 1: Understand the purpose of ORDER BY
TheORDER BYclause is used to arrange the rows returned by a query in a specific order.Step 2: Identify the sorting action
It sorts the results based on one or more columns, either ascending or descending.Final Answer:
It sorts the query results based on one or more columns. -> Option BQuick Check:
ORDER BY = Sort results [OK]
- Confusing ORDER BY with WHERE filtering
- Thinking ORDER BY limits rows
- Mixing ORDER BY with GROUP BY
price?Solution
Step 1: Recall correct ORDER BY syntax
The correct syntax usesORDER BYfollowed by the column name and thenDESCfor descending order.Step 2: Check each option
Only SELECT * FROM products ORDER BY price DESC; uses the correct keywords and order:ORDER BY price DESC.Final Answer:
SELECT * FROM products ORDER BY price DESC; -> Option AQuick Check:
ORDER BY + column + DESC = correct syntax [OK]
- Using SORT BY instead of ORDER BY
- Omitting BY after ORDER
- Using DOWN instead of DESC
employees with columns id, name, and salary, what will be the order of name in the result of this query?SELECT name FROM employees ORDER BY salary ASC;
Solution
Step 1: Understand ORDER BY salary ASC
The query sorts rows by thesalarycolumn in ascending order, meaning from smallest to largest salary.Step 2: Determine the order of names
Since the query selectsnamebut orders bysalary, the names will appear sorted by their salary from lowest to highest.Final Answer:
Names sorted by salary from lowest to highest. -> Option AQuick Check:
ORDER BY salary ASC = lowest to highest [OK]
- Assuming ORDER BY sorts by selected column only
- Confusing ASC with DESC
- Thinking order is alphabetical by default
SELECT * FROM orders ORDER BY DESC order_date;
Solution
Step 1: Analyze ORDER BY syntax
The correct syntax isORDER BY column_name DESC, where DESC comes after the column name.Step 2: Identify the error in the query
The query places DESC before the column name, which is incorrect and causes a syntax error.Final Answer:
The DESC keyword is placed before the column name incorrectly. -> Option DQuick Check:
DESC must follow column name in ORDER BY [OK]
- Placing DESC before column name
- Thinking DESC is optional
- Confusing ORDER BY with WHERE clause
Solution
Step 1: Understand sorting priorities
The main sort should be bypricedescending to get highest prices first.Step 2: Add secondary sort by name ascending
For ties in price, sorting bynameascending alphabetically resolves order.Step 3: Check query syntax
SELECT * FROM products ORDER BY price DESC, name ASC; correctly usesORDER BY price DESC, name ASCwith proper commas and keywords.Final Answer:
SELECT * FROM products ORDER BY price DESC, name ASC; -> Option CQuick Check:
ORDER BY price DESC, name ASC = correct multi-level sort [OK]
- Reversing sort order priorities
- Omitting comma between columns
- Using wrong ASC/DESC for desired order
Ready to prove it?
Pass the test and this pattern moves unknown
