w3resource

SQL Exercises: Highest purchase amount on a date for each salesman

SQL Aggregate Functions: Exercise-11 with Solution

From the following table, write a SQL query to determine the highest purchase amount made by each salesperson on '2012-08-17'. Return salesperson ID, purchase amount.

Sample table: orders


Sample Solution :

SELECT salesman_id,MAX(purch_amt) 
FROM orders 
WHERE ord_date = '2012-08-17' 
GROUP BY salesman_id;

Output of the Query:

salesman_id	max
5003		110.50
5007		75.29

Code Explanation:

The said SQL query retrieves the salesman id and the maximum purchase amount for orders that have an order date of '2012-08-17' in the 'orders' table. The result will be grouped by salesman id, and only the maximum purchase amount will be returned for each group.

Relational Algebra Expression:

Relational Algebra Expression: Highest purchase amount on a particular date for individual salesman.

Relational Algebra Tree:

Relational Algebra Tree: Highest purchase amount on a particular date for individual salesman.

Explanation:

Syntax of Highest purchase amount on a particular date for individual salesman

Visual presentation :

Highest purchase amount on a particular date for individual salesman

Practice Online


Query Visualization:

Duration:

Query visualization of Highest purchase amount on a particular date for individual salesman - Duration

Rows:

Query visualization of Highest purchase amount on a particular date for individual salesman - Rows

Cost:

Query visualization of Highest purchase amount on a particular date for individual salesman - Cost

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

Previous SQL Exercise: Find the highest purchase amount each customer ordered.
Next SQL Exercise: Highest purchase amount of customers on a given 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