
- 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
In which conditions, MySQL CASE statement return NULL?
As we know that if no comparison or condition is true then CASE statement returns the result specified after ELSE statement. But what if there is no ELSE statement, then in this situation, CASE statement would return NULL. Following is an example to demonstrate it.
Example
mysql> Select CASE 100 -> WHEN 150 THEN 'It is matched' -> WHEN 200 THEN 'It is not matched' -> END As 'It Returns NULL'; +-----------------+ | It Returns NULL | +-----------------+ | NULL | +-----------------+ 1 row in set (0.00 sec)
The query below, using the data from table ‘Students’, returns NULL because no student belongs to country WI.
mysql> Select SUM(CASE WHEN country = 'USA' THEN 1 ELSE 0 END) AS USA, -> SUM(CASE WHEN country = 'UK' THEN 1 ELSE 0 END) AS UK, -> SUM(CASE WHEN country = 'INDIA' THEN 1 ELSE 0 END) AS INDIA, -> SUM(CASE WHEN country = 'Russia' THEN 1 ELSE 0 END) AS Russia, -> SUM(CASE WHEN country = 'France' THEN 1 ELSE 0 END) AS France, -> SUM(CASE WHEN country = 'NZ' THEN 1 ELSE 0 END) AS NZ, -> SUM(CASE WHEN country = 'Australia' THEN 1 ELSE 0 END) AS Australia, -> SUM(CASE WHEN country = 'WI' THEN 1 END) AS WI -> From Students; +------+------+-------+--------+--------+------+-----------+------+ | USA | UK | INDIA | Russia | France | NZ | Australia | WI | +------+------+-------+--------+--------+------+-----------+------+ | 2 | 1 | 2 | 1 | 1 | 1 | 1 | NULL | +------+------+-------+--------+--------+------+-----------+------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL CASE statement to place custom values in place of NULL
- Which is faster, a MySQL CASE statement or a PHP if statement?
- Conditional NOT NULL case MySQL?
- MySQL If statement with multiple conditions?
- MySQL case statement inside a select statement?
- Return null for date_format when input is null in MySQL?
- How to use NULL in MySQL SELECT statement?
- Display records with conditions set using if statement in UPDATE statement with MySQL
- Perform count with CASE WHEN statement in MySQL?
- Sum if all rows are not null else return null in MySQL?
- Why should we use MySQL CASE Statement?
- MySQL order by field using CASE Statement
- Implement MySQL CASE statement with WHEN clause
- CASE WHEN column1 IS NULL THEN NULL ELSE column2 END with MySQL
- How can I return 0 for NULL in MySQL?

Advertisements