
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to order by NULL values
Let us first create a table −
mysql> create table DemoTable707 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentFirstName varchar(100), StudentMarks int ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable707(StudentFirstName,StudentMarks) values('John',45); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable707(StudentFirstName,StudentMarks) values(NULL,65); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable707(StudentFirstName,StudentMarks) values('Chris',78); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable707(StudentFirstName,StudentMarks) values(NULL,89); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable707(StudentFirstName,StudentMarks) values('Robert',99); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable707(StudentFirstName,StudentMarks) values(NULL,34); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable707(StudentFirstName,StudentMarks) values('Mike',43); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable707;
This will produce the following output -
+-----------+------------------+--------------+ | StudentId | StudentFirstName | StudentMarks | +-----------+------------------+--------------+ | 1 | John | 45 | | 2 | NULL | 65 | | 3 | Chris | 78 | | 4 | NULL | 89 | | 5 | Robert | 99 | | 6 | NULL | 34 | | 7 | Mike | 43 | +-----------+------------------+--------------+ 7 rows in set (0.00 sec)
Following is the query to order by NULL values −
mysql> select *from DemoTable707 order by StudentFirstName IS NULL, StudentFirstName DESC;
This will produce the following output -
+-----------+------------------+--------------+ | StudentId | StudentFirstName | StudentMarks | +-----------+------------------+--------------+ | 5 | Robert | 99 | | 7 | Mike | 43 | | 1 | John | 45 | | 3 | Chris | 78 | | 2 | NULL | 65 | | 4 | NULL | 89 | | 6 | NULL | 34 | +-----------+------------------+--------------+ 7 rows in set (0.00 sec)
- Related Articles
- MySQL query to order by two fields and NULL values in chronological order?
- MySQL query to convert empty values to NULL?
- MySQL query to set values for NULL occurrence
- Order MySQL query by multiple ids?
- MySQL query to select distinct order by id
- Can we use ORDER BY NULL in MySQL?
- How to concat Values in MySQL Query and to handle Null values as well?
- MySQL query to replace only the NULL values from the table?
- MySQL query to display only the empty and NULL values together?
- MySQL query to order by current day and month?
- MySQL query to compare and display only the rows with NULL values
- How to use a single MySQL query to count column values ignoring null?
- Query non-empty values of a row first in ascending order and then display NULL values
- How to order DESC by a field, but list the NULL values first?
- Count zero, NULL and distinct values except zero and NULL with a single MySQL query

Advertisements