Skip to content

Instantly share code, notes, and snippets.

@mikehins
Created July 26, 2023 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikehins/90ad980df473b0967f5ae6800e23f2a1 to your computer and use it in GitHub Desktop.
Save mikehins/90ad980df473b0967f5ae6800e23f2a1 to your computer and use it in GitHub Desktop.
# ORDER BY with nullable columns
# In MySQL they will be placed before everything.
# But the intention of the application or the UX may need a different sort order.
# In these cases the ordering for NULL values can be changed easily.
-- Default behaviour: NULL values placed first
SELECT *
FROM customers ORDERBY country ASC;
-- NULL values placed first by rule
SELECT *
FROM customers
ORDER BY country ISNOTNULL, country ASC;
-- NULL values placed last by rule
SELECT*
FROM customers
ORDER BY country ISNULL, country ASC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment