MySQL
Order By
alphanumeric sorting
database queries
SQL sorting

MySQL 'Order By' - sorting alphanumeric correctly

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Alphanumeric sorting becomes tricky when a column mixes letters and numbers in the same string. A normal ORDER BY on text is lexical, so values such as Item10 come before Item2 because character comparison reaches 1 before 2. If you want natural-looking order, you need to separate the textual part from the numeric part or store the data in a more sortable form.

Why normal string sorting looks wrong

A plain text sort does this:

sql
SELECT code
FROM products
ORDER BY code;

For data like this:

  • 'Item1'
  • 'Item2'
  • 'Item10'

The result is usually:

  • 'Item1'
  • 'Item10'
  • 'Item2'

That is correct for string comparison, but it is not the natural order most humans expect.

Extract and sort the numeric suffix

If your values follow a predictable pattern such as a fixed prefix plus a numeric suffix, you can sort by the textual part and then by the numeric part cast as a number.

sql
1SELECT code
2FROM products
3ORDER BY
4    LEFT(code, 4),
5    CAST(SUBSTRING(code, 5) AS UNSIGNED);

For values like Item1, Item2, and Item10, this produces the more natural order because 1, 2, and 10 are compared numerically instead of lexically.

This works well only when the string format is consistent. If the prefix length varies or numbers appear in different positions, the extraction logic needs to match the real data pattern.

Store sortable parts separately when possible

If you control the schema, the cleanest design is often to split the value into separate columns.

sql
1CREATE TABLE products (
2    prefix VARCHAR(20) NOT NULL,
3    sequence_no INT NOT NULL
4);
5
6SELECT prefix, sequence_no
7FROM products
8ORDER BY prefix, sequence_no;

This is simpler, easier to index, and less error-prone than parsing mixed strings during every query. The more often you need natural sorting, the more this design improvement pays off.

Zero-padding is another practical option

If the values are generated by your application, zero-padding the numeric part can make lexical sorting behave naturally.

Examples:

  • 'Item001'
  • 'Item002'
  • 'Item010'

Then a normal ORDER BY code works because the strings are aligned by width.

This is often the easiest solution when you only need display-friendly identifiers and the numeric range is known in advance.

Mixed-format data is harder

Natural sorting becomes more complicated when the values are inconsistent, such as:

  • 'A2'
  • 'A10'
  • 'B3'
  • 'Item-7'
  • 'Part12X'

At that point, a one-line ORDER BY expression can become fragile. You may need:

  • A computed sort key
  • A cleaned column stored in the table
  • Application-side sorting after structured parsing

If the format is not stable, the real fix is often data normalization rather than a clever SQL expression.

Common Pitfalls

The biggest mistake is expecting MySQL to guess "natural sort" automatically from mixed strings. A normal ORDER BY on text does pure lexical comparison.

Another issue is writing extraction logic that assumes every row has the same format when the data actually varies. That can produce wrong order or failed casts.

Developers also overlook schema design. If prefix and number are conceptually separate fields, storing them together makes sorting harder than it needs to be.

Finally, remember that expression-based sorting can reduce index usefulness. If performance matters, a normalized schema or stored sort key may be better than parsing strings on every query.

Summary

  • Plain ORDER BY on a text column sorts lexically, not naturally.
  • For predictable patterns, extract the numeric suffix and cast it to a number.
  • If you control the schema, store text and numeric parts separately.
  • Zero-padding numbers is a simple way to make lexical sorting behave naturally.
  • Inconsistent mixed-format data is usually a data-model problem more than a query problem.

Course illustration
Course illustration

All Rights Reserved.