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




5 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
  2. The cost of in vitro fertilization (IVF) in Delhi can vary depending on several factors. One of the main factors that can affect the cost is the clinic or fertility center you choose. Different clinics may have different pricing structures based on their location, facilities, expertise, and success rates. It is important to research and compare different clinics to find one that offers the best value for your specific needs.

    In addition to the clinic, the IVF cost in Delhi can also be influenced by the specific treatment plan recommended by the doctor. The complexity of the case, the number of IVF cycles required, and any additional procedures or medications needed can all impact the overall cost. Some clinics may offer package deals that include multiple cycles or additional services, while others may charge separately for each step of the process.

    ReplyDelete
  3. To get the maximum and minimum values from a table using aggregate functions, SQL provides the MAX() and MIN() functions. These are used in a SELECT statement to return the highest and lowest values of a specified column. For instance, if you're working with a "Scores" table, the query SELECT MAX(score), MIN(score) FROM Scores; will fetch the highest and lowest scores. By utilizing these functions, analyzing data becomes straightforward and efficient, offering insights into trends or outliers.
    Looking to level up your maths skills? Explore resources, practise consistently, and maths practise hit the button to unlock your full potential. With dedication, there's always a chance to achieve greatness!

    ReplyDelete