SQL Query not showing results. The query is to list the names of all the actors who played in the movie 'Anand' - sqlite

Please see the tables:
Sub-Query Method:
select p.Name from Person p where p.PID in
(select mc.PID from M_Cast mc where mc.MID in
(select m.MID from Movie m where lower(title)='anand' ))
Even join is not working:
select p.Name
from Movie m
join M_Cast mc on m.MID = mc.MID
join Person p on mc.PID = p.PID
group by m.MID
having lower(m.title)='anand'

Your first query works without errors and if you make adjustments like in my few next steps the second one will work also.
Your second query: you can not select only p.Name and group by only m.MID. If it is in select clause and is not a part of aggregate function then you have to use it in group by clause. For example like this:
select p.Name
from Movie m
join M_Cast mc on m.MID = mc.MID
join Person p on mc.PID = p.PID
group by p.Name;
Your second query also has a HAVING clause having lower(m.title)='anand' but it should be in where clause like this:
select p.Name
from Movie m
join M_Cast mc on m.MID = mc.MID
join Person p on mc.PID = p.PID
where lower(m.title)='anand'
group by p.Name;
Now that both query are working you need to check if you have a movie with title 'ANAND' in your data. Also you need to check if you have a corresponding MID and PID data in other tables.
I have tested this in MySQL but it will maybe help you even if you use other database to guide you through some mistakes... Here is a small DEMO in MySQL where you will see that data will not be returned if there is no data in one table.
Here is a small DEMO for SQLite where you can see that your first query is working:
http://sqlfiddle.com/#!7/3ec44/1
and here is a small DEMO where you can see that my code is working:
http://sqlfiddle.com/#!7/3ec44/2
Please check the data!
After I have exchanged few comments with OP I have noticed that maybe it is a blank space in data making a problem. So I suggested this:
select p.Name
from Person p
where trim(p.PID, ' ') in (select trim(mc.PID, ' ')
from M_Cast mc
where mc.MID in (select m.MID
from Movie m
where lower(title)='anand'))
This also can be implemented in the second query:
select p.Name
from Movie m
join M_Cast mc on m.MID = mc.MID
join Person p on trim(mc.PID, ' ') = trim(p.PID, ' ')
where lower(m.title)='anand'
group by p.Name;
The problem was that in the query two tables were joined with mc.PID = p.PID and one column had data with blank spaces. So the query was trying to join this data : ' 1' = '1'. TRIM function will remove all the blank spaces in the value and join will then be possible.

SELECT MOVIE.title, person.name
FROM (MOVIE INNER JOIN mcast ON MOVIE.MID = mcast.MID) INNER JOIN person ON mcast.PID = person.PID
WHERE (((MOVIE.title)="ANAND"));

select distinct p.name [Actors in Anand]
Movie m
join M_Cast mc on mc.MID=m.MID
join Person p on p.PID=mc.PID
where m.title="Anand"
order by p.name

Related

Homework Question :: Tried very hard :: Need Direction

I have been stuck on this progressive question for more than 10 days now
Questions is :: Find all the actors that made more movies with Yash Chopra than any other director
Heres my attempt
SELECT pidsWhoDidMoviesWithYashChopra.pid,
pidsWhoDidMoviesWithYashChopra.moviesWithYashChopra,
pidsOfThoseWhoDidMoviesWithDirectors.moviesByAPID,
pidsWhoDidMoviesWithYashChopra.countOfMoviesWithYashChopraByAPID,
pidsOfThoseWhoDidMoviesWithDirectors.totalNumberOfMoviesByAPID
FROM
(
SELECT TRIM(M_Cast.PID) AS pid, moviesByYashChopra.mDirectorMID AS moviesWithYashChopra, COUNT(moviesByYashChopra.mDirectorMID) AS countOfMoviesWithYashChopraByAPID
FROM M_Cast
JOIN
(
SELECT TRIM(M_Director.MID) AS mDirectorMID
FROM
M_Director
WHERE TRIM(M_Director.PID) IN
(
SELECT TRIM(Person.PID) AS personPID
FROM
Person
WHERE Person.Name LIKE '%Yash Chopra%'
)
) AS moviesByYashChopra
ON TRIM(M_Cast.MID) == moviesByYashChopra.mDirectorMID
GROUP BY pid
) AS pidsWhoDidMoviesWithYashChopra
JOIN
(
SELECT TRIM(M_Cast.PID) AS pid, TRIM(M_Cast.MID) AS moviesByAPID, COUNT(TRIM(M_Cast.MID)) AS totalNumberOfMoviesByAPID
FROM M_Cast
GROUP BY pid
) AS pidsOfThoseWhoDidMoviesWithDirectors
ON pidsWhoDidMoviesWithYashChopra.pid == pidsOfThoseWhoDidMoviesWithDirectors.pid
GROUP BY pidsWhoDidMoviesWithYashChopra.pid
And here's the output it produces
And here's the schema
Now where I require help is :: Ability to go ahead from here :: As in how do i dissect this part of the question "than any other director." :: I think that's the tricky part
Any direction/hints will be helpful, Thanks...
You need to join person (for the actor's details) with m_cast, movie, m_director and person again (for the director's details) and group by actor and director to count the number of movies each actor made with each director.
Then use window functions first_value() to get the name of the director with whom the actor made the most movies and lag() to get the 2nd max number of movies (this is needed to filter out ties).
Enclose this query inside a CTE and then filter:
with cte as (
select pa.pid, pa.name, count(*) counter,
first_value(pd.name) over (partition by pa.pid, pa.name order by count(*) desc) max_dir_name,
lag(count(*)) over (partition by pa.pid, pa.name order by count(*) desc) prev_counter
from person pa
inner join m_cast c on c.pid = pa.pid
inner join movie m on m.mid = c.mid
inner join m_director d on d.mid = m.mid
inner join person pd on pd.pid = d.pid
group by pa.pid, pa.name, pd.name
)
select pid, name, counter
from cte
where max_dir_name = 'Yash Chopra' and coalesce(prev_counter, 0) < counter

Getting a min(date) AND max(date) AND their respective titles

I have three tables that I would like to select from
Table 1 has a bunch of static information about a user like their idnumber, name, registration date
Table 2 has the idnumber of the user, course number, and the date they registered for the course
Table 3 has the course number, and the title of the course
I am trying to use one query that will select the columns mentioned in table 1, with the most recent course they registered (name and date registered) as well as their first course registered (name and date registered)
Here is what I came up with
SELECT u.idst, u.userid, u.firstname, u.lastname, u.email, u.register_date,
MIN(l.date_inscr) as mindate, MAX(l.date_inscr) as maxdate, lc.coursename
FROM table1 u,table3 lc
LEFT JOIN table2 l
ON l.idCourse = lc.idCourse
WHERE u.idst = 12787
AND u.idst = l.idUser
And this gives me everything i need, and the dates are correct but I have no idea how to display BOTH of the names of courses. The most recent and the first.
And help would be great.
Thanks!!!
You can get your desired results by generating the min/max date_inscr for each user in a derived table and then joining that twice to table2 and table3, once to get each course name:
SELECT u.idst, u.userid, u.firstname, u.lastname, u.email, u.register_date,
l.mindate, lc1.coursename as first_course,
l.maxdate, lc2.coursename as latest_course
FROM table1 u
LEFT JOIN (SELECT idUser, MIN(date_inscr) AS mindate, MAX(date_inscr) AS maxdate
FROM table2
WHERE idUser = 12787
) l ON l.idUser = u.idst
LEFT JOIN table2 l1 ON l1.idUser = l.idUser AND l1.date_inscr = l.mindate
LEFT JOIN table3 lc1 ON lc1.idCourse = l1.idCourse
LEFT JOIN table2 l2 ON l2.idUser = l.idUser AND l2.date_inscr = l.maxdate
LEFT JOIN table3 lc2 ON lc2.idCourse = l2.idCourse
As #BillKarwin pointed out, this is more easily done using two separate queries.

Sqlite Matching on multiple tags

Let's say I have three tables.
story table:
story_id, story_name, story_author
genre table:
genre_id, genre_name
pair table:
story_id, genre_id
How does one formulate a SQL statement that find all stories with both a romance genre and a comedy genre.
I have tried something like:
SELECT * from pair
INNER JOIN story
ON story.story_id = pair.story_id
INNER JOIN genre
ON genre.genre_id = pair.genre_id
WHERE genre.name = "romance"
AND genre.name = "comedy"
but that doesn't work because in each of the rows returned I only see:
story_id, story_name, story_author, genre_id, genre_name
each row only has the one genre_name listed.
Does anyone have any advice?
try somthing like this:
SELECT story.*
FROM story
INNER JOIN pair firstpair
ON firstpair.story_id = story.story_id
INNER JOIN genre firstgenre
ON firstgenre.genre_id = firstpair.genre_id
INNER JOIN pair secondpair
ON secondpair.story_id = story.story_id
INNER JOIN genre secondgenre
ON secondgenre.genre_id = secondpair.genre_id
WHERE firstgenre.name = 'romance' AND secondgenre.name = 'comedy'
this is the clumsy way to get all stories with both romance and comedy genre, you can also use a custom function to get whether the story match the condition, but I'm not sure if the function is the fast way to do it.
hope this can help~~
This is the equivalent of #baiyangcao's answer, but queries that use subqueries instead of joins can be easier to understand (and are not any less efficient):
SELECT *
FROM story
WHERE story_id IN (SELECT story_id
FROM pair
JOIN genre USING (genre_id)
WHERE genre.name = 'comedy')
AND story_id IN (SELECT story_id
FROM pair
JOIN genre USING (genre_id)
WHERE genre.name = 'romance');
It would be possible to use the same construction to remove the remaining joins, but that is probably not worth it:
SELECT *
FROM story
WHERE story_id IN (SELECT story_id
FROM pair
WHERE genre_id = (SELECT genre_id
FROM genre
WHERE name = 'comedy'))
AND story_id IN (SELECT story_id
FROM pair
WHERE genre_id = (SELECT genre_id
FROM genre
WHERE name = 'romance'));

Getting two different values for same atribute from a single table in SQL

I am having three tables viz. Bids(bid_id,base_price),Customer(customer_id,name,..) and Customer_Bid(customer_id,bid_id,bidding_amount) where customer bids and his bidded amount is stored in Customer_Bid table.
I want to show the details of customer along with his bidded id and the one with highest bid for same bid id.
I have tried to get the customer details but i am not able to show his bidded amount along with the highest bidded amount whish resides in same table.
Plz any one can help me out.
Thanks.
Edit This is the query that was in the comment
select cb.bid_id, c.customer_id ,MyBid=cb.total_bidding_ammount
, HighestBid= max(cb.total_bidding_ammount)
from customer as c
,customer_bidding as cb
,bid as b
group by cb.bid_id, c.customer_id, cb.total_bidding_ammount
If you change this:
, HighestBid= max(cb.total_bidding_ammount)
to something like this:
, HighestBid =
(select max(bidding_ammount)
from customer_bidding
where bid_id = bid.bid_id)
You will on the right track.
Try this one:
SELECT cb.bid_id,
c.customer_id,
cb.total_bidding_ammount,
topbids.customer_id as topbid_customer_id,
topbid
FROM customer c
INNER JOIN customer_bidding as cb
ON c.customer_id = cb.customer_id
INNER JOIN (
SELECT cb.bid_id, c.customer_id, MAX(cb.total_bidding_ammount) as topbid
FROM customer c
INNER JOIN customer_bidding cb
ON (c.customer_id = cb.customer_id)
GROUP BY cb.bid_id
) topbids
ON cb.bid_id = topbids.bid_id
select a.*, b.bidding_amount,
e.bidding_amount as highest_bid
from customer a
inner join customer_bid b on a.customer_id = b.customer_id
inner join bids c on b.bid_id = c.bid_id
join
(select * from (
select bidding_amount,bid_id
from customer_bid
order by bidding_amount desc
) d group by d.bid_id) e on b.bid_id = e.bid_id

SQL Query Conversion to LINQ Left Outer Join (VB.NET)

I've looked all around and spent way to long trying to convert this SQL statement into a Linq statement in VB. I'm sure it would be a good example for others out there - the statement is trying to pull products that have a many-to-many relationship with product categories, and the categories have a hierarchy of parents/children.
Here is the query I am trying to convert:
SELECT P.ProductID, P.ProductName, P.ProductSlug, P.PartNumber
FROM Products AS P
INNER JOIN Products_Categories AS PC ON PC.ProductID = P.ProductID
INNER JOIN Categories AS C ON PC.CategoryID = C.CategoryID
LEFT OUTER JOIN Categories AS P_Cats ON P_Cats.CategoryID = C.Parent
WHERE (C.CategoryID = 9) OR (C.Parent = 9) OR (P_Cats.Parent = 9)
I can get up to the point where I am trying to say "WHERE ... (P_Cats.Parent = 9)" but can't figure that part out.
THANKS!
Figured it out right after posting the question, but here is the answer in case anyone else finds it helpful:
Dim query = (From products In db.Products _
Join PC In db.Products_Categories On PC.ProductID Equals products.ProductID _
Join C In db.Categories On PC.CategoryID Equals C.CategoryID _
Group Join cat In db.Categories On cat.CategoryID Equals C.Parent Into C_Parents = Group _
From cParents In C_Parents.DefaultIfEmpty() _
Where (C.CategoryID = categoryID Or C.Parent = categoryID Or cParents.CategoryID = categoryID) _
And products.IsDeleted = False)
It was the line "From cParents In C_Parents.DefaultIfEmpty()" that I was leaving out.

Resources