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

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.


Create table dbo.Customer
 (Id int,
  FName VARCHAR(50),
  LName VARCHAR(50),
  CountryShortName CHAR(2),
  SaleAmount Int)
GO
--Insert Rows in dbo.Customer Table
insert into 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.

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

How to use Max and Min function in SQL Server




2 comments:

  1. We have Provided Chapter wise Nagaland 10th Books 2022 Pdf of Hindi, English, Maths and Science etc Available Here, A good NBSE 10th Textbook is very important because it serves as a Guide to the Exam Pattern. Nagaland HSLC Textbook 2023 Students can Download the NBSE HSLC Textbooks 2022 Pdf Format Online From the official website, The Nagaland Board Announced the Books which will have topics from which Questions will be Framed for the exam. Students must Prepare Themselves as per the Textbook as it gives a complete idea of topics. Read the content below to get detailed information about NBSE HSLC Textbook 2022. Download Nagaland 10th Books Pdf Here.

    ReplyDelete