-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_union_ex6.sql
More file actions
28 lines (20 loc) · 853 Bytes
/
Copy pathsql_union_ex6.sql
File metadata and controls
28 lines (20 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-- 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.
-- First attemtp:
SELECT s.salesman_id, s.name, c.cust_name, s.commission
FROM salesman s, customer c
UNION
SELECT s.salesman_id, s.name, c.cust_name, s.commission
FROM salesman s, customer c
ORDER BY 2;
-- Second attempt:
SELECT a.salesman_id, a.name, b.cust_name, a.commission
FROM salesman a, customer b
WHERE a.city = b.city
UNION
SELECT a.salesman_id, a.name, 'NO MATCH', a.commission
FROM salesman a, customer b
WHERE NOT a.city = ANY
(SELECT c.city
FROM customer c
WHERE b.city = c.city)
ORDER BY 2 DESC;