Why is the value - despite of using GREATEST() - still out of range? I tried the following to decrease a counter after a post was deleted by using GREATEST() so it does not result a negative value:

SELECT GREATEST(0, posts - 1)
FROM users
WHERE id = 123


but it returns:

[Err] 1690 - BIGINT UNSIGNED value is out of range in '(`db1`.`users`.`posts` - 1)'


posts returns 0:

SELECT posts
FROM users
WHERE id = 123


An the following returns 0 as expected:

SELECT GREATEST(0, 0 - 1)


So what I'm doing wrong?
If your posts column is BIGINT UNSIGNED then unsigned values have to be 0 or more, so -1 is out of range.

MariaDB [test]&gt; desc tbl1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id    | bigint(20) unsigned | YES  |     | NULL    |       |
+-------+---------------------+------+-----+---------+-------+
1 row in set (0.01 sec)

MariaDB [test]&gt; select id -1 from tbl1;
ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(`test`.`tbl1`.`id` - 1)'