0
0
MySQLquery~20 mins

Decimal and floating-point types in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Decimal and Floating-Point Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of SUM on FLOAT vs DECIMAL columns
Consider a table prices with two columns: price_float FLOAT(7,4) and price_decimal DECIMAL(7,4). It has three rows:

(10.1234, 10.1234), (20.5678, 20.5678), (30.0001, 30.0001)

What is the result of this query?

SELECT SUM(price_float), SUM(price_decimal) FROM prices;
MySQL
CREATE TABLE prices (price_float FLOAT(7,4), price_decimal DECIMAL(7,4));
INSERT INTO prices VALUES (10.1234, 10.1234), (20.5678, 20.5678), (30.0001, 30.0001);
SELECT SUM(price_float), SUM(price_decimal) FROM prices;
A(60.69129943847656, 60.6913)
B(60.6913, 60.6913)
C(60.6914, 60.6914)
D(60.6913, 60.69129943847656)
Attempts:
2 left
💡 Hint
FLOAT can introduce small precision errors; DECIMAL stores exact values.
🧠 Conceptual
intermediate
1:30remaining
Choosing between FLOAT and DECIMAL for money values
Which statement best explains why DECIMAL is preferred over FLOAT for storing monetary values in MySQL?
ADECIMAL stores exact values without rounding errors, ensuring precise money calculations.
BFLOAT automatically rounds values to the nearest integer, which is unsuitable for money.
CFLOAT supports more decimal places than DECIMAL, so it is less precise for money.
DDECIMAL uses less storage space than FLOAT, making it more efficient for money.
Attempts:
2 left
💡 Hint
Think about accuracy and rounding in financial calculations.
📝 Syntax
advanced
1:30remaining
Valid syntax for defining DECIMAL and FLOAT columns
Which of the following column definitions is syntactically valid in MySQL?
Aprice FLOAT(5,2) UNSIGNED ZEROFILL
Bprice DECIMAL(5,2) NOT NULL
Cprice DECIMAL(5) DEFAULT 0.00
Dprice FLOAT(5,2,1)
Attempts:
2 left
💡 Hint
Check the correct number of parameters and allowed attributes for DECIMAL and FLOAT.
optimization
advanced
2:00remaining
Optimizing storage for large tables with decimal values
You have a large table storing prices with up to 6 digits and 2 decimal places. Which data type choice optimizes storage while preserving exact values?
ADECIMAL(8,2)
BFLOAT(8,2)
CDECIMAL(6,2)
DFLOAT
Attempts:
2 left
💡 Hint
Consider the total digits and scale needed for your data.
🔧 Debug
expert
2:30remaining
Unexpected rounding in FLOAT arithmetic
A developer runs this query:

SELECT (0.1 + 0.2) = 0.3 AS is_equal;

It returns 0 (false). Why does this happen?
AMySQL does not support decimal addition in SELECT statements.
BThe query syntax is invalid and causes a silent error.
CThe comparison operator '=' cannot be used with floating-point numbers.
DFLOAT arithmetic can introduce tiny precision errors, so 0.1 + 0.2 is not exactly 0.3.
Attempts:
2 left
💡 Hint
Think about how computers store floating-point numbers internally.