
- 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
Fetch maximum value from multiple columns with null and non-null values?
For this, you can use COALESCE(). For the maximum value, use GREATEST() in MySQL. Let us first create a table −
mysql> create table DemoTable -> ( -> Value1 int, -> Value2 int, -> Value3 int -> ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(NULL,80,76); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(NULL,NULL,100); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(56,NULL,45); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(56,120,90); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+--------+--------+ | Value1 | Value2 | Value3 | +--------+--------+--------+ | NULL | 80 | 76 | | NULL | NULL | 100 | | 56 | NULL | 45 | | 56 | 120 | 90 | +--------+--------+--------+ 4 rows in set (0.00 sec)
Following is the query to fetch the maximum value from multiple columns −
mysql> select greatest(coalesce(Value1,0),coalesce(Value2,0),coalesce(Value3,0)) from DemoTable;
This will produce the following output −
+--------------------------------------------------------------------+ | greatest(coalesce(Value1,0),coalesce(Value2,0),coalesce(Value3,0)) | +--------------------------------------------------------------------+ | 80 | | 100 | | 56 | | 120 | +--------------------------------------------------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- How does COALESCE order results with NULL and NON-NULL values?
- Perform mathematical calculations in a MySQL table with NULL and NON-NULL values
- 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?
- MySQL select query to fetch data with null value?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Update all the fields in a table with null or non-null values with MySQL
- Display first non-null values with coalesce() in MySQL?
- Looping in JavaScript to count non-null and non-empty values
- Python Pandas – Propagate non-null values backward
- Python Pandas – Propagate non-null values forward
- Display the result with not null value first and then with null value in MySQL
- Replace null values with default value in Java Map
- Which MySQL function is used to find first non-NULL value from a list of values?
- Populate null columns in a MySQL table and set values
- How to filter non-null value in Java?

Advertisements