w3resource

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

SQL UNION : Exercise-6 with Solution

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


Sample table: Customer


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.

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.

Previous SQL Exercise: Largest and smallest orders on each date.
Next SQL Exercise: Any salesman was matched to the city of any 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.