SQLite MAX Function and HAVING Clause
MAX function can also be used with the HAVING clause. HAVING clause will be used after the GROUP BY statement, so it is necessary to use GROUP BY in the statement in which we will use HAVING. The generic syntax is as follows –
SELECT
Col1,Col2,Col3,.......,ColN
FROM
Table_Name
GROUP BY
Column_Name
HAVING Condition / Expression;
We will print the Employee ID, FirstName and LastName of only those employees, whose salary is greater than 25,000. We will pass this condition after the HAVING clause.
SELECT empID, FirstName, LastName, MAX(Salary) AS Maximum_Salary FROM Employees GROUP BY Salary HAVING MAX(Salary) > 25000;
Output:
In the output we can clearly see only the details of those employees having a salary more than 25,000 have been grouped based on their salary from lowest to highest.
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