
- 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
Display the result with not null value first and then with null value in MySQL
Let us first create a table −
mysql> create table DemoTable1357 -> ( -> StudentName varchar(40), -> StudentCountryName varchar(30) -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1357 values('Chris','US'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1357 values('David',NULL); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1357 values('David','AUS'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1357 values('Carol',NULL); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1357 values('Mike','UK'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1357;
This will produce the following output −
+-------------+--------------------+ | StudentName | StudentCountryName | +-------------+--------------------+ | Chris | US | | David | NULL | | David | AUS | | Carol | NULL | | Mike | UK | +-------------+--------------------+ 5 rows in set (0.00 sec)
Following is the query to display the result with not null value first and then with null value −
mysql> select * from DemoTable1357 -> order by (StudentCountryName IS NULL),StudentName;
This will produce the following output −
+-------------+--------------------+ | StudentName | StudentCountryName | +-------------+--------------------+ | Chris | US | | David | AUS | | Mike | UK | | Carol | NULL | | David | NULL | +-------------+--------------------+ 5 rows in set (0.00 sec)
- Related Articles
- Display NULL and NOT NULL records except a single specific value in MySQL
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Display first non-null values with coalesce() in MySQL?
- Working with NULL and IS NOT NULL in MySQL
- Insert default into not null column if value is null in MySQL?
- Set 1 for NOT NULL value in MySQL
- MySQL select query to fetch data with null value?
- Counting presence of a NOT NULL value in MySQL
- Fetch maximum value from multiple columns with null and non-null values?
- CASE WHEN column1 IS NULL THEN NULL ELSE column2 END with MySQL
- MySQL: How can I find a value with special character and replace with NULL?
- Compare for NULL value and display value 1 for these values in a new MySQL column?
- Enum with NOT NULL in a MySQL field?
- How to retrieve the corresponding value for NULL with a MySQL query?
- Return value from a row if it is NOT NULL, else return the other row value in another column with MySQL

Advertisements