﻿ SQL - View to show the salesmen issued orders on given date

# SQL Exercises: View to show the salesmen issued orders on given date

## SQL VIEW: Exercise-15 with Solution

15. From the following table, create a view to find the salespeople who placed orders on October 10th, 2012. Return all the fields of salesperson.

Sample table: salesman

Sample table: orders

Sample Solution:

``````-- Creating a VIEW named 'salesmanonoct' with all columns from the 'salesman' table
CREATE VIEW salesmanonoct

-- Selecting rows from the 'salesman' table where the 'salesman_id' is present in the result of the subquery
-- The subquery selects 'salesman_id' from the 'orders' table where 'ord_date' is '2012-10-10'
AS SELECT *
FROM salesman
WHERE salesman_id IN
(SELECT salesman_id
FROM orders
WHERE ord_date = '2012-10-10');
``````

output:

```sqlpractice=# SELECT *
sqlpractice-# FROM salesmanonoct;
salesman_id |     name     | city  | commission
-------------+--------------+-------+------------
5006 | Mc Lyon      | Paris |       0.14
5003 | Lauson Hense |       |       0.12
(2 rows)
```

Code Explanation:

The provided statement in SQL creates a new view called salesmanonoct that selects all columns from the salesman table, but only for those salesmen who had orders on October 10th, 2012.
This WHERE clause filters the results by a subquery which selects only those rows where the salesman_id column matches a value that is also found in the salesman_id column of the orders table where the ord_date column equals '2012-10-10'.

Inventory database model:

Contribute your code and comments through Disqus.

Previous SQL Exercise: View to show the number of orders in each day.
Next SQL Exercise: View to show the salesmen issued orders on given dates.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿