w3resource

SQL Exercises: Find the salesmen who have multiple customers

SQL SUBQUERY: Exercise-16 with Solution

16. From the following tables write a SQL query to find salespeople who deal with multiple customers. Return salesman_id, name, city and commission.

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 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 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 'salesman_id' is in the result of a subquery
WHERE salesman_id IN (
   -- Subquery: Selecting distinct 'salesman_id' from the 'customer' table (aliased as 'a')
   SELECT DISTINCT salesman_id 
   -- Specifying the table to retrieve data from ('customer' as 'a')
   FROM customer a 
   -- Checking the existence of records in a subquery
   WHERE EXISTS (
      -- Subquery: Selecting any record from the 'customer' table (aliased as 'b') where 'salesman_id' matches the outer query's 'salesman_id'
      SELECT * 
      -- Specifying the table to retrieve data from ('customer' as 'b')
      FROM customer b 
      -- Filtering the results based on conditions related to the outer query's 'salesman_id' and 'cust_name'
      WHERE b.salesman_id = a.salesman_id 
      AND b.cust_name <> a.cust_name
   )
);

Output of the Query:

salesman_id	name		city	commission
5001		James Hoog	New York	0.15
5002		Nail Knite	Paris		0.13

Explanation:

The said SQL query that selects all columns from the 'salesman' table where the "salesman_id" column appears in a subquery. The subquery selects the distinct "salesman_id" values from the 'customer' table (aliased as 'a') where there exists at least one record in the same 'customer' table (aliased as 'b') where the "salesman_id" value in 'b' is equal to the "salesman_id" value in 'a' and the "cust_name" value in 'b' is not equal to the "cust_name" value in 'a'. In other words, it's retrieving all the rows from the "salesman" table where the salesman has at least one customer with a different name than any other customer he has. It is using the IN clause which is used to filter the rows from the table based on the values returned by the subquery.

Visual Explanation:

SQL Subqueries:  Find the salesmen who have multiple customers.

Practice Online


Sample Database: inventory

Inventory database model

Query Visualization:

Duration:

Query visualization of Find the salesmen who have multiple customers - Duration

Rows:

Query visualization of Find the salesmen who have multiple customers - Rows

Cost:

Query visualization of Find the salesmen who have multiple customers - Cost

Contribute your code and comments through Disqus.

Previous SQL Exercise: Find out if any of the customers are located in London.
Next SQL Exercise: Find all the salesmen worked for only one customer.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.