---
title: "Sort data by multiple columns | Oplema"
description: "Learn to sort rows by several columns, break ties, see why row order affects later steps and write ORDER BY in SQL and Sort in Excel."
url: "https://oplema.com/learn/data-preparation/sort"
updated: "2026-11-04"
---

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](https://oplema.com/learn/data-preparation/what-is-data-prep), 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

1. **Sort**, selected in this live flow in [Strata](https://oplema.com/strata), Oplema's browser app for data prep, sorts by `exported_on` **Ascending** and `invoice_id` **Ascending**. The preview starts with the 2026-09-01 export. Scroll the preview to INV-1008, which shows `pending`.
2. In the first key, change **Ascending** to **Descending**.
3. The preview should now start with the later export, and INV-1008 is at the top with a different status.
4. Select **Summarize**, the last block. It holds the same row count and total as before: nothing moved but the order.

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](https://oplema.com/learn/data-preparation/unique-and-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](https://oplema.com/docs/strata/prep/sort).

## Common sort mistakes

- **Sorting dates as text.** 30/06/2026 lands after 29/08/2026, because the comparison starts at the day. [Parse dates](https://oplema.com/learn/data-preparation/parse-text-and-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  ASC
```

A 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.
