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:

Visual presentation:

Practice Online
Query Visualization:
Duration:

Rows:

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.
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
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
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