MAX() Function in SQL Server
MAX() :
This function in SQL Server is used to find the value that is maximum in the group of values stated.
Features :
- This function is used to find the maximum value.
- This function comes under Numeric Functions.
- This function accepts only one parameter namely expression.
Syntax :
MAX(expression)
Parameter :
This method accepts one parameter.
- expression –
A Specified numerical value which can either be a field or a given formula.
Returns :
It returns the value that is maximum in the group of values stated.
Example-1 :
Using MAX() function and getting the output.
CREATE TABLE product ( user_id int IDENTITY(100, 2) NOT NULL, product_1 VARCHAR(10), product_2 VARCHAR(10), price int ); INSERT product(product_1, price) VALUES ('rice', 400); INSERT product(product_2, price) VALUES ('grains', 600); SELECT MAX(price) FROM product;
Output :
600
Example-2 :
Using MAX() function and finding the maximum value of the stated float values.
CREATE TABLE floats ( user_id int IDENTITY(100, 2) NOT NULL, float_val float ); INSERT floats(float_val) VALUES (3.5); INSERT floats(float_val) VALUES (2.1); INSERT floats(float_val) VALUES (6.3); INSERT floats(float_val) VALUES (9.0); INSERT floats(float_val) VALUES (7.0); SELECT MAX(float_val) FROM floats;
Output :
9
Example-3 :
Using MAX() function and getting the output where MRP is less than the maximum of all MRP’s.
CREATE TABLE package ( user_id int IDENTITY(100, 4) NOT NULL, item VARCHAR(10), mrp int ); INSERT package(item, mrp) VALUES ('book1', 3); INSERT package(item, mrp) VALUES ('book2', 350); INSERT package(item, mrp) VALUES ('book3', 400); SELECT * FROM package WHERE mrp < (SELECT MAX(mrp) FROM package);
Output :
| user_id | item | mrp -------------------------------- 1 | 100 | book1 | 3 -------------------------------- 2 | 104 | book2 | 350
Example-4 :
Using MAX() function and getting the maximum value of all the (MRP-sales price) values.
CREATE TABLE package ( user_id int IDENTITY(100, 4) NOT NULL, item VARCHAR(10), mrp int, sp int ); INSERT package(item, mrp, sp) VALUES ('book1', 250, 240); INSERT package(item, mrp, sp) VALUES ('book2', 350, 320); INSERT package(item, mrp, sp) VALUES ('book3', 400, 350); SELECT MAX(mrp-sp) FROM package;
Output :
50
Application :
This function is used to find the maximum value of all the values stated.
Contact Us