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:

SELECT a.salesman_id, name, a.city, 'MATCHED'
FROM salesman a, customer b
WHERE a.city = b.city
UNION
(SELECT salesman_id, name, city, 'NO MATCH'
FROM salesman
WHERE NOT city = ANY
	(SELECT city
        FROM customer))
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.

SQL: Tips of the Day

Grouped LIMIT in PostgreSQL: Show the first N rows for each group?

db=# SELECT * FROM xxx;
 id | section_id | name
----+------------+------
  1 |          1 | A
  2 |          1 | B
  3 |          1 | C
  4 |          1 | D
  5 |          2 | E
  6 |          2 | F
  7 |          3 | G
  8 |          2 | H
(8 rows)

I need the first 2 rows (ordered by name) for each section_id, i.e. a result similar to:

id | section_id | name
----+------------+------
  1 |          1 | A
  2 |          1 | B
  5 |          2 | E
  6 |          2 | F
  7 |          3 | G
(5 rows)

PostgreSQL v9.3 you can do a lateral join

select distinct t_outer.section_id, t_top.id, t_top.name from t t_outer
join lateral (
    select * from t t_inner
    where t_inner.section_id = t_outer.section_id
    order by t_inner.name
    limit 2
) t_top on true
order by t_outer.section_id;

Database: PostgreSQL

Ref: https://bit.ly/3AfYwZI

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook