Quantcast
Channel: Welcome To TechBrothersIT
Viewing all articles
Browse latest Browse all 1974

How to Get Max and Mix values from a Table by using Aggregate Function - SQL Server / TSQL Tutorial Part 129

$
0
0

Scenario:

Let's say you are working as SQL Server Developer, you have dbo.Customer table with SaleAmount. You are asked to write a query that should return Max SaleAmount and Min SaleAmount from the dbo.Customer table.


Solution:

You can use Max and Min aggregate functions in SQL Server to find the Maximum and Minimum values.

Let's create dbo.Customer Table with sample data so we can use Max and Min functions.


Createtable dbo.Customer
(Id int,
FName VARCHAR(50),
LName VARCHAR(50),
CountryShortName CHAR(2),
SaleAmount Int)
GO
--Insert Rows in dbo.Customer Table
insertinto dbo.Customer
Values (
1,'Raza','M','PK',10),
(2,'Rita','John','US',12),
(3,'Sukhi','Singh',Null,25),
(4,'James','Smith','CA',60),
(5,'Robert','Ladson','US',54),
(6,'Alice','John','US',87),
(6,'John',Null,'US',Null)
Let's run the below query with Max and Min functions to finder the answer of our question.

SELECTMax(saleamount) AS MaxSaleAmount, 
Min(saleamount) AS MinSaleAmount
FROM dbo.customer

How to use Max and Min function in SQL Server




Viewing all articles
Browse latest Browse all 1974

Trending Articles