SQL Finding similarities - mysql

"Determine if there are actors with the same first name who appeared in the same movie."
This is my task and I'm supposed to do that with subqueries and I just dont really know what else to do. I tried everything with group by, order by and having count but I just don't get to the point where I get the same first name actors with the same movie.
Maybe someone can help me? I am using Sakila Database
SELECT
a.first_name
,(a.last_name)
,a.actor_id
, f.title
FROM actor a
JOIN film_actor fa ON fa.actor_id = a.actor_id
JOIN film f ON f.film_id = fa.film_id
JOIN(SELECT b.first_name, COUNT(*)
FROM actor B
GROUP BY b.first_name
HAVING COUNT(*) > 1 ) b
ON a.first_name = b.first_name
GROUP BY a.last_name
HAVING COUNT(f.title) > 1
ORDER BY a.first_name

You can do this with joins only:
select f.title, a1.first_name, a1.last_name as last_name_1, a2.last_name as last_name_2
from film f
inner join film_actor fa1 on fa1.film_id = f.film_id
inner join film_actor fa2 on fa2.film_id = f.film_id
inner join actor a1 on a1.actor_id = fa.actor_id
inner join actor a2 on a2.actor_id = fa.actor_id
where a1.first_name = a2.first_name and a1.actor_id < a2.actor_id
Starting from the film table, this follows the relationships to actor through film_actor twice, and then filters on diffrerent actors that have the same first name.
As a result, you get tuples of actors that have the same last name and played in the same film. The inequality condition ensures that there are no "mirror" records (that is, each tuple appears only once per film).

I would simply use aggregation:
SELECT fa.film_id, a.first_name,
GROUP_CONCAT(a.last_name) as last_names,
GROUP_CONCAT(a.actor_id) as actor_ids
FROM actor a JOIN
film_actor fa
ON fa.actor_id = a.actor_id
GROUP BY fa.film_id, a.first_name
HAVING COUNT(*) > 1;
Your question doesn't specify what the result set should look like. This returns one row per actors with the same first name in a film. The last names are concatenated into a string as are the actor ids.

How about this:
SELECT f.title, f.film_id, a.first_name, a.last_name, a.actor_id
FROM actor a
JOIN film_actor fa ON fa.actor_id = a.actor_id
JOIN film f ON f.film_id = fa.film_id
WHERE a.first_name IN (
SELECT a2.first_name
FROM actor a2
JOIN film_actor fa2 ON fa2.actor_id = a2.actor_id
JOIN film f2 ON f2.film_id = fa2.film_id
WHERE a2.actor_id <> a.actor_id AND f2.film_id = f.film_id
)
ORDER BY f.title ASC, a.last_name ASC, a.first_name ASC
Explaining query step-by-step
SELECT the needed fields from the joined tables
JOIN the necessary tables
WHERE (here is the subquery) a.first_name is in the set of:
firstnames of actors, different than the current actor (a2.actor_id <> a.actor_id) and the film is the same (f2.film_id = f.film_id)
The subquery in where is a similar select with joins query as the parent query.
PS:
One can do variations on this basic query template:
Eg film_id can be given as parameter, so one can find all actors with same name for specific film.
Also one can group and count how many actors appeared in same film wih same name eg by grouping on film_id and counting.
One can even optimise a bit the query by removing unnecessary joins (eg film.title may not be needed at all) and so on..
The advantage of having single results returned (instead of tuples or aggregates) is that number of actors with same name in same film is not fixed and manipulating the results, eg by grouping and counting or getting further info for each actor, is easier.
The price is a slightly more complex and potentialy slower query.

Related

SQL Sakila Query Question - Find all actors that have starred in films of all 16 film categories

I am trying to put together a query from the Sakila database.
The query should find all actors that have starred in all 16 film categories.
To get all of this information into one table for querying, I have performed a INNER JOIN:
SELECT a.first_name, a.last_name FROM actor a
INNER JOIN film_actor fa
ON fa.actor_id = a.actor_id
INNER JOIN film_category fc
ON fc.film_id = fa.film_id;
However, from there I do a GROUP BY on the category_id but don't know how to iterate through and count if a particular actor_id has all 16 categories?
Does this complex of a query require writing a FUNCTION or PROCEDURE?
You are almost there. Group against the actor name and check that the unique category count is 16:
SELECT a.actor_id, a.first_name, a.last_name
FROM actor a
INNER JOIN film_actor fa ON fa.actor_id = a.actor_id
INNER JOIN film_category fc ON fc.film_id = fa.film_id
GROUP BY a.actor_id, a.first_name, a.last_name
HAVING COUNT(DISTINCT fc.category_id) =
(
SELECT COUNT(DISTINCT category_id)
FROM film_category
)

SQL calling all Fields

I have a question, I am trying to list all of the actors for each movie in my database. Right now I have this SQL
SELECT title, count(1) COUNT, actor.first_name, actor.last_name
FROM film
INNER JOIN film_actor ON film.film_id = film.actor.film_id
INNER JOIN actor ON film_actor.actor_id = actor.actor_id
GROUP BY title.
What I am trying to do is format my out put into a text file like this:
Title:Number of Actors in each film: Actor first name(1): Actor last name(2) : Actor first name(2): Actor last name(2) ... ect
But for example, in the first film there are ten actots for this film. How would I print out the first and last name of each actor for each movie.
The problem is you want aggregate and non-aggregate data.
One way to achieve this in MySQL you would have to get the count of actors in each film as a sub query and join back to the base set.
SELECT title, b.cnt COUNT, actor.first_name, actor.last_name
FROM film
INNER JOIN film_actor
ON film.film_id = film_actor.film_id
INNER JOIN actor
ON film_actor.actor_id = actor.actor_id
INNER JOIN (SELECT film_ID, count(1) as cnt
FROM film_actor
GROUP BY film_ID) B
on film.film_ID = B.Film_ID
in databases which support window functions you could use a count(1) over (parttition by film.film_ID) as cnt
It may also be able to be achieved by using a group by with rollup without using an added join; but this creates an additional row with the total.
SELECT title, count(1) COUNT, actor.first_name, actor.last_name
FROM film
INNER JOIN film_actor
ON film.film_id = film_actor.film_id
INNER JOIN actor
ON film_actor.actor_id = actor.actor_id
on film.film_ID = B.Film_ID
GROUP BY title with rollup

How to display Null result with a WHERE clause

Hey guys I'm new to SQL and having some difficultly. I'm hoping someone could clear some stuff up for me.
This is my issue. I want to display all of the categories that an actor has played in and the amount of films they have played in that category. So for example they have played in action movies 5 times. This is what I have so far:
SELECT c.name AS "Category_Name"
, Count(c.name) AS "Count"
FROM category c
JOIN film_category fc
ON c.category_id = fc.category_id
JOIN film f
ON fc.film_id = f.film_id
JOIN film_actor fa
ON f.film_id = fa.film_id
JOIN actor a
ON fa.actor_id = a.actor_id
WHERE a.first_name = "Kevin"
AND a.last_name = "Bloom"
GROUP
BY c.name
ORDER
BY c.name ASC;
This will display all of the categories and the amount of times "Kevin Bloom" has played in each however it will not display NULL values for categories he has not played in and I need it to. I have spend a few hours trying to figure this out but it either didn't help or I wasn't able to understand it.
From what I gather the WHERE clause is causing this issue. I also believe I will likely need to use a LEFT JOIN instead and possibly a sub query. I'm a little shaky on both of these things when used in conjunction. If anyone can offer some help to a first time learner I would really appreciate it!
SELECT c.name AS "Category_Name", Count(a.actor_id) AS "Count"
FROM category c
LEFT JOIN film_category fc ON c.category_id = fc.category_id
LEFT JOIN film f ON fc.film_id = f.film_id
LEFT JOIN film_actor fa ON f.film_id = fa.film_id
LEFT JOIN actor a
ON fa.actor_id = a.actor_id
AND a.first_name = 'Kevin' AND a.last_name = 'Bloom'
GROUP BY c.name
ORDER BY c.name ASC;
Per your comment, why to use AND versus WHERE is about how WHERE is evaluated when executing. The WHERE clause limits the entire result set by the condition(s) you specify. Whereas the ON conditions only limit what is allowed to match the records and not necessarily the entire results set when an OUTER JOIN is used. So If you put a WHERE condition in that limits results based on the RIGHT side of your LEFT JOIN it becomes and INNER JOIN because it tells SQL that you only want the results that match and because only some categories match that actor you would only get those categories. However by putting the condition in the ON clause of the JOIN your results are not limited and all categories will be returned but only actors matching your criteria will be considered.
You would use a LEFT JOIN, but you have to be careful:
SELECT c.name AS Category_Name, Count(a.actor_id) AS "Count"
FROM category c LEFT JOIN
film_category fc
ON c.category_id = fc.category_id LEFT JOIN
film f
ON fc.film_id = f.film_id LEFT JOIN
film_actor fa
ON f.film_id = fa.film_id LEFT JOIN
actor a
ON fa.actor_id = a.actor_id AND a.first_name = 'Kevin' AND a.last_name = 'Bloom'
GROUP BY c.name
ORDER BY c.name ASC;
Notes:
LEFT JOIN is key to the solution.
Notice the COUNT() has changed to count the id from actor. This will return 0 for categories where he has not acted.
The standard delimiter for strings in SQL is a single quote, not a double quote.
There is no need to escape column aliases, unless necessary.

MySQL - Movie Database Query including Null Rows

For reference I am working on the Sakila rental DVD database outlined here (#2): http://www.ntu.edu.sg/home/ehchua/programming/sql/SampleDatabases.html
I am trying to find the runtime of all Sci-Fi movies each actor has been in, including those who have not been in a Sci-Fi movie. I have the correct query for those who HAVE been in a Sci-Fi movie, but I'm having trouble expanding it to include all actors even if the runtime is NULL.
Here is my query:
SELECT act.first_name, act.last_name, SUM(fm.length)
FROM film fm
INNER JOIN film_actor fa ON fa.film_id = fm.film_id
INNER JOIN actor act ON fa.actor_id = act.actor_id
LEFT JOIN film_category fc ON fm.film_id = fc.film_id
LEFT JOIN category cat ON fc.category_id = cat.category_id
WHERE cat.name = 'Sci-Fi'
GROUP BY act.first_name, act.last_name
ORDER BY act.last_name ASC
This gets me all 167 actors who are in at least 1 Sci-Fi movie. I think my WHERE clause is not allowing NULL rows, but I don't know how to fix it.
You are correct, the where clause is working on the far right side of your left joins...if it is null it's going to be filtered out because null does not equal 'Sci-Fi'. Want nulls? Tell the where statement to include them
WHERE (cat.name = 'Sci-Fi' or cat.name is null)
You should have "cat.name = 'Sci-Fi' be a part of your ON clause in the outer join.
Unless your criteria is filtering in on nulls, you should never put criteria involving an outer joined table in the WHERE clause, as it turns the outer join into an inner join. The criteria belongs in the ON clause.
SELECT act.first_name, act.last_name, SUM(fm.length)
FROM film fm
INNER JOIN film_actor fa
ON fa.film_id = fm.film_id
INNER JOIN actor act
ON fa.actor_id = act.actor_id
LEFT JOIN film_category fc
ON fm.film_id = fc.film_id
LEFT JOIN category cat
ON fc.category_id = cat.category_id
and cat.name = 'Sci-Fi'
GROUP BY act.first_name, act.last_name
ORDER BY act.last_name ASC

MySql joining 3 tables on condition

Im looking to expand on the knowledge I already have in MySql (it is not that much ), so I download a sample database from : http://downloads.mysql.com/docs/sakila-db.zip , and start to play with, my q that is there a way to join these tables ( actor, film, film-actor ) to have a table that show each actor with the films he act on?
These tables have many-to-many relationship, to show each actor with the films you can use this query -
SELECT
a.actor_id,
a.first_name,
a.last_name,
f.film_id,
f.title
FROM actor a
INNER JOIN film_actor fa
ON fa.actor_id = a.actor_id
INNER JOIN film f
ON f.film_id = fa.film_id
This is a modified query of Devart. If you want to have a result with Actor_ID, concactenated ActorName and concactenated TITLE then you should try this one:
SELECT
a.actor_id,
CONCAT(a.first_name,' ',a.last_name) AS ActorName,
GROUP_CONCAT(f.title) Titles
FROM actor a
INNER JOIN film_actor fa
ON fa.actor_id = a.actor_id
INNER JOIN film f
ON f.film_id = fa.film_id
GROUP BY actor_id