Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
So you think you can count?
Rounding functions and more
Review and practice


Hah! It worked this time, didn't it? That's how you can force the desired result in PostgreSQL.

There is one problem with this method: What if both numbers are given as columns, like hp / level? We need to use another trick: explicitly converting one column to another data type. This procedure is called casting, and it uses the structure shown below:

SELECT (hp::numeric) / level AS result
FROM character;

The structure column::type changes the column to the specified type. You can do this also by using the CAST() function:

SELECT CAST(hp AS numeric) / level AS result
FROM character;

This function takes the name of the column to convert, the keyword AS, and the new data type.

In PostgreSQL, there are three kinds of number data types:

  • Integer data types with names like smallint, integer, bigint, etc.
  • Exact number data types (i.e., decimal types) with names like numeric, decimal, money.
  • Inexact number data types with names like real, double precision.

When dividing two integers in this course, we always cast the numerator to a numeric data type. In a real world application, you may need a different precision and a different casting (e.g., casting both numbers to real).


For each character, show its name, level, and the health points divided by the mana points as the ratio column.

Cast hp to the numeric data type to get a precise result.