Challenge - 5 Problems
Numeric Precision Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of numeric addition with different precisions
What is the output of the following SQL query in PostgreSQL?
SELECT CAST(1.2345 AS numeric(5,3)) + CAST(2.1 AS numeric(3,1)) AS result;
PostgreSQL
SELECT CAST(1.2345 AS numeric(5,3)) + CAST(2.1 AS numeric(3,1)) AS result;
Attempts:
2 left
💡 Hint
Remember that numeric(p,s) means total digits p and s digits after decimal.
✗ Incorrect
The first number is cast to numeric(5,3), so it is rounded to 3 decimal places: 1.235 (1.2345 rounds up). The second number is numeric(3,1), so 2.1. Adding 1.235 + 2.1 = 3.335. The result inherits precision from the operation and is displayed as 3.335.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in numeric declaration
Which option contains a syntax error when declaring a numeric column in PostgreSQL?
Attempts:
2 left
💡 Hint
Check if both precision and scale are specified correctly.
✗ Incorrect
Option C has a missing scale value after the comma, which is invalid syntax. Both precision and scale must be specified as integers.
❓ optimization
advanced2:30remaining
Choosing numeric precision for performance
You need to store monetary values with up to 2 decimal places and values up to 999999.99. Which numeric type declaration is best for performance and storage efficiency in PostgreSQL?
Attempts:
2 left
💡 Hint
Consider total digits needed and decimal places.
✗ Incorrect
To store values up to 999999.99, you need 6 digits before decimal and 2 after, total 8 digits. numeric(8,2) fits exactly and uses less storage than larger precision types.
🔧 Debug
advanced3:00remaining
Why does this numeric calculation produce unexpected result?
Given the query:
Why might the result not be exactly 0.3?
SELECT (CAST('0.1' AS numeric) + CAST('0.2' AS numeric))::float8 AS result;Why might the result not be exactly 0.3?
Attempts:
2 left
💡 Hint
Think about differences between numeric and float types.
✗ Incorrect
Numeric type stores exact decimal values. Casting to float8 converts to floating-point which can introduce small precision errors, causing result to be slightly off from 0.3.
🧠 Conceptual
expert3:00remaining
Understanding numeric precision and scale limits
In PostgreSQL, what is the maximum precision allowed for the numeric type, and what happens if you try to declare numeric(131072,16383)?
Attempts:
2 left
💡 Hint
Check PostgreSQL documentation on numeric precision limits.
✗ Incorrect
PostgreSQL limits numeric precision to 1000 digits. Declaring numeric(131072,16383) exceeds this and causes an error.