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.

Similar Reads

SQLite MAX Function

As mentioned earlier, the SQLite MAX function can be used in various ways based on the requirements of the user. If MAX is used all alone then it can used to fetch the maximum value from the set or the column passed as its parameter. When the MAX function is used with other Aggregate Functions like HAVING or GROUP BY it can be manipulated by the user as their requirement. The generic syntax of the MAX function is given below –...

Creating and Populating SQLite Table

We will use the CREATE TABLE command to create the table and provide the column names and their data types....

SQLite MAX Function in the Subquery

In this example, we will see how we can use the MAX function in a subquery to print the FirstName, LastName , and Salary of the Employee having the Maximum Salary. The generic Syntax while using the MAX function alongside Subquery is as follows –...

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 –...

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 –...

Conclusion

The MAX Aggregate Function is a very useful tool for SQLite3 developers as it helps in finding many things like the MAX value present in a certain column, which might be very useful to know for certain tables and databases. Other than that the MAX function can work together smoothly with other Aggregate Functions like HAVING, GROUP BY etc. which makes it more powerful in finding and returning more accurate results based on certain conditions....

Contact Us