I have the following three tables. See full db<>fiddle here
members
| member_id | first_name | last_name |
|---|---|---|
| 1 | Roby | Dauncey |
| 2 | Isa | Garfoot |
| 3 | Sullivan | Carletto |
| 4 | Jacintha | Beacock |
| 5 | Mikey | Keat |
| 6 | Cindy | Stenett |
| 7 | Alexina | Deary |
| 8 | Perkin | Bachmann |
| 10 | Suzann | Genery |
| 39 | Horatius | Baukham |
| 41 | Bendicty | Willisch |
movies
| movie_id | movie_name | movie_genre |
|---|---|---|
| 10 | The Bloody Olive | Comedy,Crime,Film-Noir |
| 56 | Attack of The Killer Tomatoes | (no genres listed) |
ratings
| rating_id | movie_id | member_id | rating |
|---|---|---|---|
| 19 | 10 | 39 | 2 |
| 10 | 56 | 41 | 1 |
Now the question is:
Out of the total number registered members, how many have actually left a movie rating? Display the result as a percentage
This is what I have tried:
SELECT CONVERT(VARCHAR,(CONVERT(FLOAT,COUNT([Number of Members])) / CONVERT(FLOAT,COUNT(*)) * 100)) + '%'
AS 'Members Percentage'
FROM (
SELECT COUNT(*) AS 'Number of Members'
FROM members
WHERE member_id IN (
SELECT member_id FROM members
EXCEPT
SELECT member_id FROM ratings
)
) MembersNORatings
And my query result is displaying as 100%. Which is obvious that the result is wrong.
| Members Percentage |
|---|
| 100% |
What I figured out was that in the first line of the query:
COUNT(*) value is being recognized as the value equivalent to the alias [Number of Members]. That's why it is showing 100%.
I thought of replacing COUNT(*) with SELECT COUNT(*) FROM members but before I try to run the query, it was showing error saying
Incorrect Syntax near SELECT.
What change do I need to make in my existing query in order to get the proper percentage result?


