w3resource

SQL Exercises: Customer with the highest purchase amount

SQL Aggregate Functions: Exercise-17 with Solution

From the following table, write a SQL query to determine the maximum order (purchase) amount generated by each salesperson. Filter the rows for the salesperson ID is in the range 5003 and 5008 (Begin and end values are included.). Return salesperson id and maximum purchase amount.

Sample table: orders


Sample Solution:

SELECT salesman_id,MAX(purch_amt) 
FROM orders 
GROUP BY salesman_id 
HAVING salesman_id BETWEEN 5003 AND 5008;

Output of the Query:

salesman_id	max
5005		270.65
5003		2480.40
5007		75.29
5006		1983.43

Code Explanation:

In the given SQL query, the salesman_id and the maximum purchase amount (purch_amt) will be retrieved from the 'orders' table for salesmen with an id between 5003 and 5008.
The result will show the maximum purchase amount made by each salesman in the specified range. The "HAVING" clause filters the result set by only showing salesmen whose id is between 5003 and 5008.
The result will show the maximum purchase amount made by each salesman, with the salesman_id being grouped together.

Explanation:

Syntax of highest purchase amount with specied customer ID range

Visual presentation:

Result of highest purchase amount with specied customer ID range

Practice Online


Query Visualization:

Duration:

Query visualization of Find highest purchase amount by the customer from the salesman with a specified ID range - Duration

Rows:

Query visualization of Find highest purchase amount by the customer from the salesman with a specified ID range - Rows

Cost:

Query visualization of Find highest purchase amount by the customer from the salesman with a specified ID range - Cost

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous SQL Exercise: Customer details and purchases over a certain amount.
Next SQL Exercise: Count all orders for a specific date.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

SQL: Tips of the Day

What is the best way to paginate results in SQL Server?

SELECT  *
FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY OrderDate ) AS RowNum, *
          FROM      Orders
          WHERE     OrderDate >= '1980-01-01'
        ) AS RowConstrainedResult
WHERE   RowNum >= 1
    AND RowNum < 20
ORDER BY RowNum

Database: SQL Server

Ref: https://bit.ly/3MGrNlk

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook