20

I have to display the countries that are big by area or big by population but not both and Show name, population and area. Basically it's a XOR operation if i am not wrong.

A country is big if it has an area of more than 3 million sq km or it has a population of more than 250 million.

I have tried this

SELECT name, population, area
FROM world
WHERE (area > 30000000 | population > 25000000) &
      (area < 30000000 & population < 25000000)

I am trying this on sqlzoo.net - SELECT_from_WORLD_Tutorial: Q.No-8. Please select the SQL Engine to SQLSERVER.

Ashish
  • 211
  • 1
  • 2
  • 6

8 Answers8

22

You can implement a XOR like this - don't forget that the question will require you to use <= to correctly use the XOR operator:

SELECT name
    , population
    , area
FROM world
WHERE (area > 3000000 AND population <= 250000000)
OR (area <= 3000000 AND population > 250000000)
Community
  • 1
  • 1
Radu Gheorghiu
  • 20,049
  • 16
  • 72
  • 107
  • This query has a problem. It will ignore areas which match exactly 3 million km^2 or populations with exactly 2.5 million. – Tim Biegeleisen Dec 08 '16 at 07:56
  • @TimBiegeleisen, I merely edited the logic in the `OP's` example to get the `XOR` logic, which is why I left the area and population comparison as in the original post (`area > 30000000 | population > 25000000`). – Radu Gheorghiu Dec 08 '16 at 08:02
  • @Radu Gheorghiu, Thanks for the suggestion but it does not produce the required table. – Ashish Dec 08 '16 at 09:04
  • @Ashish Why is that? Can you post some sample data and expected results? Also, can you explain on the test data why it isn't returning the correct data? – Radu Gheorghiu Dec 08 '16 at 09:05
  • @RaduGheorghiu, I have tried multiple things but none are working, the link to this question is here http://sqlzoo.net/wiki/SELECT_from_WORLD_Tutorial , Q No 8,kindly select the SQLEngine to SQL Server before attempting. – Ashish Dec 08 '16 at 09:10
  • @Ashish Just to point out, you are comparing with the wrong numbers (and **my mistake** for copy-pasting them from your question into my answer instead of writing them myself). Your conditions are comparing the area with `30.000.000` (not 3 million) and the population with `25.000.000` (not 250 million), so you're missing and adding some zeros. Just use this query and you'll get the "happy face" on sqlzoo. – Radu Gheorghiu Dec 08 '16 at 09:21
  • 1
    Thanks @Radu, my bad, will look closer at nos in future.An important lesson indeed. – Ashish Dec 08 '16 at 09:45
6
SELECT name,
       population,
       area
FROM world
WHERE (area > 3000000 AND population <= 25000000) OR   -- big area, small population
      (area <= 3000000 AND population > 25000000)      -- small area, big population

Note that I used <= to represent the "smaller then" condition. This is to avoid a situation where an area equals 3 million km^2 or a population exactly equals 2.5 million. Using < would eliminate data in this case.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I agree with your `<=` edit, although just pointing out that the question says "**more** than 3 million sq km or it has a population of **more** than 250 million", which would imply a non-inclusive comparison. – Radu Gheorghiu Dec 08 '16 at 07:55
  • Also, the OP's post has a non-inclusive comparison operator. (`area > 30000000 | population > 25000000`) – Radu Gheorghiu Dec 08 '16 at 08:01
5
SELECT name,
       population,
       area
  FROM world
 WHERE (area > 3000000) <> /* XOR */ (population > 25000000)

Is briefer albeit less readable. As a general rule, <> or != is a good replacement for logicial XOR.

David Larochette
  • 1,200
  • 10
  • 18
4
SELECT name,
   population,
   area
FROM world
WHERE (area > 3000000 XOR population > 250000000) 

The question wants you to answer like this. This is the correct and short way of using 'XOR' for this question.

Volkan
  • 41
  • 1
4
SELECT name, population, area
FROM world
WHERE (area > 3000000 OR population > 250000000) AND NOT 
      (area > 3000000 AND population > 250000000)

Imagine this through a Venn diagram where a = area > 3000000, b = population > 250000000

a XOR b would be equal to (a Union b) Minus (a Intersection b)

Harshit Goyal
  • 63
  • 1
  • 6
2
SELECT NAME,
       population,
       area
FROM   world
WHERE  area > 3000000
        OR population > 250000000
EXCEPT
SELECT NAME,
       population,
       area
FROM   world
WHERE  area > 3000000
       AND population > 250000000

Above is the actual representation in set theory.

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
0
Select name, population, area
from world
where (population >= 250000000 AND NOT area >= 3000000) OR 
      (area >= 3000000 AND NOT population >= 250000000);

This will sure give you your desire answer.

-1

The query will be like this:

SELECT name, population, area 
FROM world
WHERE (area > 3000000 AND  population < 250000000) OR
      (area < 3000000 AND  population > 250000000);
Salahuddin Ahmed
  • 4,854
  • 4
  • 14
  • 35