w3resource

SQL Exercises: Any salesman was matched to the city of any customer

SQL UNION : Exercise-7 with Solution

7. From the following tables, write a SQL query that appends strings to the selected fields, indicating whether the city of any salesperson is matched with the city of any customer. Return salesperson ID, name, city, MATCHED/NO MATCH.

Sample table: Salesman


Sample table: Customer


Sample Solution:

-- Selecting specific columns (a.salesman_id, name, a.city, 'MATCHED') from the 'salesman' table (aliased as 'a') and the 'customer' table (aliased as 'b')
SELECT a.salesman_id, name, a.city, 'MATCHED'

-- Joining the 'salesman' table (aliased as 'a') and the 'customer' table (aliased as 'b') based on the condition that the 'city' values are equal in both tables
FROM salesman a, customer b
WHERE a.city = b.city

-- Performing a UNION operation with the result set of a subquery that selects specific columns (salesman_id, name, city, 'NO MATCH') from the 'salesman' table
UNION

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

-- Filtering rows in the 'salesman' table where the 'city' is not equal to any 'city' value in the 'customer' table
FROM salesman
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		city		?column?
5005		Pit Alex	London		MATCHED
5007		Paul Adam	Rome		NO MATCH
5002		Nail Knite	Paris		MATCHED
5006		Mc Lyon		Paris		MATCHED
5003		Lauson Hen	San Jose	NO MATCH
5001		James Hoog	New York	MATCHED

Code Explanation:

The said query in that retrieves information about salesmen and their cities, and whether or not they have customers in the same city.
The result set includes salesmen who have customers in their city, as well as salesmen who do not have customers in any city. The result set is sorted by the salesmen's names in descending order.
The uses of JOIN clause between the salesman and customer tables retrieve all salesmen who have customers in the same city. The SELECT statement also adds a column with the value 'MATCHED' to indicate that these salesmen have a match with a customer.
The second part of the query uses a subquery to retrieve all salesmen who do not have customers in any city. The SELECT statement also adds a column with the value 'NO MATCH' to indicate that these salesmen do not have a match with a customer.
The UNION operator combines the results of the two queries into a single result set.
The ORDER BY clause sorts the result set by the second column in descending order i.e., by the salesmen's names.

Practice Online


Inventory database model

Query Visualization:

Duration:

Query visualization of Appends strings to the selected fields, indicating whether or not a specified salesman was matched to a customer in his city - Duration

Rows:

Query visualization of Appends strings to the selected fields, indicating whether or not a specified salesman was matched to a customer in his city - Rows

Cost:

Query visualization of Appends strings to the selected fields, indicating whether or not a specified salesman was matched to a customer in his city - Cost

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

Previous SQL Exercise: Salesmen who do not have customers in their cities.
Next SQL Exercise: Ratings of all customers with a comment string.

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.