FAQ: Queries - Limit

This community-built FAQ covers the “Limit” exercise from the lesson “Queries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development
Data Science

Learn SQL

FAQs on the exercise Limit

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

For LIMIT, it doesn’t seem that this command correctly returns data when there is a tie for the final position. For example, for the data set, if you use:

SELECT *
FROM movies
ORDER BY imdb_rating DESC
LIMIT 5;

You return 5 movies as asked, but if you use a large limit you will see that Star Wars and Intersteller are both ranted 8.7 on IMDb. There must be another parameter that breaks the tie-- seems to be id #. How could you change tie-breaking secondary parameters or change the code to return more rows of data in the case of a tie for the final spot to show all movies that tied for this last spot?

1 Like

SQL Server appears to have a selector to do what you want: SELECT TOP 12 WITH TIES * according to this, but I can’t make it work in the CA version. I’m not sure what the underlying SQL version is that we are dealing with here.

2 Likes

What happens if the limit command conflicts with the tying value of order by command.

For Eg.
In the given table ‘movies’ there were 3 values with ‘imdb_rating = 8.8’ instead of 2 as given in the example.
and the limit remained same i.e
LIMIT 3
So, what will be the criteria of selection in this case?
Or will it display all 3 values with ‘imdb_rating = 8.8’ making it a total of 4 values, including the one with ‘imdb_rating = 9.0’.

1 Like

why is the order of the the query so important, while they will return the same results ?

1 Like

Why did we not but imdb_rating as ‘imdb_rating’ even though this is a string?
how do we determine when to put them within single inverted commoas

There’s actually more than 2 values with rating of 8.8, you can see by increasing the limit to 4.
If limit is set to 3 it will only display the quantity of values requested, but I don’t know what are the tie-breaker parameters that SQL use in this case. As someone said before it appears to be the ‘id’ number.

I think they only need to be wrapped with single quotes like ‘imdb_rating’ when it is a value text string. In this case imdb_rating it’s not a value but a column name.

it would be nice to know from somebody more experienced why SELECT TOP n with TIES * does not work with codecademy´s SQL version. can anybody help us out? much apreciated

Perhaps if there is a tie one can use an IF statement. Though I am new to SQL, not sure if SQL allows for IF statements. I would you can do and if tie sort by how much they gross or something.