Count number of ID in relation and connect to name in PostgreSQL - sql

I have three tables: films, actors and the relationship actors_film.
I'm trying to decide which actors has been in the most number of movies. If the actor has been in a movie this is shown with actor_id(primary key).
I figured I want to count the number of times each actor_id shows in the table film_actor for each film_id, and then connect this with the right name(actor.first_name).
I've got the first part down, but can't seem to connect the Count-value to the right name.
SELECT actor_id, COUNT(*) AS number_of_films
FROM film_actor
GROUP by actor_id
ORDER by 2
DESC limit 1;
How would I go forward connecting the actors name from the actor-table?
I'm thinking I need some form of:
WHERE actor.actor_id = film_actor.actor_id.

You need a simple inner join between the table actors and the table actors_film:
SELECT a.actor_name, a.actor_id, COUNT(*)
FROM actors a INNER JOIN actors_films af
ON a.actor_id = af.actor_id
GROUP BY a.actor_name, a.actor_id
ORDER BY 3;

Related

Sakila with customer x and why who has rented at least 5 movies

Trying to create query with Sakila that finds for each customer X and another customer Y both of who have rented at least 5 movies from the same actor, such that the actor has acted in more than 5 movies. Find all such pairs of Customers (X, Y) and against each pair, the actor name and the number of shared movies of those actors by those customers. With four columns. I cannot figure how to make the run test work and what I am doing wrong. Right now, I have:
CREATE VIEW CustomerFilmActor AS
(SELECT customer_id, film_id, actor_id
FROM rental
JOIN inventory ON rental.inventory_id = inventory.inventory_id
GROUP BY customer_id, film_id, actor_id
ORDER BY customer_id);
SELECT c1.customer_id AS cid1, c2.customer_id AS cid2,
COUNT(c1.film_id) AS ActorMovies
FROM CustomerActorDetails c1
JOIN ActorMovies a ON c1.actor_id = c2.actor_id
GROUP BY cid1, cid2
HAVING COUNT(c1.film_id) >= 5
AND cid1 > cid2
ORDER BY COUNT(c1.film_id) DESC;

SQL query column does not exist

New to SQL here. I tried WITH clause today and can't figure out why I get an error from my query.
Table structure
CREATE TABLE actor
(
actor_id
first_name
last_name
last_update
)
CREATE TABLE film_actor
(
actor_id
film_id
last_update
)
CREATE TABLE film
(
film_id
title
release_year
rating
)
Query that causes the error:
WITH tempotable(actorwithid) AS
(
SELECT
actor.actor_id, first_name, last_name,film_id
FROM
actor
LEFT OUTER JOIN
film_actor ON actor.actor_id = film_actor.actor_id
WHERE
first_name = 'Nick'
AND last_name = 'Wahlberg'
)
SELECT
title, film.film_id, first_name, last_name, actor_id
FROM
film
LEFT OUTER JOIN
tempotable ON film.film_id = tempotable.film_id
ORDER BY
film_id
I can get desired outcome if I switch second left outer join table but I also want actor_id column in my final outcome. After add extra SELECT actor_id in second join, an error: column "actor_id" does not exist pop up. But actor_id column suppose the first column in the tempotable.
I tried tempotable.actor_id still same error.
This is a question from Udemy online sql lessons. Very appreciate for any help!

counting individual elements that meet a certain criteria without duplicates in sqlite3

I'm trying to count all the individual actors from movies in 2004 the problem is when I tried using the count() function it returned how much times every actor appeared in a movie that year.
basically I cant get count() to play well with the GROUP BY function.
SELECT COUNT(name) FROM people
INNER JOIN stars ON stars.person_id = people.id
INNER JOIN movies ON stars.movie_id = movies.id
WHERE movies.year = 2004
GROUP BY name;
relevant tables: movies (id, title, year), stars (movie_id, person_id), people (id, name)
You just need to count the number of DISTINCT person_id in stars (no need to use the people table at all) that have been in movies made in 2004:
SELECT COUNT(DISTINCT person_id) AS num_actors
FROM stars
JOIN movies ON movies.id = stars.movie_id
WHERE movies.year = 2004
Apparently you are not specifying the field you intent to be used for aggregation.Query will return name of actors and respective count of movies. Try below,
SELECT name,COUNT(*) FROM people
INNER JOIN stars ON stars.person_id = people.id
INNER JOIN movies ON stars.movie_id = movies.id
WHERE movies.year = 2004
GROUP BY name;

How to count number of items in a specific type in a table in sql?

I have a table named Movie, with actors attribute. actors_type is specific and looks like this:
GEORGE.ACTOR_TYPE('Clint Eastwood', 'Christopher Carley', 'Bee Vang', 'Ahney Her')
ACTOR_TYPE is implemented as a varray(5) of varchar(20)
the query I tried to count the number of movies for each actor is :
select m.title, a.column_value, count(m.title)
from movie m, table(m.actors) a
group by m.title, a.column_value
order by a.COLUMN_VALUE
which gives me a count of each row(?) Not the count of movies for each actor. the output is as below:
what I am trying to get is to List actors that acted in multiple movies and show movie title and the actor.
but when I add m.title in the select statement, it will count each row.
This is the other query I wrote:
select a.column_value, count(m.title)
from movie m, table(m.actors) a
having count(m.title) > 1
group by a.column_value
order by a.COLUMN_VALUE
and the result is:
I need to add the title to the output too, but when I add it, all the counts will be one, as the first table.
Movie Table:
There is no table for Actors, we create table for it via table(m.actors) a to access its items
You have to remove m.title from group by and selection cause you are going to count that according to actor.I assume column_value is a common column between two tables , so i used that on join
You need to try like below
select a.column_value, count(m.title)
from movie m
join m.actors a on m.column_value=a.column_value
group by a.column_value
order by a.COLUMN_VALUE

SQL select on a many-to-many table

I've got 3 tables: Movies, Actors, and MovieActors. MovieActors is a many-to-many relationship of Movies and Actors with columns MovieActorId, MovieId, and ActorId
How do I find movies that have a certain set of actors in it? For example I want to find all movies that have both Michael Fassbender (actor Id 1) and Brad Pitt (actor Id 2) in them. What would the query look like?
One way is to join the tables. Filter for the actors and then insure the count has the number of actors you want in it (2 in this case)
SELECT
m.MovieID
FROM
Movies m
INNER JOIN MovieActors ma
ON m.MovieID = ma.MovieID
WHERE
ma.ActorID IN (1,2)
GROUP BY
m.MovieID
HAVING COUNT(DISTINCT ma.ActorID) = 2
DEMO
Note
Thanks to user814064 for pointing out that since Actors can have more than one role on a movie we need to count the DISTINCT ma.ActorID not just * The SQL Fiddle Demo demonstrates the difference
select m.movieid
from movies m
inner join movieactors ma on ma.movieid = m.movieid
where ma.actorid in (1,2)
group by m.movieid
having count(distinct ma.actorid) = 2
To keep it simple, you can just do two in clauses:
select * from Movies m
where m.MovieId in (select MovieId from MovieActors where ActorId = 1)
and m.MovieId in (select MovieId from MovieActors where ActorId = 2)
Performance may not be as good as a single join, but it's clean and easy to read.