MySQL REVERSE() Function – How to reverse values in MySQL?

REVERSE() Function

In this tutorial, we will learn about the MySQL REVERSE() function. MySQL REVERSE() function is a string function that is used to reverse a given argument and return the result.

Suppose you have 123 as an argument in the REVERSE() function, then the result returned would be 321. The REVERSE() function is usually used with the SELECT Statement often.

Let’s begin with the syntax of the reverse() function and then move on to some of the examples where the function applies and how to use it with different values of the MySQL table.


Syntax of MySQL REVERSE()

The reverse function has a very simple syntax on its own. The function takes an expression as its argument and then reverses the string that is returned by the expression.

REVERSE(expression)

The expression can return a numeric or character string.


Examples of MySQL REVERSE()

Let’s go over some of the examples of the MySQL REVERSE() function now.

1. Basic Examples

Let us take a look at some basic examples to understand how REVERSE() works. How about reversing the following two expressions – “JournalDev” and 123? The queries for this are:

SELECT REVERSE(“JournalDev”);
SELECT REVERSE(123);

And the output we get is:

Reverse Basic Example

As you can see, we get the reversed values of the given arguments.

2. Reversing Data In Tables – Simple Example

Now let us use REVERSE() to reverse the values in a column of a table. Consider the below Employee table.

Employee Table Reverse
Employee Table

How about we reverse all the names of the employees in the table? The query is,

SELECT REVERSE(Name) FROM Employee;

And we get our output as,

Reverse Row Values

If you prefer to display the reversed column with another name, you can use the AS keyword to create an alias.

3. Palindrome Using REVERSE()

A palindrome is a number, string, phrase, or any other sequence of characters whose reversed value is the same as the original value. For example, if you reverse the word “madam”, you get the reversed word as “madam” again. So over here, the word “madam” is a palindrome string. Numbers can be palindrome too. Numbers like 121, 131, 232, 1001, 60606, and so on are palindrome numbers.

Now, let us take a complex example with the REVERSE() function. How about finding the distinct Palindrome values in the Office_Code column? Let us also give an alias called Palindrome_Office_Code to the result. So, we will make use of the DISTINCT keyword, WHERE clause, IN Operator, Aliases, and Subqueries for this answer.

SELECT DISTINCT Office_Code AS Palindrome_Office_Code FROM Employee 
WHERE REVERSE(Office_Code) IN 
(SELECT Office_Code FROM Employee);

So the inner subquery will return the list of values in the Office_Code column. If the value reversed office code value happens to be in that list, then it is returned in our result-set. The DISTINCT keyword avoids repetition of the same value.

The output we get is,

Reverse Palindrome Example

As you can see, only the value “MUM” is a palindrome in the Office_Code column.


Conclusion

The MySQL REVERSE() function is very useful and fun to play around with. It adds a whole new level of flexibility to query in MySQL.


References