How to Get the Max Value for Each Column?

MAX() Function is a function that is used to return the maximum value from a set of values. It can be used with a single column or with multiple columns. To get the maximum value for each column in a table, we can use the MAX() function along with below methods which are given below:

  1. Using SELF-JOIN
  2. USING WHERE CLAUSE

let’s Setting Up the Environment

For better understanding, we need a table on which we will perform various operations and queries. The below query creates a table and then inserts some records in it.

Query:

CREATE TABLE test (
  id INTEGER PRIMARY KEY,
  val1 VARCHAR(20),
  val2 INTEGER
);

INSERT INTO test VALUES (21, 'val1', 32);
INSERT INTO test VALUES (11, 'val2', 90);
INSERT INTO test VALUES (90, 'val1', 18);
INSERT INTO test VALUES (77, 'val1', 65);
INSERT INTO test VALUES (43, 'val3', 20);
INSERT INTO test VALUES (81, 'val3', 88);
INSERT INTO test VALUES (29, 'val2', 72);
INSERT INTO test VALUES (55, 'val2', 47);
INSERT INTO test VALUES (72, 'val3', 11);

Output:

Output

How to Select Row With Max Value in SQL Server

In SQL Server, retrieving rows that contain the maximum value for a specific column for each distinct value in another column can be a common and challenging task. This process is done by identifying the maximum value for each group and then selecting the corresponding rows. In this article, we’ll explore how to achieve this using SQL Server’s features, providing clarity and efficiency in fetching such records.

Similar Reads

How to Get the Max Value for Each Column?

MAX() Function is a function that is used to return the maximum value from a set of values. It can be used with a single column or with multiple columns. To get the maximum value for each column in a table, we can use the MAX() function along with below methods which are given below:...

1. Using SELF JOIN

A self-join perform an operation when a table is joined with itself. To solve the problem presented to us, we will join the original table with a little modified version of the table. In the modified version, we will group the table by val1 column and then use the MAX() function to find the maximum value of val2 for each group. We will later join this with the original table on the values of t1.val1=t2.val1 and t1.val2=t2.max_val2....

2. USING WHERE CLAUSE

We can make use of WHERE clause along with a subquery to find the max value. Just like in method 1, we will create a modified version of the table in which we group the data and find the maximum value for each group. Just the difference in this method is that rather than using JOIN, we will use WHERE to compare the value in the original table with the modified table....

More Technical Example

Let’s create the table and insert some data inside it. The following query creates a sale_record table and inserts several records in it....

Conclusion

In this article, we covered how we can find the records which have the maximum value for a column for each distinct value of another column in SQL Server. We had a chance to look at two different methods to go about doing this, first using SELF-JOIN and later looked at how we can achieve the same using WHERE clause. We also how we can use the concepts we learned in this article to a real-life situation through the technical example....