Basically I'm trying to get the one row that says the movie title and the min(cost) which would be the min from the cost column - min

What are the names for the least expensive movies in the collection? Show both the title of the film and the cost.
table name is movies
and the column name im trying to get a min() from is "COST"
i've tried everything i know to solve it,
select title, min(COST)
from movies.
basically im trying to get the one row that says the movie title and the min(cost) which would be the min from the cost column, any help would help me a lot.
im using sql i9

try this:
SELECT TITLE, MIN(COST) FROM movies GROUP BY TITLE LIMIT 1;

Related

How can I output a table containing the count of all items that match this condition in SQL?

I'm working in CS50 pset7 SQLite queries, and I'm stuck in this problem:
write a SQL query to determine the number of movies with an IMDb rating of 10.0. Your query should output a table with a single column and a single row (plus optional header) containing the number of movies with a 10.0 rating.
So basically what I have to do is go into a table called 'ratings',
which has the strucutre of the image above, and get the number of how many items in the column rating has a value of 10.0.
I have tried count(SELECT * FROM ratings WHERE rating=10.0 but I believe count doesn't work like that...
Hopefully you can help me! Thanks!
Try the below -
select count(*)
FROM ratings WHERE rating=10.0
For returning number of rows of table we use count(*) function of SQL.
Select count(*) from ratings where rating where rating = 10.0 ;

can = follow by a variable in sql

I have two tables movie with moive id, movie title, director of the movie, and rating with rating id, movie id, and rating.
The question is to select director's name together with the title(s) of the movie(s) they directed that received the highest rating among all of their movies, and the value of that rating.
I am trying to understand the following solution
select distinct director, title, stars
from (movie join rating using (mid)) m
where stars in (select max(stars)
from rating join movie using (mid)
where m.director = director)
I am in particular confused with the last subquery
select max(stars)
from rating join movie using (mid)
where m.director = director
from all I know, '=' can only be followed by a fixed value, but here it seems to suggest 'looping' through all distinct directors. Which table is the latter director referring to? And how does the looping concept work in sql?
Although this code works to find the highest rated of all movies, it does not do so for each distinct director and it's not the simplest solution, which I will include below. However, the answers to your questions are:
1) The second director (from the where clause in the subquery) is referring to the table created in that subquery while the other m.director is using the m alias of the first table created in the main query.
2) This isn't really a loop here, in the traditional sense of the word. Basically what the above query is saying is: 'Give me the distinct director name, movie name, and rating from the table created by joining rating to movie, where the number of stars is the largest number of stars pulled from this subquery.' Loops for SQL server use the WHILE keyword but they are pretty rare in SQL since there are other functions (or clauses) that can fulfill the same purpose without the need for iteration.
The query posted in your comment only returns a single line with the data for the highest rated movie of all movies in the database not the highest rated movie for each director. The following is a simpler way of writing the query which gives the highest rating achieved for all movies for each director:
SELECT director, title, MAX(stars)
FROM movie
JOIN rating
ON movie.title = rating.movieID
GROUP BY director

Is there a way to get the average of a column which the query is not grouped by

I dont rly know how to explain my Problem, but i have a Query where i need to group by a column but on the other i need to get an avg of a column which is not grouped by.
My Code is like this:
Select SID,PID,Cost, AVG(COST)
from catalog
group by SID,PID
ORDER by SID
All Columns are in the same table.
What can i do to get the AVG(Cost) of PID?
My Question is related to an exam question which is the following: Find the SID's who charge more for some PID than the average cost of that PID.
The table has the columns SID, PID, COST. I cant upload pictures of the table because my account is new, so im sorry.
So my Problem was that i couldnt get the AVG of the PID, my next Problem because i already tried it with Partition is, that i dont know how the having clause has to look like. Do i need a sub-query for that?
You can use window functions to add an average to a row:
select SID, PID, Cost,
avg(cost) over (partition by pid) as avg_cost_for_pid
from catalog
order by sid;

Calculate avg for field using data from another table MS Access

I have 3 tables :
Film.rating is the average of ratings for that film from the Rate table.
In SQL, to calculate film rating I would do:
SELECT AVG(Rate.rating)
FROM Rate, Film
WHERE Rate.uidFilm=Film.uidFilm;
I do not know how to write this in MS Access and where I should put this (or maybe another?) formula?
How do I 'connect' Film.rating with the calculation above?
In constructor for the Film table?
You need a GROUP BY in your query. Try this
SELECT Film.Title, AVG(Rate.rating)
FROM Rate
INNER JOIN Film ON Rate.uidFilm = Film.uidFilm
GROUP BY Film.Title;
This should give you the average ratings of all movies with their title.

correlated query to update a table based on a select

I have these tables Genre and Songs. There is obviously many to many relationship btw them, as one genre can have (obviously) have many songs and one song may belong to many genre (say there is a song xyz, it belong to rap, it can also belong to hip-hop). I have this table GenreSongs which acts as a many to many relationship map btw these two, as it contains GenreID and SongID column. So, what I am supposed to do this, add a column to this Genre table named SongsCount which will contain the number of songs in this genre. I can alter table to add a column, also create a query that will give the count of song,
SELECT GenreID, Count(SongID) FROM GenreSongs GROUP BY GenreID
Now, this gives us what we require, the number of songs per genre, but how can I use this query to update the column I made (SongsCount). One way is that run this query and see the results, and then manually update that column, but I am sure everyone will agree that's not a programmtic way to do it.
I came to think I would require to create a query with a subquery, that would get the value of GenreID from outer query and then count of its value from inner query (correlated query) but I can't make any. Can any one please help me make this?
The question of how to approach this depends on the size of your data and how frequently it is updated. Here are some scenarios.
If your songs are updated quite frequently and your tables are quite large, then you might want to have a column in Genre with the count, and update the column using a trigger on the Songs table.
Alternatively, you could build an index on the GenreSong table on Genre. Then the following query:
select count(*)
from GenreSong gs
where genre = <whatever>
should run quite fast.
If your songs are updated infrequently or in a batch (say nightly or weekly), then you can update the song count as part of the batch. Your query might look like:
update Genre
set SongCnt = cnt
from (select Genre, count(*) as cnt from GenreCount gc group by Genre) gc
where Genre.genre = gc.Genre
And yet another possibility is that you don't need to store the value at all. You can make it part of a view/query that does the calculation on the fly.
Relational databases are quite flexible, and there is often more than one way to do things. The right approach depends very much on what you are trying to accomplish.
Making a table named SongsCount is just plainly bad design (redundant data and update overhead). Instead use this query for single results:
SELECT ID, ..., (SELECT Count(*) FROM GenreSongs WHERE GenreID = X) AS SongsCount FROM Genre WHERE ID = X
And this for multiple results (much more efficient):
SELECT ID, ..., SongsCount FROM (SELECT GenreID, Count(*) AS SongsCount FROM GenreSongs GROUP BY GenreID) AS sub RIGHT JOIN Genre AS g ON sub.GenreID = g.ID