Concept Flow - ORDER BY single column
Start Query
Fetch Rows from Table
Sort Rows by Column
Return Sorted Rows
End Query
The query fetches all rows, sorts them by one column, then returns the sorted list.
SELECT name, age FROM users ORDER BY age;
| Step | Action | Data State | Result |
|---|---|---|---|
| 1 | Fetch all rows from users | [{name: 'Alice', age: 30}, {name: 'Bob', age: 25}, {name: 'Carol', age: 35}] | All rows fetched unsorted |
| 2 | Sort rows by age ascending | [{name: 'Bob', age: 25}, {name: 'Alice', age: 30}, {name: 'Carol', age: 35}] | Rows sorted by age |
| 3 | Return sorted rows | [{name: 'Bob', age: 25}, {name: 'Alice', age: 30}, {name: 'Carol', age: 35}] | Query result returned |
| 4 | End query | N/A | Execution complete |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| rows | empty | [{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}] |
ORDER BY single column: - Syntax: SELECT columns FROM table ORDER BY column_name; - Sorts rows by one column in ascending order by default. - Use DESC for descending order. - Returns all rows sorted by that column. - Sorting happens after fetching rows.
ORDER BY clause do in a MySQL query?ORDER BY clause is used to sort the results of a query based on one or more columns.WHERE, grouping by GROUP BY, and limiting rows by LIMIT. So, only sorting matches ORDER BY.employees by the column salary in descending order?ORDER BY column_name ASC|DESC. Here, DESC means descending order.products with columns id and price, what will be the order of id after running this query?SELECT id FROM products ORDER BY price ASC;
id: 1, price: 50id: 2, price: 20id: 3, price: 30price in ascending order (lowest to highest).id order is [2, 3, 1].SELECT name FROM customers ORDER salary DESC;
BY after ORDER. The query uses ORDER salary DESC which is invalid.DESC is valid with ORDER BY. No WHERE clause is needed for sorting. ORDER BY comes after SELECT.students with columns name and score. You want to list all students sorted by their score from highest to lowest, but if two students have the same score, sort those students alphabetically by name ascending. Which query achieves this?score descending (highest first). Secondary sort for ties is by name ascending (alphabetical).score DESC then name ASC, matching the requirement. SELECT * FROM students ORDER BY name ASC, score DESC; reverses order priorities. SELECT * FROM students ORDER BY score ASC, name DESC; sorts score ascending and name descending, which is wrong. SELECT * FROM students ORDER BY score DESC; misses secondary sort.Pass the test and this pattern moves unknown