Answering business questions using SQL Guided Project

Hi,

I am having some trouble undestanding the difference between the following statements of the solutions:

SELECT
g.name genre,
count(uts.invoice_line_id) tracks_sold,
cast(count(uts.invoice_line_id) AS FLOAT) / (
SELECT COUNT(*) from usa_tracks_sold
) percentage_sold

Shouldn’t both statements in bold give the same number?

Following up, why does the next statement result in an error?

SELECT
    g.name genre,
    COUNT(ts.invoice_line_id) number_of_tracks,
    CAST(COUNT(ts.invoice_line_id) / SUM(COUNT(ts.invoice_line_id))) percentage_sold

Thanks!
Ivo

Hi @ivo.monteiro8,

count(uts.invoice_line_id) returns the count of all rows. Whereas SELECT COUNT(*) from usa_tracks_sold returns the count of rows where the customer’s country is USA.

So they are returning different results.

SELECT
g.name genre,
COUNT(ts.invoice_line_id) number_of_tracks,
CAST(COUNT(ts.invoice_line_id) / SUM(COUNT(ts.invoice_line_id))) percentage_sold

You are getting an error here because you haven’t mentioned the CAST type. For example: cast(count(uts.invoice_line_id) AS FLOAT)

Best,
Sahil

1 Like

Hi,

Thanks for the reply @Sahil!
Much appreciated.

Ivo

1 Like