Menu
CoddyTech

Exact Decimal Division

Part of the SQL Foundations section of Coddy's PostgreSQL journey. Lesson 2 of 22.

Division of two integers truncates toward zero. Convert one operand to numeric before dividing when a fractional answer matters. Numeric arithmetic is useful for exact decimal quantities.

SELECT 9 / 4 AS whole, 9::numeric / 4 AS precise;

whole is 2, while precise is 2.25. Casting the result after integer division is too late because the fractional part has already been discarded.

Cast an operand before division. ROUND(value, 2) rounds a numeric value to two decimal places.

challenge icon

Challenge

Medium

Return id and half_price, calculated as price divided by two with decimal arithmetic, rounded to two decimal places. Order by id.

The input tables are shown in the test panel. Each run starts from fresh data. Return the named columns in the stated order; do not hard-code the displayed rows.

Try it yourself

-- Write your SQL here.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in SQL Foundations

Practice on your own: PostgreSQL playground