AVG not correct?

Screen Link:

My Code:

SELECT ROUND (AVG (bytes/1024/1024), 2) AS megabytes
  FROM track;

What I expected to happen:
The megabytes value is 31,47

What actually happened:
It says the final megabytes value should be 31.96

Replace this line with the output/error
1 Like

Hi @badinez,

I agree that it’s not obvious from the task, but here you have to use 1024.0 in your code (instead of 1024) to ensure a division in FLOAT. This is the same approach as using CAST, only that here it looks shorter and simpler. In this case, you’ll get the correct answer.

It’s enough to use it only once, no need to do so in both occasions. For example, you can use:

SELECT ROUND(AVG(bytes/1024/1024.0), 2) AS megabytes
  FROM track;