Lesson

MySQL REVERSE() Function

10 min reading
Free Course

MySQL REVERSE() Function

The REVERSE() function returns a string with all of its characters arranged in backward order. For non-binary utf8mb4 strings, MySQL reverses logical characters safely without scrambling multibyte UTF-8 sequences.

Syntax

REVERSE(str)
  • str: The string expression or column to reverse.
  • Return value: Reversed string, '' for '', or NULL when str is NULL.

Because REVERSE() operates on characters for text types, REVERSE('café') produces 'éfac' cleanly.

Practical Example

Suppose demo_orders contains tracking_number strings of varying lengths, and you want to extract or inspect the last token before a hyphen or build a reversed checksum view.

SELECT
    order_id,
    tracking_number,
    REVERSE(tracking_number) AS reversed_tracking,
    REVERSE(SUBSTRING_INDEX(REVERSE(tracking_number), '-', 1)) AS last_segment
FROM demo_orders
ORDER BY order_id;
order_id tracking_number reversed_tracking last_segment
501 TRK-2026-991 199-6202-KRT 991
502 AIR-88-PL LP-88-RIA PL
503 NULL NULL NULL

Note that SUBSTRING_INDEX(tracking_number, '-', -1) extracts '991' more directly, though REVERSE() helps when pairing with LOCATE() from the right edge.

Common Mistakes & NULL Behavior

  • Using REVERSE() on BINARY columns: On BINARY or VARBINARY data, REVERSE() flips raw bytes. Reversing raw UTF-8 bytes stored inside a BLOB corrupts multibyte characters.
  • Reversing combining diacritics: If a character uses a base letter plus a separate combining accent code point, reversing swaps their order and shifts the accent onto the neighbor.
  • Indexing reversed domains: Some teams index REVERSE(email) to speed up suffix searches like @gmail.com using a standard B-tree prefix scan (LIKE REVERSE('%@gmail.com')).
  • NULL input: REVERSE(NULL) returns NULL.

Frequently Asked Questions

How can I check if a string is a palindrome in MySQL?

Compare WHERE code = REVERSE(code). Under default _ci collations, 'Level' matches 'leveL' unless you compare with BINARY.

Does REVERSE() trim trailing spaces before flipping?

No. Trailing spaces become leading spaces in the reversed output (REVERSE('abc ') yields ' cba').

Is REVERSE() available in SQLite as well?

No. Standard SQLite does not include a built-in REVERSE() function, whereas MySQL supports it natively.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum