Operators | Description |
---|---|
AND && | Logical AND |
OR (||) | Logical OR |
XOR | Logical XOR |
NOT (!) | Logical NOT |
LESS THAN < | less than |
GREATER THAN > | greater than |
LESS THAN OR EQUAL <= | less than or equal to |
GREATER THAN OR EQUAL >= | greater than or equal to |
EQUAL = | equal |
NOT EQUAL <> or != | not equal |
This logical operator AND:
SELECT 1 && 1;
-> 1
SELECT 1 && 0;
-> 0
SELECT 1 && NULL;
-> NULL
SELECT 0 && NULL;
-> 0
SELECT NULL && 0;
-> 0
AND can be used to select rows that satisfy all the conditions given in a statement. For Example, to find the names of the students between the age 20 to 25 years, the query would be like:
SELECT first_name, last_name, age
FROM user_details
WHERE age >= 20 AND age <= 25;
The output would be:
first_name | last_name | age |
---|---|---|
Mary | Bean | 20 |
Henry | Catson | 22 |
Sheila | Donaldson | 25 |
The logical “AND” operator selects rows only if the data in all relevant columns is satisfied. In this case, ‘first_name’ and ‘last_name’ simply need to have non-NULL values, and ‘age’ needs to be a value between 20 and 25.
This logical operator OR:
SELECT 1 || 1;
-> 1
SELECT 1 || 0;
-> 1
SELECT 0 || 0;
-> 0
SELECT 1 || NULL;
-> 1
SELECT 0 || NULL;
-> NULL
In other words, OR is used to select rows that satisfy at least one of the given conditions.
For example, the following query could be used to find the user_id for people that live in either California or Texas:
SELECT user_id, state
FROM user_location
WHERE state = 'California' OR state = 'Texas';
The result set could be something like:
user_id | city | state |
---|---|---|
608 | Sacremento | California |
844 | Austin | Texas |
917 | Oakland | California |
SELECT 1 XOR NULL;
-> NULL
SELECT 1 XOR 0;
-> 1
SELECT 1 XOR 1 XOR 1;
-> 1
SELECT 1 XOR 1;
-> 0
Note that “a XOR b” is the mathematical equivalent of (a AND (NOT b)) OR ((NOT a) and b).
This logical operator NOT:
SELECT NOT 10;
-> 0
SELECT NOT 0;
-> 1
SELECT NOT NULL;
-> NULL
SELECT ! (1+1);
-> 0
SELECT ! 1+1;
-> 1
If you want to find rows that do not satisfy a condition, you can use the logical operator, NOT. NOT results in the reverse of a condition. That is, if a condition is satisfied, then the row is not returned.
For example: If you want to find out the user_id for people who do not practice medicine as their profession, the query would be like:
SELECT user_id, title, occupation
FROM user_occupations
WHERE NOT occupation = 'Doctor';
The result set would be something like:
user_id | degree | occupation |
---|---|---|
322 | PhD | Professor |
579 | PhD | Writer |
681 | MD | Consultant |
Equal:
SELECT 1 = 0;
-> 0
SELECT '0' = 0;
-> 1
SELECT '0.0' = 0;
-> 1
SELECT '0.01' = 0;
-> 0
SELECT '.01' = 0.01;
-> 1
Not equal:
SELECT '.01' <> '0.01';
-> 1
SELECT .01 <> '0.01';
-> 0
SELECT 'zing' <> 'zingg';
-> 1