Skip to content
June 29, 2010 / evermean

SELECT TOP in MySQL


Question: Is there a way to emulate the behavior of SELECT TOP [Integer] … in MySQL?

Answer: Yes there is…you have to make use of the LIMIT clause.

Example:

The query…

SELECT TOP 5 * FROM movies;

Output:

titleID title url
0012349 The Kid http://www.imdb.com/title/tt0012349/
0015864 The Gold Rush http://www.imdb.com/title/tt0015864/
0017136 Metropolis http://www.imdb.com/title/tt0017136/
0017925 The General http://www.imdb.com/title/tt0017925/
0018455 Sunrise: A Song of Two Humans http://www.imdb.com/title/tt0018455/

…will look like this in MySQL…

SELECT * FROM movies LIMIT 5;

Output:

titleID title url
0012349 The Kid http://www.imdb.com/title/tt0012349/
0015864 The Gold Rush http://www.imdb.com/title/tt0015864/
0017136 Metropolis http://www.imdb.com/title/tt0017136/
0017925 The General http://www.imdb.com/title/tt0017925/
0018455 Sunrise: A Song of Two Humans http://www.imdb.com/title/tt0018455/

You can find more information about selections with LIMIT here.

Hope this helps…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.