﻿ SQL - Largest and smallest orders on each date

# SQL Exercises: Largest and smallest orders on each date

## SQL UNION: Exercise-5 with Solution

5. From the following tables, write a SQL query to find the salespeople who generated the largest and smallest orders on each date. Sort the result-set on third field. 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))
ORDER BY 3
``````

Sample Output:

```salesman_id	name		ord_no	?column?	ord_date
5002		Nail Knite	70001	highest on	2012-10-05
5001		James Hoog	70002	lowest on	2012-10-05
5003		Lauson Hen	70003	highest on	2012-10-10
5003		Lauson Hen	70004	highest on	2012-08-17
5001		James Hoog	70005	lowest on	2012-07-27
5001		James Hoog	70005	highest on	2012-07-27
5001		James Hoog	70008	highest on	2012-09-10
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
5002		Nail Knite	70012	lowest on	2012-06-27
5002		Nail Knite	70012	highest on	2012-06-27
5001		James Hoog	70013	lowest on	2012-04-25
5001		James Hoog	70013	highest on	2012-04-25
```

Code Explanation:

The said query in SQL that retrieves information about the highest and lowest orders placed by each salesman, along with the date of the order.
The first query before UNION keyword selects the salesman ID, name, order number, and order date for the highest order placed by each salesman. It does so by joining the 'salesman' and 'orders' tables and selecting rows where the salesman ID matches in both tables, and the purchase amount is equal to the maximum purchase amount for that date, as determined by a subquery.
The second query after UNION keyword selects the lowest order for each salesman.
The "UNION" keyword combines the results of the two queries into a single table.
The ORDER BY clause orders the results by the third column, which is the order number.

## Query Visualization:

Duration:

Rows:

Cost:

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

Previous SQl Exercise: Largest and smallest orders on each date.
Next SQL Exercise: Salesmen who do not have customers in their cities.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## 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