What if we set a LIMIT value that is greater than the total number of rows?


In SQL, what happens if we set a LIMIT value that is greater than the total number of rows?


If the number set in the LIMIT clause surpasses the number of rows available to select, then it will just exclude the remaining amount of rows in the result set.

You might think of LIMIT as an upper bound for the number of rows to return, but not as a strict number of rows that must be returned.


Say the table `name` has only 90 rows. 
Then, since 100 is greater than the number of rows, 
it will just return what rows are there. 

FROM names
LIMIT 100;

What happens if there are multiple cells beyond the limit that share the same value? In the sample exercise for instance, if there were 3 movies with 8.8 rating. How would it decide which to show?


I’m not 100% certain, but I believe that in your situation without any other sorts or restrictions it would select the first three entries in the table that met your criteria based on the order they were originally entered into the table.


I think so as well, in the exercise I initially thought that ‘Inception’ and ‘Star Wars: Episode V’ were ordered that way because of alphabetical order. However, upon a second look, it would make a lot more sense that they were ordered by their id, which would confirm your point.


For the same values typically indeed it’s how they were inputed originally… but this might depend on language implementation. E.g. in python if I recall correctly a dictionary does not always return same positions of it’s elements as it does not have it fixed… so it’s related to the question how actually table data stored - more like lists or dictionaries without fixed position…

1 Like

If you query

FROM movies
LIMIT 3000;

You’ll get the same result as

FROM movies

there’s no repeated or empty rows

1 Like

If there are multiple movies with the same rating that fall into the third place and you want to determine which movie specifically is selected, you can add additional columns to the ORDER BY clause to break the tie.

For example, if you want to order the movies with the same rating by the year of release in ascending order, you can modify the query like this:

sql code
SELECT name, year, imdb_rating
FROM movies
ORDER BY imdb_rating DESC, year ASC

This query will return the top 3 movies with the highest rating, and in case of ties in the rating, it will further sort the movies by the year of release in ascending order. This way, you can determine which movie is selected in the third place based on its release year.

Alternatively, you can also add a unique identifier or a secondary criterion to the ORDER BY clause to further break the tie and ensure that only one row is selected.


Can you combine the limit command with the distinct command? Like a limit to 3 distinct imdb_rating values?

on a related note, how do we find the top 3 rated movies for each year? e.g. top 3 rated for 2015, top 3 rated for 2014…2013…