SQLite MAX Function and GROUP BY Clause
The GROUP BY function is used to Group Together rows based on certain conditions. GROUP BY statement is generally used with other Aggregate functions like COUNT(), MAX(), MIN(), SUM(), and AVG(). We will see here how we can use MAX function along with GROUP BY function. The generic syntax is below –
SELECT
(Col1,Col2,Col3,.......,ColN)
FROM
Table_Name
GROUP BY
Column_Name
The Column used after GROUP BY should be used in the SELECT statement above, otherwise it will not work. For Example, we will use the MAX with the GROUP BY clause to print out the order of the maximum salary of each employee, regardless of their employee Id or anything else except their salary.
SELECT empID, FirstName, LastName, MAX(Salary) AS Maximum_Salary FROM Employees GROUP BY Salary;
Output:
In the output , we can clearly see that the Salary has been sorted from lowest to highest without considering anything else like FirstName , LastName, etc. The Salary has been GROUPED BY based on their amount.
SQLite MAX() Function
MAX function is a type of Aggregate Function available in SQLite, which is primarily used to find out the maximum value from a given set (a column that is passed as its parameter). Other than that, the MAX function can also be used with other Aggregate functions like HAVING, GROUP BY, etc to sort or get some values that are obeying the condition mentioned by these other Aggregate Functions.
In this article, we will see how we can use the MAX function to do various tasks and how the MAX function can be used in various ways to get more precise outputs from the table.
Contact Us