Concept Flow - ORDER BY single column
Start Query
Fetch Rows
Sort Rows by Column
Return Sorted Rows
End Query
The query fetches rows from a table, sorts them by one column, then returns the sorted rows.
SELECT name, age FROM people ORDER BY age;
| Step | Action | Rows Before | Sort Column | Rows After | Notes |
|---|---|---|---|---|---|
| 1 | Start query execution | N/A | N/A | N/A | Query begins |
| 2 | Fetch all rows from people | [{"name":"Alice","age":30},{"name":"Bob","age":25},{"name":"Carol","age":35}] | age | [{"name":"Alice","age":30},{"name":"Bob","age":25},{"name":"Carol","age":35}] | Rows fetched unsorted |
| 3 | Sort rows by age ascending | [{"name":"Alice","age":30},{"name":"Bob","age":25},{"name":"Carol","age":35}] | age | [{"name":"Bob","age":25},{"name":"Alice","age":30},{"name":"Carol","age":35}] | Rows sorted by age |
| 4 | Return sorted rows | [{"name":"Bob","age":25},{"name":"Alice","age":30},{"name":"Carol","age":35}] | age | [{"name":"Bob","age":25},{"name":"Alice","age":30},{"name":"Carol","age":35}] | Query ends with sorted output |
| Variable | Start | After Fetch | After Sort | Final |
|---|---|---|---|---|
| rows | N/A | [{"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 [ASC|DESC]; By default, ORDER BY sorts ascending (ASC). It sorts the query result rows by the specified column. Does not change table data, only output order. Useful to organize results for reading or further processing.
ORDER BY do when used with a single column?ORDER BY clause is used to arrange the rows returned by a query in a specific order based on one or more columns.Employees by the column LastName in ascending order?ORDER BY column_name. To specify ascending order, use ASC (which is also default).ORDER BY LastName ASC. SELECT * FROM Employees SORT BY LastName; uses invalid keyword SORT BY. SELECT * FROM Employees ORDER LastName; misses BY. SELECT * FROM Employees ORDER BY LastName DESC; sorts descending, not ascending.Products with columns ProductID and Price, what will be the order of ProductID after running this query?SELECT ProductID FROM Products ORDER BY Price DESC;
ProductID: 1, 2, 3Price: 10, 30, 20Price in descending order, so highest price first.SELECT Name FROM Customers ORDER LastName;
BY after ORDER. The query uses ORDER LastName which is invalid.SELECT statement is valid, and Name is assumed to exist. ORDER BY can be used with SELECT. Only one column is needed for ORDER BY.Students with columns StudentID, Name, and Score. You want to list all students ordered by their Score from highest to lowest, but if two students have the same score, order them alphabetically by Name. Which query correctly achieves this?Score descending (highest first). Secondary sort for ties is by Name ascending (alphabetical).Score DESC then Name ASC, matching requirements. 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, wrong directions. SELECT * FROM Students ORDER BY Score DESC; only sorts by Score DESC, no tie-breaker.Pass the test and this pattern moves unknown