VillageSQL is a drop-in replacement for MySQL with extensions.
All examples on this page work on VillageSQL. Install Now →
*
in rental_rate * 1.1. This lesson covers the everyday set, and the one
behavior that catches everybody, which is what happens when NULL turns up.
Open the client with mysql -u root -p sakila before you start.
How an operator works
LEFT_VALUE and RIGHT_VALUE with values and OPERATOR with one of
the symbols below. A value can be a column, or it can be a literal, meaning a
number or a piece of text written directly into the statement.
A SELECT with no FROM evaluates its expressions and returns a single row,
which makes it the quickest way to try an operator out.
Examples
1) Comparison returns 1 or 0
MySQL has no distinct boolean type. A comparison that holds returns the integer1, and one that does not returns 0. The words TRUE and FALSE are
spellings of those 2 integers.
=, not the double == that most programming languages
use. <> and != both mean not equal and behave identically. This tutorial
writes <>.
2) Arithmetic, and which operator binds tighter
2 + 3 * 4 is 14, so multiplication happened first. Brackets override that and
make it 20. / gives a decimal result even between 2 whole numbers, DIV
throws the fraction away, and % gives the remainder.
3) DIV and % bind as tightly as division
- bound tighter than /, the first column would be 3 rather than 8.
4) Operators work on columns
Anything you can do to a literal you can do to a column.NULL is not equal to anything, including NULL
You metNULL in the sorting lesson as the marker for a value that was never
recorded. It behaves unlike any real value here.
NULL = NULL returns NULL rather than 1 or 0. So does NULL <> NULL. Every
operator in example 1 behaves that way against NULL.
<=> is the exception. It compares 2 values and always returns 1 or 0, never
NULL, treating NULL as equal to itself.
The consequence is that = NULL matches nothing, ever. When you need to find
the rows where a value is absent, IS NULL is the test, and
it returns 1 or 0 like an ordinary comparison.
Summary
- Expect
1or0back from a comparison, not true or false. - Use
=for equality and<>for inequality. - Remember that
*,/,DIVand%bind tighter than+and-. - Use
DIVfor whole number division and%for the remainder. - Expect
NULLfrom any ordinary comparison involvingNULL, and use<=>orIS NULLinstead.
See also
- SELECT — where these expressions go
- Column aliases — naming the column a comparison produces
- NULL in MySQL — how NULL behaves across the rest of the language

