2

I have a created a table named city in my database. This table has 2 columns, called 'name' and 'country'. I have created a query that returns the combinations of cities from different countries which is below:

SELECT c1.name, c1.country, c2.name, c2.country 
FROM city c1, city c2
WHERE c1.country != c2.country 

This query works, but however the city pairs are repeated ie. I get results with:

    Berlin Germany London England 
London England Berlin Germany

which means that the city pair berlin/hamburg is repeated in my result set. Is there a way around this?

2
  • 1
    Why do you have city c3 in the query at all? Commented Aug 6, 2012 at 5:23
  • Question has been adjusted. Thanks for that. Commented Aug 6, 2012 at 5:23

3 Answers 3

3
SELECT c1.name, c1.country, c2.name, c2.country 
FROM city c1, city c2
WHERE c1.country < c2.country
Sign up to request clarification or add additional context in comments.

4 Comments

shouldn't that be c1.name < c2.name? That way cities in the same country will show up ... and cities in separate countries won't show up twice.
His original SQL says country. In any case, the point of the solution is that for whatever column you want to be different, use < instead of !=. This ensures that you get just one row for each pair rather than 2. Going by the example output he gave, I suspect what he needs is c1.country = c2.country AND c1.name < c2.name.
he's also complaining that the cities show up twice. once in the c1 table and once in the c2 table. by forcing the name in c1 to be smaller than the name in c2 a repeat is prevented.... I see now you realised that too. Why don't you add that to your answer?
Thanks guys. My original question was incorrect. I wanted the cities to come from different countries so Barmars answer was correct. However I learnt from all answers so thanks for your help.
0

You can do this by using LEFT JOIN see Visual Explanation Of Joins try this:

SELECT c1.name, c1.country, c2.name, c2.country 
FROM city c1
     LEFT JOIN city c2
         ON c1.country = c2.country 
            AND c1.name = c2.name
WHERE c1.country IS NULL OR 
      c2.country IS NULL;

Comments

-1

A good database will never have two or more same records in a single table. So my recommendation is two make 2 tables:

cities: ID, country_ID, Name
countries: ID, Name

and the select should be:

SELECT c.Name, co.Name
FROM cities c
INNER JOIN countries co
    ON co.ID=c.country_ID 

3 Comments

What makes you think he has duplicate records in his table? The duplication only comes when he does the self-join with a != condition, because you get pairs of rows where A!=B or B!=A. That's why changing the condition to < solves it.
I didn't said about what the used query returns. I said that in his cities table he store New York, USA; Los Angeles, USA where USA is a duplicate entry in this row and maybe in others, which is not correctly entry for a strong database. So he should store just unique city names having relation with the id of the country.
So you weren't actually answering his question, just making a general suggestion about his database design. That belongs in a comment, not an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.