MySQL Comments and Operators
This lesson collects two small but everyday topics: how to write comments in SQL, and the operators MySQL offers for arithmetic, comparison and logic.
Comments
MySQL understands three comment styles:
| Style | Example | Notes |
|---|---|---|
-- |
-- a comment |
to end of line; the -- must be followed by a space (or tab/newline) |
# |
# a comment |
to end of line; MySQL-specific |
/* */ |
/* a comment */ |
can span lines or sit in the middle of a statement |
-- List the bakery products
SELECT name, # the product name
unit_price /* current price */
FROM products
WHERE category_id = 2 -- Bakery
/* AND discontinued = 0 */;+------------------+------------+
| name | unit_price |
+------------------+------------+
| Sourdough Loaf | 4.80 |
| Butter Croissant | 1.60 |
| Chocolate Cake | 18.00 |
| Rye Crackers | 2.90 |
+------------------+------------+
Commenting out a condition, as in the last line, is a quick way to experiment. Note that 5--1 is not a comment in MySQL (no space after --): it means 5 - (-1).
SELECT 5--1 AS not_a_comment, 5 -- 1
AS this_is_a_comment;+---------------+-------------------+
| not_a_comment | this_is_a_comment |
+---------------+-------------------+
| 6 | 5 |
+---------------+-------------------+
In the second expression the space turns -- 1 into a comment that hides the rest of the line, so the result is just 5 and the alias on the next line belongs to it.
Note:
/*! ... */is special: MySQL executes the text inside.mysqldumpuses it for version-specific options (/*!40101 SET NAMES utf8mb4 */). Don't use that form for normal comments.
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ - * |
add, subtract, multiply | 7 * 3 |
21 |
/ |
division (decimal result) | 7 / 2 |
3.5000 |
DIV |
integer division | 7 DIV 2 |
3 |
% or MOD |
remainder | 7 % 2 |
1 |
SELECT 7 + 3, 7 - 3, 7 * 3, 7 / 2, 7 DIV 2, 7 % 2, -7 % 2, 1 / 0;+-------+-------+-------+--------+---------+-------+--------+-------+
| 7 + 3 | 7 - 3 | 7 * 3 | 7 / 2 | 7 DIV 2 | 7 % 2 | -7 % 2 | 1 / 0 |
+-------+-------+-------+--------+---------+-------+--------+-------+
| 10 | 4 | 21 | 3.5000 | 3 | 1 | -1 | NULL |
+-------+-------+-------+--------+---------+-------+--------+-------+
The remainder keeps the sign of the first number (-7 % 2 is -1), and division by zero gives NULL.
Comparison operators
| Operator | Meaning |
|---|---|
= |
equal |
<> or != |
not equal |
< > <= >= |
less / greater (or equal) |
<=> |
NULL-safe equal (NULL <=> NULL is 1) |
BETWEEN ... AND ... |
inside an inclusive range |
IN (...) / NOT IN (...) |
one of / none of a list |
LIKE / REGEXP |
pattern / regular-expression match |
IS NULL / IS NOT NULL |
test for NULL |
Comparisons return 1, 0 or NULL. Strings compare alphabetically using the collation (case-insensitively by default); comparing a string with a number converts the string to a number, which can surprise you:
SELECT 'apple' < 'banana' AS alphabetical,
'ABC' = 'abc' AS case_insensitive,
'10' = 10 AS converted,
'10 items' = 10 AS also_converted,
2 BETWEEN 1 AND 3 AS in_range;+--------------+------------------+-----------+----------------+----------+
| alphabetical | case_insensitive | converted | also_converted | in_range |
+--------------+------------------+-----------+----------------+----------+
| 1 | 1 | 1 | 1 | 1 |
+--------------+------------------+-----------+----------------+----------+
'10 items' = 10 is true because MySQL reads the leading digits of the string as a number. It's one more reason to compare numbers with numbers and strings with strings.
Logical operators
| Operator | Meaning |
|---|---|
AND (&&) |
both true |
OR (||) |
at least one true |
NOT (!) |
negation |
XOR |
exactly one true |
The symbol forms &&, || and ! are MySQL-specific and deprecated; use the keywords. In standard SQL (and in MySQL's PIPES_AS_CONCAT mode) || concatenates strings instead.
Bit operators
& (and), | (or), ^ (xor), ~ (invert), << and >> (shift) work on the bits of integers. They're handy for flags packed into one number:
SELECT 6 & 3 AS bit_and, 6 | 3 AS bit_or, 6 ^ 3 AS bit_xor, 1 << 4 AS shifted, TRUE XOR FALSE AS logical_xor;+---------+--------+---------+---------+-------------+
| bit_and | bit_or | bit_xor | shifted | logical_xor |
+---------+--------+---------+---------+-------------+
| 2 | 7 | 5 | 16 | 1 |
+---------+--------+---------+---------+-------------+
Operator precedence
From highest to lowest (operators on the same line have equal precedence): ! · unary - and ~ · ^ · * / DIV % · - + · << >> · & · | · comparisons (=, <, LIKE, IN, …) · BETWEEN CASE · NOT · AND · XOR · OR · assignment :=. When in doubt, use parentheses.
Test yourself
Which line is a valid single-line comment in MySQL?
- --comment
- -- comment
- // comment
- ' comment
What is SELECT 17 DIV 5, 17 % 5;?
- 3.4, 2
- 3, 2
- 3, 3
- 4, 2
What does SELECT NULL <=> NULL; return?
- NULL
- 0
- 1
- An error