
- 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
How to use NULL in MySQL SELECT statement?
In MySQL, the length of NULL is 0. Here, we will see how NULL can be used with SELECT statement. Let us create a table with the help of CREATE command −
Creating a table −
mysql> CREATE table NullWIthSelect -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.62 sec)
Above, I have created a table successfully. Now I will insert some records with the help of INSERT command −
Inserting records −
mysql> INSERT into NullWIthSelect values('John'); Query OK, 1 row affected (0.16 sec) mysql> INSERT into NullWIthSelect values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> INSERT into NullWIthSelect values(); Query OK, 1 row affected (0.18 sec) mysql> INSERT into NullWIthSelect values('Carol'); Query OK, 1 row affected (0.15 sec) mysql> INSERT into NullWIthSelect values(''); Query OK, 1 row affected (0.16 sec) mysql> INSERT into NullWIthSelect values('David'); Query OK, 1 row affected (0.19 sec) mysql> INSERT into NullWIthSelect values(); Query OK, 1 row affected (0.08 sec)
Above, I have inserted 7 records in which one record has empty value and two have null values. Rest of them has some values.
To display all the records, we can use the SELECT command −
mysql> SELECT * from NullWIthSelect;
The following is the output
+-------+ | Name | +-------+ | John | | Bob | | NULL | | Carol | | | | David | | NULL | +-------+ 7 rows in set (0.00 sec)
Now, we can use NULL with SELECT statement as shown below.
Firstly, let us see the syntax −
SELECT * from yourTableNamet where column_name is NULL;
Applying the above query to know which column value is null. The query is as follows −
mysql> SELECT * from NullWIthSelect where Name is NULL;
The following is the output −
+------+ | Name | +------+ | NULL | | NULL | +------+ 2 rows in set (0.00 sec)
Now, we can get the length of NULL value that is 0. The query is as follows −
mysql> SELECT count(Name) from NullWIthSelect where Name is NULL;
The following is the output −
+-------------+ | count(Name) | +-------------+ | 0 | +-------------+ 1 row in set (0.04 sec)
- Related Articles
- Can we use SELECT NULL statement in a MySQL query?
- How to use a select statement while updating in MySQL?
- How to use the CAST function in a MySQL SELECT statement?
- How to use prepared statement for select query in Java with MySQL?
- How can I use MySQL IF() function within SELECT statement?
- How to select ID column as null in MySQL?
- How can we use MySQL SELECT statement to count number of rows in a table?
- How to create Tab Delimited Select statement in MySQL?
- SELECT WHERE IN null in MySQL?
- Explain the use of SELECT DISTINCT statement in MySQL using Python?
- Count only null values in two different columns and display in one MySQL select statement?
- How can we use SET statement to assign a SELECT result to a MySQL user variable?
- How to select return value from MySQL prepared statement?
- How to use alias in MySQL select query?
- MySQL case statement inside a select statement?
