
- 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
Query non-empty values of a row first in ascending order and then display NULL values
For this, use ORDER BY ISNULL(). Let us first create a table −
mysql> create table DemoTable669 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentScore int ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable669(StudentScore) values(45) ; Query OK, 1 row affected (0.80 sec) mysql> insert into DemoTable669(StudentScore) values(null); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable669(StudentScore) values(89); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable669(StudentScore) values(null); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable669;
This will produce the following output −
+-----------+--------------+ | StudentId | StudentScore | +-----------+--------------+ | 1 | 45 | | 2 | NULL | | 3 | 89 | | 4 | NULL | +-----------+--------------+ 4 rows in set (0.00 sec)
Following is the query to display non-empty values in ascending order. The null values would be displayed afterwards −
mysql> select *from DemoTable669 ORDER BY ISNULL(StudentScore),StudentScore;
This will produce the following output −
+-----------+--------------+ | StudentId | StudentScore | +-----------+--------------+ | 1 | 45 | | 3 | 89 | | 2 | NULL | | 4 | NULL | +-----------+--------------+ 4 rows in set (0.00 sec)
- Related Articles
- Display first non-null values with coalesce() in MySQL?
- MySQL query to display only the empty and NULL values together?
- Return only the non-empty and non-null values from a table and fill the empty and NULL values with the corresponding column values in MySQL?
- Looping in JavaScript to count non-null and non-empty values
- MySQL Order by a specific column x and display remaining values in ascending order
- How does COALESCE order results with NULL and NON-NULL values?
- MySQL query to convert empty values to NULL?
- MySQL query to order by NULL values
- GROUP BY and display only non-empty column values in MySQL
- MySQL query to order by two fields and NULL values in chronological order?
- MySQL query to first set negative value in descending order and then positive value in ascending order
- Display records by first fixing the first two values in a column and then using DISTINCT to display other values in MySQL
- Ignore null values in MySQL and display rest of the values
- Ignore NULL values from separate tables in a single MySQL query and display count of NOT NULL records
- Fix a row value and then ORDER BY DESC rest of the values in MySQL

Advertisements