w3resource

SQL Exercises: Largest and smallest orders are produced on each date

SQL UNION: Exercise-4 with Solution

4. From the following table, write a SQL query to find the salespersons who generated the largest and smallest orders on each date. Return salesperson ID, name, order no., highest on/lowest on, order date.

Sample table: Salesman


Sample table: Orders


Sample Solution:

SELECT a.salesman_id, name, ord_no, 'highest on', ord_date
FROM salesman a, orders b
WHERE a.salesman_id =b.salesman_id
AND b.purch_amt=
	(SELECT MAX (purch_amt)
	FROM orders c
	WHERE c.ord_date = b.ord_date)
UNION
(SELECT a.salesman_id, name, ord_no, 'lowest on', ord_date
FROM salesman a, orders b
WHERE a.salesman_id =b.salesman_id
AND b.purch_amt=
	(SELECT MIN (purch_amt)
	FROM orders c
	WHERE c.ord_date = b.ord_date))

Sample Output:

salesman_id	name	   	ord_no	?column?	ord_date
5001		James Hoog	70002	lowest on	2012-10-05
5001		James Hoog	70005	highest on	2012-07-27
5001		James Hoog	70005	lowest on	2012-07-27
5001		James Hoog	70008	highest on	2012-09-10
5001		James Hoog	70013	highest on	2012-04-25
5001		James Hoog	70013	lowest on	2012-04-25
5002		Nail Knite	70001	highest on	2012-10-05
5002		Nail Knite	70012	highest on	2012-06-27
5002		Nail Knite	70012	lowest on	2012-06-27
5003		Lauson Hen	70003	highest on	2012-10-10
5003		Lauson Hen	70004	highest on	2012-08-17
5005		Pit Alex	70009	lowest on	2012-09-10
5006		Mc Lyon		70010	lowest on	2012-10-10
5007		Paul Adam	70011	lowest on	2012-08-17

Code Explanation:

The said query in SQL that selects the salesman_id, name, ord_no, and ord_date for each order where the purchase amount is the highest or lowest for that order date. The query uses a subquery to find the highest or lowest purchase amount for each order date, and then joins the orders and salesman tables using the salesman_id column.
The output of the query will consist of two sets of rows: one for the highest purchase amount and one for the lowest purchase amount for each order date. The 'highest on' and 'lowest on' values are added as literals to indicate which one of the two values it is displaying.
The outer query of the first subquery selects the rows where the purchase amount matches the maximum value.
This UNION keyword combines the results of the first query with the results of the second query.
The outer query of the second subquery selects the rows where the purchase amount matches the minimum value.

Practice Online


Inventory database model

Query Visualization:

Duration:

Query visualization of Make a report of which salesman produce the largest and smallest orders on each date - Duration

Rows:

Query visualization of Make a report of which salesman produce the largest and smallest orders on each date - Rows

Cost:

Query visualization of Make a report of which salesman produce the largest and smallest orders on each date - Cost

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

Previous SQL Exercise: Salesmen, customer involved in inventory management.
Next SQL Exercise: Largest and smallest orders on each 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

Grouped LIMIT in PostgreSQL: Show the first N rows for each group?

db=# SELECT * FROM xxx;
 id | section_id | name
----+------------+------
  1 |          1 | A
  2 |          1 | B
  3 |          1 | C
  4 |          1 | D
  5 |          2 | E
  6 |          2 | F
  7 |          3 | G
  8 |          2 | H
(8 rows)

I need the first 2 rows (ordered by name) for each section_id, i.e. a result similar to:

id | section_id | name
----+------------+------
  1 |          1 | A
  2 |          1 | B
  5 |          2 | E
  6 |          2 | F
  7 |          3 | G
(5 rows)

PostgreSQL v9.3 you can do a lateral join

select distinct t_outer.section_id, t_top.id, t_top.name from t t_outer
join lateral (
    select * from t t_inner
    where t_inner.section_id = t_outer.section_id
    order by t_inner.name
    limit 2
) t_top on true
order by t_outer.section_id;

Database: PostgreSQL

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

 





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