Can we use IFNULL along with MySQL ORDER BY?


You can use IFNULL along with ORDER BY clause. The syntax is as follows −

SELECT *FROM yourTableName ORDER BY IFNULL(yourColumnName1,yourColumnName2);

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table IfNullDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> ProductName varchar(10),
   -> ProductWholePrice float,
   -> ProductRetailPrice float,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (1.19 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into IfNullDemo(ProductName,ProductWholePrice,ProductRetailPrice) values('Product-1',99.50,150.50);
Query OK, 1 row affected (0.21 sec)
mysql> insert into IfNullDemo(ProductName,ProductWholePrice,ProductRetailPrice) values('Product-2',NULL,76.56);
Query OK, 1 row affected (0.18 sec)
mysql> insert into IfNullDemo(ProductName,ProductWholePrice,ProductRetailPrice) values('Product-3',105.40,NULL);
Query OK, 1 row affected (0.20 sec)
mysql> insert into IfNullDemo(ProductName,ProductWholePrice,ProductRetailPrice) values('Product-4',NULL,NULL);
Query OK, 1 row affected (0.18 sec)
mysql> insert into IfNullDemo(ProductName,ProductWholePrice,ProductRetailPrice) values('Product-5',209.90,400.50);
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement. The query is as follows −

mysql> select *from IfNullDemo;

The following is the output −

+----+-------------+-------------------+--------------------+
| Id | ProductName | ProductWholePrice | ProductRetailPrice |
+----+-------------+-------------------+--------------------+
|  1 | Product-1   |              99.5 |              150.5 |
|  2 | Product-2   |              NULL |              76.56 |
|  3 | Product-3   |             105.4 |               NULL |
|  4 | Product-4   |              NULL |               NULL |
|  5 | Product-5   |             209.9 |              400.5 |
+----+-------------+-------------------+--------------------+
5 rows in set (0.02 sec)

Here is the query to order by if null −

mysql> select *from IfNullDemo order by ifnull(ProductWholePrice,ProductRetailPrice);

The following is the output −

+----+-------------+-------------------+--------------------+
| Id | ProductName | ProductWholePrice | ProductRetailPrice |
+----+-------------+-------------------+--------------------+
|  4 | Product-4  |               NULL |               NULL |
|  2 | Product-2  |               NULL |              76.56 |
|  1 | Product-1  |               99.5 |              150.5 |
|  3 | Product-3  |              105.4 |               NULL |
|  5 | Product-5  |              209.9 |              400.5 |
+----+-------------+-------------------+--------------------+
5 rows in set (0.00 sec)

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

218 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements