Table of contents
1.
Introduction
2.
Which SQL Keyword is used to Retrieve a Maximum Value?
2.1.
Syntax
3.
What is MAX() Keyword in SQL?
3.1.
Syntax of MAX() for Multiple Columns
4.
Example with Single Expression
4.1.
Create Table
4.2.
Insert Data  
4.3.
Query
5.
Table having NULL Value
5.1.
Update Query
5.2.
MAX() Query
6.
Example with GROUP BY Clause
6.1.
MAX() Query
6.2.
MAX() Query with HAVING Clause
7.
Example with WHERE Clause
7.1.
MAX() Query with Where Clause
8.
Frequently Asked Questions
8.1.
Can I use SQL MAX Function for Non-numeric Columns?
8.2.
What are NULL values in a Table?
8.3.
What do you mean by the Primary Key of a table?
9.
Conclusion
Last Updated: Mar 28, 2024
Medium

Which SQL Keyword is used to Retrieve a Maximum Value?

Author Nidhi Kumari
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

IBM researchers developed SQL in the 1970s. SQL stands for Structured Query Language. It is a simple and powerful programming language that interacts with different databases. SQL helps in managing and modifying the data in databases. SQL stores the data in the form of tables. SQL enables users to retrieve the precise data they require using a set of commands whenever they want. 

Which SQL Keyword is used to retrieve a maximum value?

Focusing on the SQL commands, we will discuss one SQL keyword in this article. After reading this article, you will be able to grasp the idea of which SQL keyword is used to retrieve a maximum value of a table.

Which SQL Keyword is used to Retrieve a Maximum Value?

Keywords are the reserved words in SQL used to manipulate the databases. They perform some particular tasks on databases. SQL contains various keywords such as SELECT INSERT, DELETE etc.
 
It is worth noting that SQL is case-insensitive. So, it does not matter if you are writing SELECT or select.

Now, coming to the question, which SQL keyword is used to retrieve a maximum value?

The answer is MAX() Keyword.

MAX() keyword in SQL retrieves a table's maximum or largest value. 

Syntax

SELECT MAX(Column-name)
FROM table
WHERE conditions;

 

Let's move to the next topic to understand the MAX() Keyword in detail.

What is MAX() Keyword in SQL?

As we discussed earlier, the MAX() keyword returns the maximum value. The MAX() function is an aggregate function in SQL.

An aggregate function in SQL returns a result having a single value after performing calculations on a group of values.

Aggregate Functions

Note: The aggregate functions ignore the NULL values in a table’s column.

You have seen the syntax of the MAX() keyword for a single column mentioned under the topic: which SQL keyword is used to retrieve a maximum value. What if you want to group the results of one or more columns?

Syntax of MAX() for Multiple Columns

Here is the MAX() Keyword’s syntax in case of grouping the results of one or more columns.

SELECT column1, column2, ... column_n,
       MAX(aggregate_column)
FROM tables
[WHERE conditions]
GROUP BY column1, column2, ... column_n;


Let’s understand the MAX() function with the help of examples.

Example with Single Expression

The SQL MAX function can be used in the simplest way possible by returning a single item that contains the MAX value.

Consider the example of the CodingNinjas_Articles table having Article_IDArticle_NameSubject, and Views as the fields; some data has been stored in the fields.

The following is the code for creating our table.

Create Table

CREATE TABLE CodingNinjas_Articles(
Article_ID int,
Article_name varchar(200),
Subject varchar(100),
views int
  );

Insert Data  

INSERT INTO CodingNinjas_Articles VALUES (1, 'SQL Keywords', 'DBMS', 1500);
INSERT INTO CodingNinjas_Articles VALUES (2, 'OSI Model', 'CN', 1300);
INSERT INTO CodingNinjas_Articles VALUES (3, 'Arrays', 'DSA', 1400);
INSERT INTO CodingNinjas_Articles VALUES (4, 'OOPs', 'JAVA', 1300);
INSERT INTO CodingNinjas_Articles VALUES (5, 'MAX() Keyword', 'SQL', 1600);


Using the SELECT statement, let's check the data in our table to see how it looks.

SELECT * FROM CodingNinjas_Articles

 

Output:

Output

Query

Now, we want to find the value of the maximum views. So, we will use the MAX() function.

SELECT MAX(views) AS "Maximum view"
FROM CodingNinjas_Articles;

 

Output:

Output


The MAX(views) field has been aliased as "Maximum view" in this SQL MAX function example. When the result set is returned, the field name will appear as "Maximum View."

Aliases are often used to improve the readability of column names. A table or a column within a table can have a temporary name using SQL aliases. By using the AS keyword, an alias is created. An alias only exists while that query is executing.

Table having NULL Value

Consider an example when our table has NULL values. The MAX() function will ignore the NULL Values.
You can update the table using the UPDATE Keyword. Here is an example:

Update Query

UPDATE CodingNinjas_articles
SET views = NULL
WHERE Article_ID = 5;

 

Output:

The table will appear as follows:

Output

MAX() Query

When we execute the MAX function, it will ignore the NULL Value. Here is an example:

SELECT MAX(views) AS "Maximum view"
FROM CodingNinjas_articles;

 

Output:

Output

Example with GROUP BY Clause

You can also use the MAX() function with the GROUP BY Clause. Here is the syntax.

SELECT MAX(column_name)
FROM table
GROUP BY column1, column 2….. column_n;

 

For example, we are using the SQL MAX function to return each subject's name and the subject's maximum views.

MAX() Query

SELECT Subject, MAX(views) AS "Maximum views"
FROM CodingNinjas_articles
GROUP BY Subject;

 

Output:

Output

You can use the HAVING clause to define the condition that needs to be met to filter out specific data from a given group according to that condition.

MAX() Query with HAVING Clause

The syntax of using the HAVING keyword along with GROUP BY and MAX() keywords are as follows.

SELECT MAX(column_name)
FROM table
GROUP BY column1, column 2….. column_n
HAVING Conditions;

 

Here is an example of the maximum view of an article which belongs to SQL Subject. 

SELECT MAX(views) AS "Maximum views" 
FROM CodingNinjas_articles
GROUP BY Subject
HAVING Subject = 'SQL';

 

Output:

Output

Example with WHERE Clause

You can also use the MAX() function with the WHERE Clause. Here is the syntax.

SELECT MAX(column_name)
FROM table
WHERE conditions;

MAX() Query with Where Clause

Let's assume you want to list the article’s and subject’s names along with the highest views they have received.

Here is the code:

SELECT Article_name, Subject, MAX(Views) 
FROM CodingNinjas_articles
WHERE views = 
(
    SELECT MAX(views)
    FROM CodingNinjas_articles
);

 

Output:

Output

Frequently Asked Questions

Can I use SQL MAX Function for Non-numeric Columns?

Yes, you can use the SQL MAX function for non-numeric columns. When you use the MAX function, the lexicographic order is considered to generate the output. It compares similar characters in the two strings from left to right to identify which string comes first. For example, ‘a’ will have high priority than ‘b’. 

What are NULL values in a Table?

According to SQL, any datatype may have a NULL value. A particular label called a NULL value is used in SQL to denote a data value that doesn't exist in the database. A field with spaces or a zero value differs from one with a NULL value.

What do you mean by the Primary Key of a table?

You can uniquely identify each record in a table using the Primary Key. A Primary Key should be UNIQUE and not NULL. For example, in a student_details table, the registration number can be considered a Primary Key. There can only be one Primary Key in a table.

Conclusion

We have discussed the MAX keyword in detail. We started by asking which SQL keyword is used to retrieve a maximum value and got the answer MAX(). You can use the MAX() keyword with GROUP BY and WHERE Clauses.

We hope this blog has helped you. We recommend you visit our articles on different topics of DBMS, such as

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!

Don't stop here, Ninja; check out the Top 100 SQL Problems to get hands-on experience with frequently asked interview questions and land your dream job.

Happy Reading!!

Live masterclass