Concept Flow - Why ordering matters
Start Query
Retrieve Rows
Apply ORDER BY
Sort Rows by Column(s)
Return Sorted Result Set
End Query
The query retrieves rows, then sorts them by specified columns before returning the ordered result.
SELECT name, age FROM people ORDER BY age ASC;
| Step | Action | Data State | Result |
|---|---|---|---|
| 1 | Start query execution | Table 'people' with unsorted rows | No output yet |
| 2 | Retrieve all rows | Rows: (Alice, 30), (Bob, 25), (Carol, 35) | Rows fetched unsorted |
| 3 | Apply ORDER BY age ASC | Sorting rows by age ascending | Rows reordered |
| 4 | Return sorted rows | Rows: (Bob, 25), (Alice, 30), (Carol, 35) | Output result set |
| 5 | End query | Result set delivered | Query complete |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| rows | empty | (Alice,30),(Bob,25),(Carol,35) | (Bob,25),(Alice,30),(Carol,35) | (Bob,25),(Alice,30),(Carol,35) |
ORDER BY sorts query results by specified columns. Without ORDER BY, row order is not guaranteed. Syntax: SELECT columns FROM table ORDER BY column ASC|DESC; Ordering affects how results are presented. Use ASC for ascending, DESC for descending order.