How to Find the Maximum of Multiple Columns in PL/SQL?
Last Updated :
11 Nov, 2024
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 help of two different methods, the GREATEST function and the CASE statement to resolve problems by various examples.
Method 1: Using the GREATEST Function
To find the maximum of multiple columns in PL/SQL, the GREATEST function is utilized. This function compares multiple expressions and returns the greatest value among them. The syntax for using the GREATEST function is as follows:
Syntax
SELECT GREATEST(column1, column2, column3, ...)
AS max_value FROM table_name;
Explanation:
- GREATEST: It is a function that is used to find the greatest value among a list of expressions.
- AS: This is a keyword in SQL used to alias the result of a query. In this case, max_value is the alias given to the result of the GREATEST() function.
- max_value: This is the alias assigned to the result of the GREATEST() function. It represents the maximum value among the specified columns.
- FROM: It is a keyword that is used to specify the table from which data will retrieved.
- table_name: This is the name of the table from which data will retrieved.
Examples of Using GREATEST Function
Example 1: Let's Say We Have a Table Called "Students" with the Following Data
We can create the Students table using the following code which defines the table structure with columns such as Stu_name, hindi, English and maths as Columns.
Query:
PL/SQL
CREATE TABLE students (
Stu_name VARCHAR(50),
hindi INT,
english INT,
maths INT
);
INSERT INTO students (Stu_name, hindi, english, maths) VALUES
('John', 85, 90, 95),
('Alice', 78, 92, 88),
('Bob', 92, 85, 90),
('Emma', 80, 88, 92),
('Michael', 95, 82, 89);
Output:
Students TableSuppose we have to find the maximum mark for each student. we have to find the maximum mark for each student using the following query.
Query:
SELECT Stu_name,
GREATEST(hindi, english, maths) AS max_mark
FROM students;
Output:
OutputExplanation: We can observe that the desired output is obtained. The query identifies the highest score among hindi, english, and maths for each student.
Method 2: Using the CASE Statement
An alternative approach to finding the maximum value of multiple columns in PL/SQL is by using the CASE statement. The syntax for using the CASE statement to find the maximum value is as follows.
Syntax
SELECT
CASE
WHEN column1 >= column2 AND column1 >= column3 THEN column1
WHEN column2 >= column1 AND column2 >= column3 THEN column2
ELSE column3
END AS max_value
FROM table_name;
Explanation:
- SELECT: This is a keyword in SQL used to retrieve data from a database.
- CASE: The CASE statement is a conditional expression used to evaluate multiple conditions and return a value based on the first condition that is true.
- >=: Greater than or equal to comparison operator.
- WHEN: This keyword is used within the CASE statement to specify the condition that is being evaluated.
- THEN: This keyword is used in the CASE statement to specify the value that should be returned if the condition specified in the WHEN clause is true.
- ELSE: This keyword is used in the CASE statement to specify the value that should be returned if none of the conditions specified in the WHEN clauses are true.
- AS max_value: This part of the query assigns an alias "max_value" to the result of the CASE statement, providing a name for the calculated maximum value.
- FROM table_name: This specifies the table from which the data is being retrieved. "table_name" is a placeholder for the actual name of the table in the database.
Examples of Using CASE Statement
Example 1: Let's Say We Have a Table Called "sales_data" with the Following Data
We can create the sales_data table using the following code which defines the table structure with columns such as product_id, sales_jan, sales_feb and sales_mar as Columns.
Query:
PL/SQL
CREATE TABLE sales_data (
product_id INT,
sales_jan INT,
sales_feb INT,
sales_mar INT
);
INSERT INTO sales_data (product_id, sales_jan, sales_feb, sales_mar) VALUES
(1, 100, 120, 110),
(2, 150, 130, 140),
(3, 90, 95, 100),
(4, 80, 85, 90),
(5, 200, 180, 210);
Output:
Sale's Table Explanation: We have fetched the output using CASE Statement.
Now, we need to find the maximum sales for each product_id. We can achieve this using the following query.
Query:
SELECT
product_id,
CASE
WHEN sales_jan >= sales_feb AND sales_jan >= sales_mar THEN sales_jan
WHEN sales_feb >= sales_jan AND sales_feb >= sales_mar THEN sales_feb
ELSE sales_mar
END AS max_sales
FROM sales_data;
Output:
OutputExplanation: We can observe that the desired output is obtained. The query evaluates each column and determines the maximum sales figure for each product_id.
Conclusion
Overall we can say that by understanding both the GREATEST function and the CASE statement we can effectively find the maximum value among multiple columns in PL/SQL. While GREATEST offers a simpler syntax, the CASE statement allows for more complex conditional logic, making both approaches useful depending on the specific requirements.
Similar Reads
How to Find the Maximum of Multiple Columns in SQL
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
2 min read
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 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 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 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
How to SELECT DISTINCT on Multiple Columns in SQL?
In the world of databases, data duplication can lead to confusion and inefficiency. SQL provides a powerful tool, SELECT DISTINCT, to retrieve unique values from columns. However, when dealing with multiple columns, the approach becomes more detailed. In this article, we will explain how to use SELE
4 min read
How to Get the Type of Columns in SQL
In SQL, the types of columns in a database table play a fundamental role in data management and query execution. Each column is designed to store specific types of data, such as numbers, text, dates, or binary data. Understanding these column types is essential for effective database design, query o
4 min read
if-else to find the maximum of two numbers.
In this article, we will discuss how to find the maximum of two numbers with its working example in the R Programming Language using R if-else conditions. Syntax:max_number <- if (condition) { # Code block executed if the condition is TRUE value_if_true } else { # Code block executed if the condi
2 min read
How to Efficiently Convert Rows to Columns in PL/SQL?
In Oracle PL/SQL, converting rows into columns is a common operation, especially useful for reporting, data analysis, and reformatting data for easy visualization. PL/SQL, or Procedural Language/Structured Query Language, is a powerful procedural extension to SQL, created by Oracle, that integrates
5 min read
How to Select Row With Max Value on a Column in SQL?
SQL is a powerful language for managing and handling relational databases. A common requirement in database management is to retrieve rows where a specific column has the maximum value. Here, we will look at different ways to do this, depending on different situations and database environments. This
5 min read