SQL Exercises: Any salesman was matched to the city of any customer
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
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 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.
Go to:
PREV : Salesmen who do not have customers in their cities.
NEXT : Ratings of all customers with a comment string.
Practice Online
Query Visualization:
Duration:
Rows:
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.
