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 :
CREATE DATABASE <DatabaseName>;
CREATE DATABASE CSEportal;
Query returned successfully in 3 secs 817 msec.
Using the Database :
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 :
CREATE TABLE TableName (field1 dataType1 , field2 dataType2...fieldN dataTypeN);
CREATE TABLE GeeksforGeeks(
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 :
INSERT INTO tablename (field1,field2,...fieldN) VALUES (value1,value2...valueN);
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;
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:
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;
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:
Select max(sno),max(description) from GeeksforGeeks;
4 well explained
So here, we have retrieved the maximum value of two fields (out of all the rows) using a single query only.
Unlock the Power of Placement Preparation!
Feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our Complete Interview Preparation
Course is the ultimate guide to conquer placements. Trusted by over 100,000+ geeks, this course is your roadmap to interview triumph.
Ready to dive in? Explore our Free Demo Content and join our Complete Interview Preparation