I know SQL 92 is old standard but iám pretty sure this is not changed in the newer SQL standard versions.
SQL 92 defines
73)Subclause 6.12, "<numeric value expression>"
: When the data type
of both operands of the addition. subtraction, multiplication,
or division operator is exact numeric, the precision of the
result is implementation-defined."*
75)Subclause 6.12, "<numeric value expression>"
: When the data
type of either operand of an arithmetic operator is approximate
numeric, the precision of the result is implementation-defined."*
The question is: 0.1
and 0.2
in the query SELECT 0.1 + 0.2
a approximate or is it exact?
The answer is: you don't know also the database can't know.
So the database will run the implemention defined for MySQL and MariaDB engines this seams to be handled as DECIMAL(1,1)
datatypes
Why does Nick's answer return the correct values or expected ones with a table definition
SQL 92 also defines
Implicit type conversion can occur in expressions, fetch opera-
tions, single row select operations, inserts, deletes, and updates.
Explicit type conversions can be specified by the use of the CAST
operator.
Which Nick has done by defining the datatype in the table.
Edited this answer because i found something in the MySQL's manual today.
The query
SELECT (0.1 + 0.2) = 0.3
Results into 1
in MySQL which means MySQL uses exact numeric calculation and uses Precision Math where possible.
So the MySQL does know that 0.1
, 0.2
and 0.3
are exact datatypes here and needs to calculate exact, like i was expecting before this edit.
Meaning the query
SELECT (0.1 + 0.2) = 0.3
will run under the hood more or less like
SELECT CAST((0.1 + 0.2) AS DECIMAL(1, 1)) = CAST((0.3) AS DECIMAL(1, 1));