sql max function

The SQL max function is used to extract the largest value from a table’s column. This function returns the maximum value of the column. We have some excellent examples of SQL max function from which you will know how to use max in SQL.

SQL Max Function

/ ***** The largest value(salary) will come ***** /

SELECT MAX(salary)
FROM Employees; 

Output: 9800


/ ***** The largest salary that falls between employeeID  1 and 4 ***** /

SELECT MAX(salary) AS TopSalary
FROM Employees
WHERE employeeID BETWEEN 1 AND 4;

Output: 5600


/ ***** The largest age coming above employeeID 5 ***** /

SELECT MAX(age) AS Age
FROM Employees
WHERE employeeID >=5;

Output: 100


/ ***** Highest salary out of 3 Employees ***** /

SELECT MAX(salary) AS Salary
FROM Employees
WHERE name IN ('hij', 'qrs', 'wxy');

Output: 9800


/ ***** The highest salary of one whose name starts with t ***** /

SELECT MAX(salary) AS Salary
FROM Employees
WHERE name LIKE 't%';

Outout: 8000

Related: 8 great examples of applying SQL limit

EmployeeIDNameAgeSalaryJoinDate
1abc20200019-2-2020
2efg30540010-2-2019
3hij40560016-4-20015
4klm50360010-5-2004
5nop60240015-2-2009
6qrs70570010-6-2018
7tuv80800025-12-2016
8wxy90980010-7-2005
9zzz100520030-9-2014

Find Highest Salary Of Employee

If you want to extract the salary of an Employee from the database, then you have to follow the example given below.

/ ***** Name of the Employee who has the highest salary ***** /

SELECT name, MAX(salary) AS Salary 
FROM Employees 
WHERE (SELECT MAX(salary) FROM Employees);

Find Second Highest Salary

If you want to extract the second highest salary, then you have to see the example given below.

/ ***** Second highest salary ***** /

SELECT name, MAX(salary) AS Salary 
FROM Employees 
WHERE salary <(SELECT MAX(salary) FROM Employees);

Get Highest Value Without Max

If you want to extract the largest values without the SQL max function, then for that you have to use the ORDER BY and LIMIT keywords.

/ ***** Three highest salaries ***** /

SELECT name, salary
FROM Employees 
ORDER BY salary DESC
LIMIT 3;

Get Lowest Value Without Max

/ ***** Three lowest salaries ***** /

SELECT name, salary
FROM Employees 
ORDER BY salary ASC
LIMIT 3;

SQL String Functions

We have some of the most commonly used String Functions of SQL available. To explain to you better, we have explained below how to string functions works.

No.FunctionsDescription
1CONCATCombine two strings together
2DIFFERENCEMatch two values
3LOWERUsed to convert a string to lowercase
4UPPERUsed to convert a string to uppercase
5FORMATUsed to apply special format
6TRIMExcludes spaces from a string
7LENTo check the length of string
8REPLACEUsed to replace string with a new substring
9STRReplaces a number as a string
10REVERSEIt reverses the string

Related: 25 important examples of SQL where clause

Related: 20 best examples of SQL like operator