w3resource

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. From the following tables, create a view to find the salesperson who handles a customer who makes the highest order of a day. Return order date, salesperson ID, name.

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: From the following tables, create a view to get the salesperson and customer by name. Return order name, purchase amount, salesperson ID, name, customer name.
Next: From the following tables, create a view to find the salesperson who handles the customer with the highest order, at least 3 times on a day. Return salesperson ID and name.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



SQL: Tips of the Day

MySQL export schema without data

mysqldump -h yourhostnameorIP -u root -p --no-data dbname > schema.sql

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