Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SELECT QUERY COUNT

Tags:

sql

I'm new to this so bear with me.

I'm trying to create a select query from a Movie database. Amongst other tables there is a Role table that features information such as roleID, roleName, gender, actorID, movieID. An actor can have many roles in different movies.

I am trying to create the query so it will tell me how many actors have three or more roles in the database.

I've tried a few solutions and they output data just not sure if it is right.

SELECT COUNT (DISTINCT actorID) FROM Role WHERE actorID >= 3

SELECT COUNT (actorID) FROM Role GROUP BY movieID HAVING COUNT (actorID) >=3
like image 688
user1300580 Avatar asked Nov 29 '22 02:11

user1300580


2 Answers

Try something like:

select actorID, count(*)
from Roles  
group by actorID  
having count (*) >= 3

If you want to retrieve other properties about the actor you add those to both the select and group by clauses

like image 124
kaj Avatar answered Dec 10 '22 03:12

kaj


Try:

SELECT COUNT(*) FROM Role 
GROUP BY actorid
HAVING COUNT(*) >= 3
like image 43
ipr101 Avatar answered Dec 10 '22 03:12

ipr101