Lesson 7 of 15 · Lab
Sorting data by one or more columns
Sorting puts rows in a chosen order without changing any value. To sort by several columns, list them in priority order, and each later column breaks ties in the one before. Use it to read a table, to rank and to prepare for any step that depends on position.
Sort the amounts A 20, B 10 and C 20 from largest down, and A and C tie. Add the name as a second key, and A comes first on every run.
Your rows here are 18 clean invoice rows from fictional Vandelay Coffee Supply, in the order the files happened to arrive.
If you sort newest export first, do rows and total in the last block change?
No. Sorting can't add, remove or alter a row. What changes is which row a later step meets first.
Put the latest export first
- Sort, selected in this live flow in Strata, Oplema's browser app for data prep, sorts by
exported_onAscending andinvoice_idAscending. The preview starts with the 2026-09-01 export. Scroll the preview to INV-1008, which showspending. - In the first key, change Ascending to Descending.
- The preview should now start with the later export, and INV-1008 is at the top with a different status.
- Select Summarize, the last block. It holds the same row count and total as before: nothing moved but the order.
Swipe sideways to see the canvas, or open this page on a larger screen to work with it.
Reading the result
The first row is INV-1008, exported 2026-10-01, booked. rows is 18 and total is 15,850 in both directions.
Row order is an input to later steps
Without a stated order, "first" means whatever the file did. Writing the order down makes it a rule you can defend, here that a later export knows more than an earlier one. Removing duplicates relies on it: this order decides which copy of an invoice survives.
The second key breaks ties. All eight rows from one export share a date, so invoice_id gives them a repeatable order. See the Sort reference.
Common sort mistakes
- Sorting dates as text. 30/06/2026 lands after 29/08/2026, because the comparison starts at the day. Parse dates first.
- Sorting numbers as text. 10 lands before 9, for the same reason.
- Leaving ties open. Equal rows can swap between runs, so add a key that's unique.
- Sorting too early. Later steps may not keep the order, so sort right before the step that needs it.
ORDER BY and Excel's Sort
SELECT invoice_id, exported_on
FROM invoices
ORDER BY exported_on DESC, invoice_id ASCA SQL result has no guaranteed order without ORDER BY, and databases differ on whether nulls come first or last. In Excel, use Data > Sort and Add Level for each key, or SORTBY in a formula. Include the whole table in the sort, because sorting one column alone detaches its values from their rows.