Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XOR in SQL Server

Tags:

sql-server

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.

like image 712
Ashish Avatar asked Dec 08 '16 07:12

Ashish


People also ask

What does XOR mean in SQL?

MySQL XOR operator checks two operands (or expressions) and returns TRUE if one or the other but not both is TRUE.

Does XOR exist in SQL?

SQL AND OR NOT and XOR statement can be used with WHERE clause to list a set of records with matching combination of a database table.

What is XOR used for?

XOR is a bitwise operator, and it stands for "exclusive or." It performs logical operation. If input bits are the same, then the output will be false(0) else true(1).

What is Bitwise or in SQL?

Bitwise operators perform bit manipulations between two expressions of any of the data types of the integer data type category. Bitwise operators convert two integer values to binary bits, perform the AND, OR, or NOT operation on each bit, producing a result.


1 Answers

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)
like image 70
Radu Gheorghiu Avatar answered Sep 22 '22 19:09

Radu Gheorghiu