w3resource
SQL exercises

SQL VIEW Exercises: Define a view that find the salesman who has the customer with the highest order of a day

SQL VIEW: Exercise-7 with Solution

7. Write a query to create a view that finds the salesman who has the customer with the highest order of a day.

Sample table: customer


Sample table: salesman


Sample table: orders


Sample Solution:

CREATE VIEW elitsalesman
AS SELECT b.ord_date, a.salesman_id, a.name
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);

output:

sqlpractice=# SELECT *
sqlpractice-# FROM elitsalesman;
  ord_date  | salesman_id |     name
------------+-------------+--------------
 2012-08-17 |        5003 | Lauson Hense
 2012-07-27 |        5001 | James Hoog 
 2012-09-10 |        5001 | James Hoog 
 2012-10-10 |        5003 | Lauson Hense
 2012-06-27 |        5002 | Nail Knite
 2012-04-25 |        5001 | James Hoog 
 2012-10-05 |        5002 | Nail Knite
 2012-09-22 |        5006 | Mc Lyon
(8 rows)

Inventory database model:

Inventory database model

Contribute your code and comments through Disqus.

Previous: Write a query to create a view that shows for each order the salesman and customer by name.
Next: Write a query to create a view that finds the salesman who has the customer with the highest order at least 3 times on a day.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming