Https://www.codecademy.com/paths/data-science/tracks/sql-intermediate/modules/dspath-sql-aggregates-projects-and-applications/projects/sql-hackernews

Hi everyone!

Not sure if this question has been answered yet but I’ll give it a shot. I was curious if there is any reason why this particular line in SQL outputs 0

SELECT (517 + 309 + 304 + 282) / 6366;

But, when I run this:

SELECT (517 + 309 + 304 + 282) / 6366.0;

I get an answer of 0.221803330191643, which is the right answer.

Is there any reason for this and if so, is this just a regular SQL command (to add a decimal after the whole number)?

Any help would be appreciated!

It looks like the SQL that Codecademy runs on the backend defaults to integer division. This will round the result down to the nearest integer. To return a number with decimals (a float), you convert one of the numbers into a float by adding the .0 at the end.

Not all RDBMSs do integer division by default (MySQL defaults to float division and uses the DIV function to do integer division), but it is good practice to just include the .0 at the end of one of your numbers in your equation either way if you want to ensure you return a float.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.