SQL – Operators
Comparison Operators
= < > <= >= <> != !< !>
Arithmetic Operators
+ - * / %
Logical Operators
AND OR IN BETWEEN LIKE
IS NULL NOT
SQL – Comparison Operators
Comparison Operators help define whether a condition between
two fields or functions of fields is true or false
= Equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
<> != Not equal to
!> Not greater than
!< Not less than
WHERE FIELD_A <= FIELD_B
HAVING SUM(FIELD_C) != 100
SQL – Arithmetic Operators
Arithmetic Operators execute common arithmetic functions
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder of x/y)
WHERE FIELD_A + FIELD_B = FIELD_C
HAVING SUM(FIELD_D) – 100 > SUM(FIELD_E)/2
WHERE (FIELD_A + FIELD_B) / 10 < 50
SELECT FIELD_A + FIELD_B AS FIELD_N
SELECT GROUP_FIELD, SUM(FIELD_C) - SUM(FIELD_D) AS FIELD_N
SQL – Logical Operators
Logical Operators define specific logical conditions in queries
AND All conditions in a statement are true
OR At least one condition in a statement is true
AND OR
WHERE FIELD_A < 10 AND FIELD_B > 100
HAVING SUM(FIELD_C) = 50 OR MIN(FIELD_D) >= 5
SQL – Logical Operators
Logical Operators define specific logical conditions in queries
IN Whether a field or expression value is in a specific
list of possible values
• Shorthand for a long list of OR conditions
WHERE FIELD_A = 'AAA' OR FIELD_A = 'BBB' OR FIELD_A = 'CCC'
WHERE FIELD_A IN ('AAA', 'BBB', 'CCC')
BETWEEN Whether a field value or expression is between two
other values or expressions
• Shorthand for a compound AND condition
WHERE FIELD_A >= 10 AND FIELD_A <= 100
WHERE FIELD_A BETWEEN 10 AND 100
SQL – Logical Operators
Logical Operators define specific logical conditions in queries
LIKE Returns true if a specific set of characters is present
in a string or text value
• Uses wildcards:
% Any string of zero or more characters
_ Any single character
WHERE FIELD_A LIKE 'abc%'
• True for any string that starts with ‘abc’ regardless of how long that string is
WHERE FIELD_A LIKE 'abc_'
• True only if the string starts with ‘abc’ and is four characters long
WHERE FIELD_A LIKE '%abc%'
• True if ‘abc’ appears anywhere in the string
SQL – Logical Operators
Logical Operators define specific logical conditions in queries
IS NULL Returns true if a value or expression is NULL
WHERE FIELD_A IS NULL
NOT Reverses the logical meaning of other operators
AND OR NOT AND
WHERE NOT (FIELD_A < 10 AND FIELD_B > 100)
WHERE FIELD_A NOT LIKE '%abc%'
WHERE FIELD_A IS NOT NULL
Recap
Comparison Operators
Arithmetic Operators
Logical Operators