How to Find the Maximum of Multiple Columns in SQL
Last Updated :
10 Apr, 2024
Finding the maximum value of multiple columns is one of the most common analytical tasks essential for making decisions and analyzing data. Using the MAX() function of SQL, users can find the maximum value in a single column. But to find the maximum value in multiple columns, users need to use other SQL statements like CASE or GREATEST.
This article covers the methods to find the max value of multiple columns in a SQL table.
Demo SQL Database
Below is a demo SQL table that will be used in examples later in the article.
id | column1 | column2 |
---|
1 | 10 | 20 |
2 | 25 | 15 |
3 | 5 | 40 |
4 | 30 | 10 |
To create this table, write the following SQL queries:
SQL
CREATE TABLE SampleTable (
id INT PRIMARY KEY,
column1 INT,
column2 INT
);
INSERT INTO SampleTable (id, column1, column2)
VALUES
(1, 10, 20),
(2, 25, 15),
(3, 5, 40),
(4, 30, 10);
Finding the Maximum Value Among Multiple Columns in SQL
There are two methods in SQL to find the maximum value of multiple columns in SQL:
Let's study each of these methods in detail.
Using CASE expression to Find Max Value of Multiple Columns in SQL
The CASE expression allows the user to write conditions much like if-else or switch statements in SQL. We can use this to create condition when one column is greater than every other.
Example
Finding maximum of two columns in SQL:
SELECT id, column1, column2,
CASE
WHEN column1 > column2 THEN column1
ELSE column2
END AS max_value
FROM SampleTable;
Output:
Using CASE expressionExplanation: As you can already see if the number of columns from which we have to compare increases, the code becomes very complicated and cumbersome. The next method solves this issue.
Using GREATEST() Function to Find Max Value of Multiple Columns in SQL
The GREATEST() function returns the maximum value of all the arguments with the number of arguments can be anything. So using the GREATEST() function we can compare literal values as well as columns.
Example
Finding max of two columns in SQL using GREATEST function
SELECT id, column1, column2,
GREATEST(column1, column2) AS max_value
FROM SampleTable;
Output:
OutputConclusion
In this article, we covered how to find the maximum of multiple columns in SQL. There are two different methods to do this, first using CASE statement and second the GREATEST function.
GREATEST function is a better option as CASE statement is a more complex approach to find max value of multiple columns. Both the methods are explained with examples for better understanding.
Similar Reads
How to Find the Maximum of Multiple Columns in SQLite? SQLite is a serverless architecture that does not require any server to perform operations and queries. It is widely used in embedded systems, mobile applications, and small-scale web applications because of its simplicity, efficiency, and portability. SQLite supports most of the standard SQL featur
4 min read
How to Find the Maximum of Multiple Columns in PL/SQL? In PL/SQL finding the maximum value of multiple columns is a common requirement for maintaining the database. This operation is important for various applications, from financial analysis to data reporting. In this article, we will learn How to find the maximum of multiple columns in PL/SQL with the
5 min read
How to Find the Maximum of Multiple Columns in SQL Server? When working with SQL Server databases, there are times when we need to find the maximum value among multiple columns. This task can be accomplished using various techniques within SQL queries. By using functions like CASE and GREATEST, SQL Server provides efficient ways to determine the maximum val
4 min read
How to Find the Maximum of Multiple Columns in PostgreSQL? PostgreSQL is one of the most advanced general-purpose object-relational database management systems and is open-source. Being an open-source software, its source code is available under PostgreSQL license, a liberal open-source license. Anyone with the right skills is free to use, modify, and distr
4 min read
How to Find the Maximum of Multiple Columns in MySQL? Database management often involves the need to extract meaningful insights from multiple columns. One common challenge is identifying the maximum value across these columns. MySQL, a robust database management system, offers effective functionality for such tasks. To find the maximum value among sev
5 min read