E
E
EvaEvochka2020-05-07 22:34:52
SQL
EvaEvochka, 2020-05-07 22:34:52

How to create all possible pairs of cities?

Good afternoon! There is a table with city names. How to write a query that displays all the possible number of pairs that these cities can create with each other?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2020-05-07
@EvaEvochka

JOIN

F
Frowsy, 2020-05-08
@Frowsy

If you are interested in combinations of n elements by k=2, then, for example, the cities table, in which city names are in city_name, the solution is
SELECT t1.city_name, t2.city_name FROM cities t1 CROSS JOIN cities t2 WHERE t1.city_name < t2. city_name;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question