How can I output a table containing the count of all items that match this condition in SQL? - 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 ;

Related

query which returns 10 values with a complex condition

I am writing now a pretty complex query and I am facing now a problem I am not able to solve.
I have a table called tbl with 2 columns:
movie_id, Rank
(INTEGER), (LIKE\DISLIKE\NULL)
I need to write a query that returns the top 10 movies which
have the most number of LIKES.
(If there is equality of likes, they need to ordered by Ascending movie_id)
Edge Cases:
If there are less than 10 movies which have Rank = 'LIKE'
(let's say there are only 7) then I need to return those 7 movie_id's ordered by the number of likes and another 3 movies_id which are ordered by movie_id
(it doesn't matter if there is 'DISLIKE' or NULL in the Rank value)
If there aren't 10 movies on the table then I need to return the movies that are in the table (in the same way explained before, that is, first I need to return the movies ordered by the number of'LIKES' and then the rest ordered by movie_id)
Can someone please help me with this?
Thank you!
I think this does what you describe:
select t.*
from tbl t
order by ( (ranktype = 'like')::int ) desc,
rank desc
fetch first 10 rows only;

Understanding summary of inner query

Problem understanding subquery
I do not understand this example from www.sqlitetutorial.net/sqlite-subquery :
Only one number is returned by the inner query: 1422138358
But the average of this number is different:
So why is the average of 1422138358 not 1422138358? The two queries are not independent? If I remove "ORDER BY albumid" the result is the same:
Example data:
http://www.sqlitetutorial.net/sqlite-sample-database/
Edit: Ok, there is probably some integer overflow going on as the columns are integer, but I still don't understand why the example take the average of a single number?
Very possibly that it was a mistake
1)
From the text you can see that they wanted to 'sum the size of an album' and you`re querying Tracks table, which supposedly have an album_ID column
2)
You cannot use ORDER BY if you`re using only aggregation column
such as
select SUM(bytes)
from Tracks
Order by albumID
because it has nothing to order it by from.
Also note that you cannot use order by in subqueries
Finally what was missing here was this remaining of the query :
Select AVG(album.size) as [avg(album.size)]
from (
select albumID,SUM(bytes) as size
from Tracks
GROUP BY albumID
) as album
You can learn more about subqueries here
And if you want to play around with these, heres the code that you can replicate and use it for further exercies on that website:
CREATE TABLE tracks (AlbumID int,bytes int)
CREATE TABLE albums (AlbumID int, title nvarchar(50))
insert into Tracks values (1,2),(2,10),(3,15)
Select AVG(album.size) as [avg(album.size)]
from (
select AlbumID,SUM(bytes) as size
from tracks
GROUP BY albumID
) as album
Hope it helps

How to print out the number of records given a condition [duplicate]

This question already has answers here:
Count(*) vs Count(1) - SQL Server
(13 answers)
Closed 5 years ago.
I'm creating a SQL database for movies and I want to print out the number of movies that are Sci-Fi in the database.
I have ran this command, but I'm getting an error.
Can anyone please points out to what is wrong in the command?
SELECT COUNT Genre FROM Movies WHERE Genre='Sci-Fi'
count is a function, so you need to call it with an argument list in (). count, specifically, can get a special argument *, which means it will just count all the rows (that pass the filtering):
SELECT COUNT(*) FROM Movies WHERE Genre = 'Sci-Fi'
-- Here ----^
Additionally, since you genre column doesn't contain single values (a decision you may want to revisit), using the = operator like this will miss any movie that has multiple genres. Using like should do the trick:
SELECT COUNT(*) FROM Movies WHERE Genre LIKE '%Sci-Fi%'
Use below query
select count(*) from movies where Genre like "%Sci-Fi%"
SELECT COUNT(Title) FROM Movies WHERE Genre like '%Sci-Fi%'
SELECT COUNT(*) FROM Movies WHERE Genre like '%Sci-Fi%'
Try this one: SELECT COUNT(*) FROM Movies WHERE Genre like 'Sci-Fi'

SQL query giving strange results

I am trying to execute a SQL query which gives me strange results. Here are the tables:
U1Base has information about movie ratings given by users - it includes the user ID, movie ID and Rating given by the user to the movie (also additional information not relevant to the question)
U1Test has the same columns as U1Base and is used for testing the results of the calculations on U1Base
these two tables are imported from MovieLens so they are beyond reproach (I did need to clean some of the data but it was only one row out of 80000)
S1_Similarity includes the similarity measure between every two users in the database
I want to calculate a prediction for the rating a user would give a movie he/she have not watched. For this I want to extrapolate from u1base and compare the results to u1test.
u1test has 20000 rows.
Here is the query I use for selecting:
SELECT test.user_id,
test.movie_id,
sum(cast(base.rating as float))/5
FROM u1test AS test,
u1base AS base
WHERE base.user_id<>test.user_id
AND base.movie_id=test.movie_id
AND base.user_id IN (
SELECT TOP 5 user_id2
FROM s1_similarity
WHERE user_id1=test.user_id
ORDER BY similarity desc
)
GROUP BY
test.user_id,test.movie_id
For some reason this query does not return 20,000 rows as I expected. If I increase the parameter for top it includes more rows. I thought that each row in u1test will be given a calculated average.
What am I doing wrong?
Dotan

Using result of count(*) in oracle in an arithmetic expression

There is a simple issue that I am stuck with and need some help.
I have two different tables, in Oracle 10g, called BOOK_DETAILS (primary key- book_code) and BOOK_ISSUE (foreign and primary key- book_code). The BOOK_ISSUE table also has a column called BOOK_ISSUE_STATUS that holds 'y' or 'n' values depending on if the book is issued or not.
Now I have to do the following simple query - find out the number of books available in the library (i.e. total number of books available - number of books issued).
So I tried doing the following query :-
select count(b.book_code)-count(i.book_code)
from
lms_book_issue i, lms_book_details b
where
i.book_issue_status='Y'
;
But apparently I am not getting the desired result. My logic is to subtract the result of count of number of books issued (which I get from the BOOK_ISSUE table) from the result of the count the number of books in the BOOK_DETAILS table.
Please let me know where am I going wrong. Moreover the above query simply gives 0 as the result. So an explanation of that as well would be highly appreciated.
You can get (total number of books - books with status 'Y'), by :
select
(select count(book_code) from BOOK_DETAILS) -
(select count(book_code) from BOOK_ISSUE where book_issue_status='Y')
from dual
This is a basic mistake, count(col) get you the number of rows in the query with not-null value.
You got 0 simply because the 2 counts gave to the same number.
In this case you even didn't join the 2 tables together with "i.book_code = b.book_code"
But even than it will return you 0,
In your way, the correct select is:
select
count(b.book_code) - count(i.book_code)
from
lms_book_issue i, lms_book_details b
where
b.book_code = i.book_code(+) AND
i.book_issue_status = 'Y'
;
This way you selected all the books and the books and joined only with issue tab only with rows that have issue=Y, therefor the first count will give you the number of books and the 2nd one will count only the issued books because the rest will be NULL and won't count.