w3resource

SQL Exercises: Salesmen who do not have customers in their cities


6. From the following tables, write a SQL query to find those salespeople who live in the same city where the customer lives as well as those who do not have customers in their cities by indicating 'NO MATCH'. Sort the result set on 2nd column (i.e. name) in descending order. Return salesperson ID, name, customer name, 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
        5007 | Paul Adam  | Rome     |       0.13
        5003 | Lauson Hen | San Jose |       0.12

Sample table: Customer

 customer_id |   cust_name    |    city    | grade | salesman_id 
-------------+----------------+------------+-------+-------------
        3002 | Nick Rimando   | New York   |   100 |        5001
        3007 | Brad Davis     | New York   |   200 |        5001
        3005 | Graham Zusi    | California |   200 |        5002
        3008 | Julian Green   | London     |   300 |        5002
        3004 | Fabian Johnson | Paris      |   300 |        5006
        3009 | Geoff Cameron  | Berlin     |   100 |        5003
        3003 | Jozy Altidor   | Moscow     |   200 |        5007
        3001 | Brad Guzan     | London     |       |        5005

Sample Solution:

-- Selecting columns (salesman.salesman_id, name, cust_name, commission) from tables 'salesman' and 'customer'
SELECT salesman.salesman_id, name, cust_name, commission
FROM salesman, customer

-- Filtering rows where the city in the 'salesman' table matches the city in the 'customer' table
WHERE salesman.city = customer.city

-- Performing a UNION operation with the result set of a subquery
UNION

-- Selecting specific columns (salesman_id, name, 'NO MATCH', commission) from the 'salesman' table
SELECT salesman_id, name, 'NO MATCH', commission
FROM salesman

-- Filtering rows where the city in the 'salesman' table is not equal to any city in the 'customer' table
WHERE NOT city = ANY
    (SELECT city
     FROM customer)

-- Ordering the result set based on the second column (name) in descending order
ORDER BY 2 DESC

Sample Output:

salesman_id	name		cust_name		commission
5005		Pit Alex	Julian Green		0.11
5005		Pit Alex	Brad Guzan		0.11
5007		Paul Adam	NO MATCH		0.13
5002		Nail Knite	Fabian Johnson		0.13
5006		Mc Lyon		Fabian Johnson		0.14
5003		Lauson Hen	NO MATCH		0.12
5001		James Hoog	Nick Rimando		0.15
5001		James Hoog	Brad Davis		0.15

code Explanation:

The said query in SQL that retrieves data from the 'salesman' and 'customer' tables, and outputs a list of salespeople and their commissions based on the city where they are located and the city of their customers.
The query first joins the 'salesman' and 'customer' tables based on the condition that the salesman's city matches the customer's city. It then uses the UNION operator to combine this result with another query that selects salespeople who do not have any customers in any city. For these salespeople, the query outputs "NO MATCH" instead of the customer name.
The ORDER BY clause sorted the output by the second column in descending order.

Go to:


PREV : Largest and smallest orders on each date.
NEXT : Any salesman was matched to the city of any customer.


Practice Online



Inventory database model.


Query Visualization:

Duration:

Query visualization of List all the salesmen, and indicate those who do not have customers in their cities, as well as whose who do - Duration.


Rows:

Query visualization of List all the salesmen, and indicate those who do not have customers in their cities, as well as whose who do - Rows.


Cost:

Query visualization of List all the salesmen, and indicate those who do not have customers in their cities, as well as whose who do - Cost.


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

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.