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.
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 Maximum Value of Trig Function?
To find the maximum value of a trigonometric function, first identify its standard range. For sine and cosine, the maximum value is 1, since these functions oscillate between -1 and 1. For functions like asin(x) or acos(kx), multiply the amplitude by the maximum of the base function (1). For tangent
3 min read
Select row with maximum and minimum value in Pandas dataframe
Let's see how can we select rows with maximum and minimum values in Pandas Dataframe with help of different examples using Python. Creating a Dataframe to select rows with max and min values in DataframePython3 # importing pandas and numpy import pandas as pd import numpy as np # data of 2018 driver
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 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 Select Row With Max Value in SQL?
SQL(Structured Query Language) is a powerful tool that is used to manage and query data in relational databases. A common requirement in data analysis is finding the maximum value in a column for each distinct value of another column, such as determining the highest salary in each department. This c
4 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
How to find last value from any table in SQL Server
We could use LAST_VALUE() in SQL Server to find the last value from any table. LAST_VALUE() function used in SQL server is a type of window function that results the last value in an ordered partition of the given data set. Syntax : SELECT *, FROM tablename LAST_VALUE ( scalar_value ) OVER ( [PARTIT
2 min read