Challenge - 5 Problems
Decimal and Floating-Point Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of SUM on FLOAT vs DECIMAL columns
Consider a table
What is the result of this query?
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;
Attempts:
2 left
💡 Hint
FLOAT can introduce small precision errors; DECIMAL stores exact values.
✗ Incorrect
FLOAT sums can have tiny floating-point rounding errors, so the sum is close but not exactly 60.6913. DECIMAL sums are exact and match the expected total.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about accuracy and rounding in financial calculations.
✗ Incorrect
DECIMAL stores exact fixed-point numbers, avoiding rounding errors common in FLOAT, which is a floating-point type.
📝 Syntax
advanced1:30remaining
Valid syntax for defining DECIMAL and FLOAT columns
Which of the following column definitions is syntactically valid in MySQL?
Attempts:
2 left
💡 Hint
Check the correct number of parameters and allowed attributes for DECIMAL and FLOAT.
✗ Incorrect
DECIMAL(5,2) is valid: 5 total digits, 2 after decimal. FLOAT(5,2) is valid and UNSIGNED ZEROFILL is allowed for FLOAT; however, FLOAT(5,2,1) is invalid syntax. DECIMAL(5) without scale is allowed but defaults scale to 0, so default 0.00 is incompatible.
❓ optimization
advanced2: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?
Attempts:
2 left
💡 Hint
Consider the total digits and scale needed for your data.
✗ Incorrect
DECIMAL(6,2) stores exactly 6 digits total with 2 decimals, matching the requirement and using minimal storage. DECIMAL(8,2) uses more storage than needed. FLOAT types do not guarantee exact values.
🔧 Debug
expert2:30remaining
Unexpected rounding in FLOAT arithmetic
A developer runs this query:
It returns 0 (false). Why does this happen?
SELECT (0.1 + 0.2) = 0.3 AS is_equal;It returns 0 (false). Why does this happen?
Attempts:
2 left
💡 Hint
Think about how computers store floating-point numbers internally.
✗ Incorrect
Floating-point numbers like 0.1 and 0.2 cannot be represented exactly in binary, causing small rounding errors. Thus, (0.1 + 0.2) is slightly different from 0.3, making the equality false.