简体   繁体   中英

SQL writing query to fulfill two requirements [closed]

I am doing a practice problem where I need to write a query that shows all top 100 songs from 1985 to 1990. I tried the following code:

SELECT * FROM billboard.chart 
WHERE year BETWEEN 1985 AND 1990
LIMIT 100 

But it seems like the table contains a tie, so that only gives me until rank 96. I tried using two WHERE clauses, but this gives me an error.

SELECT * FROM billboard.chart 
WHERE year BETWEEN 1985 AND 1990
WHERE year_rank BETWEEN 1 AND 100 

How should I go about extracting both certain time period and certain ranks?

This will give you the expected output.

SELECT * FROM billboard.chart 
WHERE year BETWEEN 1985 AND 1990
AND year_rank BETWEEN 1 AND 100 

You can explore default RANK or DENSE_RANK function within MySQL, it gives you more flexibility on how to handle ties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM