The MAX()
function is used with the WHERE
clause to gain further insights from our data. In SQL, the MAX()
function computes the highest or maximum value of numeric values in a column.
SELECT MAX(column_name)
FROM table_name
WHERE condition;
Let’s assume we have a Person
table with columns such as name
, salary
, state
, and gender
.
We want to get the maximum salary paid in Lagos.
The following code shows how to use the MAX()
function with the WHERE
clause in SQL:
CREATE TABLE Person (ID int,name varchar(100),salary int,gender varchar(10),state varchar(15));-- Insert dataINSERT INTO PersonVALUES (1,'Sharon Peller',40000,'Female','Kogi');INSERT INTO PersonVALUES (2,'Paul Dons',150000,'Male','Lagos');INSERT INTO PersonVALUES (3,'Ameera Abedayo',200000,'Female','Imo');INSERT INTO PersonVALUES (4,'Maria Asaad',320000,'Female','Lagos');INSERT INTO PersonVALUES (5,'David Hassan',250000,'Male','Abuja');INSERT INTO PersonVALUES (6,'Niniola Disu',80000,'Female','Lagos');INSERT INTO PersonVALUES (7,'Praise Dominion',340000,'Female','Lagos');INSERT INTO PersonVALUES (7,'Divine Favour',280000,'Female','Abuja');INSERT INTO PersonVALUES (7,'Praise Dominion',100000,'Female','Lagos');INSERT INTO PersonVALUES (8,'Joe Smith',75000,'Lagos');-- QuerySELECT MAX(salary)FROM PersonWHERE state = 'Lagos';
Person
table with the columns: id
, name
, salary
, gender
, and state
.Person
table.MAX()
function with the WHERE()
clause.