w3resource

SQL Exercises: Display the salesmen with customers following them

SQL SUBQUERY: Exercise-20 with Solution

20. From the following tables write a SQL query to find salespeople whose place of residence matches any city where customers live. Return salesman_id, name, city and commission.

Sample table: Salesman
salesman_id  name        city        commission
-----------  ----------  ----------  ----------
5001         James Hoog  New York    0.15
5002         Nail Knite  Paris       0.13
5005         Pit Alex    London      0.11
5006         Mc Lyon     Paris       0.14
5003         Lauson Hen  San Jose    0.12
5007         Paul Adam   Rome        0.13
Sample table : Customer
customer_id  cust_name     city        grade       salesman_id
-----------  ------------  ----------  ----------  -----------
3002         Nick Rimando  New York    100         5001
3005         Graham Zusi   California  200         5002
3001         Brad Guzan    London      100         5005
3004         Fabian Johns  Paris       300         5006
3007         Brad Davis    New York    200         5001
3009         Geoff Camero  Berlin      100         5003
3008         Julian Green  London      300         5002
3003         Jozy Altidor  Moncow      200         5007

Sample Solution:

-- Selecting all columns from the 'salesman' table
SELECT *
-- Specifying the table to retrieve data from ('salesman')
FROM salesman 
-- Filtering the results based on the condition that 'city' is in the set of values returned by a subquery
WHERE city IN
    -- Subquery: Selecting 'city' values from the 'customer' table
    (SELECT city
     FROM customer);

Output of the Query:

salesman_id	name		city		commission
5001		James Hoog	New York	0.15
5002		Nail Knite	Paris		0.13
5005		Pit Alex	London		0.11
5006		Mc Lyon		Paris		0.14

Explanation:

The said SQL query that selects all columns from the 'salesman' table where the "city" value of the salesman table is present in the list of "city" column from the 'customer' table. The subquery retrieves all the cities from the customer table, and the main query retrieves the rows from the salesman table where the city of the salesman matches any of the values in the subquery. The IN keyword is used to check if the value in the "city" column of the 'salesman' table is present in the list of values returned by the subquery. This query will return all the rows from the salesman table for the salesman who live in the same city as any of the customers.

Visual Explanation:

SQL Subqueries: Display the salesmen for whom there are customers that follow them.

Practice Online


Sample Database: inventory

Inventory database model

Query Visualization:

Duration:

Query visualization of Display the salesmen for whom there are customers that follow them - Duration

Rows:

Query visualization of Display the salesmen for whom there are customers that follow them - Rows

Cost:

Query visualization of Display the salesmen for whom there are customers that follow them - Cost

Contribute your code and comments through Disqus.

Previous SQL Exercise: Find salespeople in the city where the customer lives.
Next SQL Exercise: Alphabetically list the salesmen below their customers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/sql-exercises/subqueries/sql-subqueries-inventory-exercise-20.php