Which pairs of customers from the same city will a SELF JOIN return?
Find every pair of distinct customers who live in the same city. A customer can appear in several pairs if multiple other customers live in that city. Which rows will the query return?
Predict the resultJOIN and row countsJuniorAnalystBackendQA
Input data · iv_customers
| id | name | city |
|---|---|---|
| 1 | Alice | Berlin |
| 2 | Bob | Paris |
| 3 | Carol | NULL |
| 4 | Dan | Berlin |
| 5 | Eve | Paris |
Query
SELECT c1.name AS customer_1,
c2.name AS customer_2
FROM iv_customers c1
JOIN iv_customers c2 ON c1.city = c2.city
WHERE c1.id < c2.id
ORDER BY c1.id, c2.id