1
1
Logical and Comparison Operators
2
2
================================
4
========= ==========================
6
========= ==========================
12
<= less than or equal to
13
>= greater than or equal to
16
========= ==========================
4
============================== ================================
6
============================== ================================
7
:ref:`and` && Logical AND
8
:ref:`or` (||) Logical OR
10
:ref:`not` (!) Logical NOT
11
:ref:`less_than` < less than
12
:ref:`greater_than` > greater than
13
:ref:`less_or_equal` <= less than or equal to
14
:ref:`greater_or_equal` >= greater than or equal to
16
:ref:`not_equal` <> or != not equal
17
============================== ================================
25
This logical operator AND:
27
* evaluates to 1 if all operands are nonzero and not NULL
28
* evaluates to 0 if one or more operands are 0
29
* otherwise returns NULL
44
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: ::
46
SELECT first_name, last_name, age
48
WHERE age >= 20 AND age <= 25;
52
+---------------+------------------+-------+
53
|first_name |last_name |age |
54
+===============+==================+=======+
56
+---------------+------------------+-------+
58
+---------------+------------------+-------+
59
|Sheila |Donaldson |25 |
60
+---------------+------------------+-------+
62
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.
69
This logical operator OR:
71
* returns 1 if any operand is nonzero and both operands are non-NULL, and returns 0 otherwise
72
* returns 1 with a NULL operand if the other operand is nonzero, and retunes NULL otherwise
73
* returns NULL if both operands are NULL
88
In other words, OR is used to select rows that satisfy at least one of the given conditions.
90
For example, the following query could be used to find the user_id for people that live in either California or Texas: ::
94
WHERE state = 'California' OR state = 'Texas';
96
The result set could be something like:
98
+---------------+------------------+-----------+
99
|user_id |city |state |
100
+===============+==================+===========+
101
|608 |Sacremento |California |
102
+---------------+------------------+-----------+
103
|844 |Austin |Texas |
104
+---------------+------------------+-----------+
105
|917 |Oakland |California |
106
+---------------+------------------+-----------+
114
* returns NULL if either operand is NULL
115
* evaluates to 1 for non-NULL operands (if an odd number of operands is nonzero)
116
* otherwise 0 is returned
124
SELECT 1 XOR 1 XOR 1;
129
Note that "a XOR b" is the mathematical equivalent of (a AND (NOT b)) OR ((NOT a) and b).
137
This logical operator NOT:
139
* evaluates to 1 if the operand is 0
140
* evaluates to 0 if the operand is nonzero
141
* NOT NULL returns NULL
156
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.
158
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: ::
160
SELECT user_id, title, occupation
161
FROM user_occupations
162
WHERE NOT occupation = 'Doctor';
164
The result set would be something like:
166
+---------------+------------------+--------------+
167
|user_id |degree |occupation |
168
+===============+==================+==============+
169
|322 |PhD |Professor |
170
+---------------+------------------+--------------+
172
+---------------+------------------+--------------+
173
|681 |MD |Consultant |
174
+---------------+------------------+--------------+
203
Less than or equal: ::
209
.. _greater_or_equal:
211
GREATER THAN OR EQUAL
212
----------------------
214
Greater than or equal: ::
245
SELECT '.01' <> '0.01';
247
SELECT .01 <> '0.01';
249
SELECT 'zing' <> 'zingg';