Mastering MySQL for Data AnalystsLesson 13 of 42
Lesson 13 of 42

Sorting Data: ORDER BY, Multi-Column Sorting & NULLs Handling in MySQL

Overview & Learning Objectives

Relational database tables are inherently unordered sets; rows are returned in arbitrary order unless explicitly sorted using `ORDER BY`. Analysts sort records across multiple hierarchical columns (e.g. Category ASC, then Revenue DESC). Unlike PostgreSQL which has `NULLS FIRST / NULLS LAST` syntax, MySQL treats `NULL` as lower than any non-NULL value (sorting first in `ASC` and last in `DESC`). Analysts control NULL positioning using `ISNULL()` or custom boolean sort keys.

  • Sort query outputs using single and multi-column `ORDER BY` with `ASC` and `DESC`.
  • Understand MySQL default NULL sorting behavior (NULLs sort lowest).
  • Control NULL placement explicitly using `ISNULL(col)` or `ORDER BY -col DESC`.

Industry Standard

Standard: Analytics pipelines at Uber enforce sorting on indexed timestamp columns to enable streaming merge sorts and optimize Top-N queries.

Real-World Example: ScoopCraft Dark-Store Leaderboard

ScoopCraft ranks stores by region alphabetically, and within each region by sales revenue descending, placing unrated stores at the very bottom.

Store NameRegionRevenue (₹)Customer RatingSort Priority
Indiranagar HubSouth₹45,000.004.91st in South
Koramangala HubSouth₹38,000.004.82nd in South
Whitefield HubSouth₹29,000.00NULLLast in South (NULL pushed to bottom)
Connaught HubNorth₹52,000.004.91st in North

Syntax & Core SQL Implementation

-- Standard multi-column sorting
SELECT outlet_name, region, gross_sales, rating
FROM outlets
ORDER BY region ASC, gross_sales DESC;

-- Controlling NULL placement in MySQL (Putting NULLs last in ASC sort)
SELECT item_name, discount_pct
FROM menu_items
ORDER BY ISNULL(discount_pct) ASC, discount_pct ASC;

Parameters & Arguments Breakdown

  • ASC: Sorts values in ascending order (A-Z, 0-9). This is the default.
  • DESC: Sorts values in descending order (Z-A, 9-0).
  • ISNULL(col): Returns 1 if col is NULL and 0 if non-NULL. Sorting by ISNULL(col) ASC places non-NULLs (0) before NULLs (1).

Sample Walkthrough (Q&A)

Question: How do you force NULL values to appear at the end when sorting ascending in MySQL?

SQL Pattern: ORDER BY ISNULL(col) ASC, col ASC;

Result: Non-null values sort lowest-to-highest, and all NULLs are pushed to the very end

Explanation: ISNULL(col) evaluates to 0 for real values and 1 for NULLs; sorting by this flag first forces NULLs to the bottom.

Common Mistakes & Limitations

  • Assuming MySQL supports PostgreSQL's `NULLS LAST` syntax; running `ORDER BY rating ASC NULLS LAST` throws a syntax error in MySQL.
  • Sorting by column ordinal numbers (`ORDER BY 1, 2`) in production queries, which silently corrupts sorting when SELECT columns are reordered.

Keyboard Shortcuts (MySQL Workbench)

ActionWindowsMac
Execute QueryCtrl + EnterCmd + Enter

Pro Tips & Modern Best Practices

  • In MySQL, an alternative trick to push NULLs to the bottom in descending sort is `ORDER BY col IS NULL, col DESC`.
  • For fast sorting on large datasets, ensure an index exists that matches both the columns and sort directions in your `ORDER BY` clause.