Skip to content
Related Articles

Related Articles

SQL Query for Finding Maximum Values in Rows
  • Last Updated : 07 Apr, 2021

SQL stands for Structured Query Language and is used to create, maintain and retrieve the data from relational databases. Relational Database Management Systems (RDBMS) like MySQL, MS Access, Oracle, and SQL Server use SQL as their standard database language. Here we are going to see the SQL query for Finding Maximum Values in Rows. Let us understand it by creating a database named “CSEportal”. 

Creating Database :

Syntax : 
CREATE DATABASE <DatabaseName>;
Example :
CREATE DATABASE CSEportal;
Output :
Query returned successfully in 3 secs 817 msec.

Using the Database :

Syntax :
USE <DatabaseName>;
Example :
USE CSEportal;

Using the above commands, we have successfully created our database named “CSEportal”, now we need to create a table(Relation) named “GeeksforGeeks” in this database. 

Creating table :



Syntax :
CREATE TABLE TableName (field1 dataType1 , field2 dataType2...fieldN dataTypeN);
Example : 
CREATE TABLE GeeksforGeeks(
sno int, 
Description VARCHAR(40), 
courses VARCHAR(40));

This will create an empty table, so let us populate our table with some records using the INSERT INTO command to perform the actual operations on the tables. 

Inserting records in the table :

Syntax :
INSERT INTO tablename (field1,field2,...fieldN) VALUES (value1,value2...valueN);
Example :
INSERT INTO GeeksforGeeks(sno,Description,Courses) VALUES(1,'Cse Portal','DBMS');

Similarly, we can fill our table using this INSERT INTO command. To see the created table, we can run the SELECT command which is shown below:

SELECT * from GeeksforGeeks;

Output :

Our Table “GeeksforGeeks”

Now we can move ahead to write our SQL query for finding maximum values in all the rows, This can be done using MAX(field) function in SQL. Let us try to retrieve the maximum value of the field “Description” as shown below:

Select max(Description) as Maximum from GeeksforGeeks;

Here we have used the ‘as ‘ keyword just to change the name of the resulting field as shown in the output below:

Output : 

Here we have got ‘well explained’ as the output since it is the maximum value out of all the rows of the table. Let us try to apply this on the field holding some numeric values to get a more clear idea. 

 Select max(sno) from GeeksforGeeks;

Output : 

Clearly, 4 is the maximum value out of all the rows of the table, hence we have got 4 as our output. Also, here we have not used the ‘as’ keyword, so in the resulting field, we have got ‘max(sno)’ as its name. It is an optional step and can be done in the same manner as shown above. We can also retrieve maximum values of more than one field out of all the rows using a single query as shown below:

Query:
Select max(sno),max(description) from GeeksforGeeks;

Output:
max(sno)    max(description)

4            well explained

So here, we have retrieved the maximum value of two fields (out of all the rows) using a single query only.

My Personal Notes arrow_drop_up
Recommended Articles
Page :