0
0
PostgreSQLquery~20 mins

Numeric and decimal precision in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Numeric Precision Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A3.335
B3.3350
C3.33
D3.334
Attempts:
2 left
💡 Hint
Remember that numeric(p,s) means total digits p and s digits after decimal.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in numeric declaration
Which option contains a syntax error when declaring a numeric column in PostgreSQL?
Anumeric(10,2)
Bnumeric(7,3)
Cnumeric(5,)
Dnumeric(4,0)
Attempts:
2 left
💡 Hint
Check if both precision and scale are specified correctly.
optimization
advanced
2: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?
Anumeric(8,2)
Bnumeric(10,2)
Cnumeric(6,2)
Dnumeric(7,3)
Attempts:
2 left
💡 Hint
Consider total digits needed and decimal places.
🔧 Debug
advanced
3:00remaining
Why does this numeric calculation produce unexpected result?
Given the query:
SELECT (CAST('0.1' AS numeric) + CAST('0.2' AS numeric))::float8 AS result;

Why might the result not be exactly 0.3?
ABecause the string literals are not valid numeric values
BBecause numeric type cannot represent decimal values accurately
CBecause the addition operator is not supported for numeric types
DBecause casting numeric to float8 introduces floating-point precision errors
Attempts:
2 left
💡 Hint
Think about differences between numeric and float types.
🧠 Conceptual
expert
3: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)?
AMaximum precision is unlimited; numeric(131072,16383) is allowed but inefficient
BMaximum precision is 1000; declaring numeric(131072,16383) causes an error
CMaximum precision is 131072; numeric(131072,16383) is valid
DMaximum precision is 16383; numeric(131072,16383) causes an error
Attempts:
2 left
💡 Hint
Check PostgreSQL documentation on numeric precision limits.