28

I want to know the alternative of the TOP keyword as in MySQL. I have read about TOP in SQL Server.

Is there any alternative to this in MySQL, or any other method in MySQL from which we can get same functionality?

Ben
  • 51,770
  • 36
  • 127
  • 149
Avinash
  • 6,064
  • 15
  • 62
  • 95

4 Answers4

33

Ordering and limiting the results:

SELECT field1, field2
FROM myTable
ORDER BY field1 ASC
LIMIT 10
Sampson
  • 265,109
  • 74
  • 539
  • 565
  • This is the right answer but you should review Pascal MARTIN 's answer for a deeper understanding. – HPWD Dec 27 '16 at 20:40
10

You can use the LIMIT keyword (See the documentation of the SELECT instruction) -- it goes at the end of the query :

select *
from your_table
where ...
limit 10

to get the top 10 lines


Or even :

select *
from your_table
where ...
limit 5, 10

To get 10 lines, startig from the 6th (i.e. getting lines 6 to 15).

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 1
    This man is always so cool and fast with his answers, great stuff man :) – Sarfraz Feb 12 '10 at 05:57
  • @Avinash : I'm not sure I can give a definite answer that will be true for every situation, but, in some case, it will manipulate more data that you'd like *(for instance, when there is an `order by` clause, the `limit` can only be applied after the `order by` has been calculated, obviously)* ;;; @Sarfraz : thanks ;-) – Pascal MARTIN Feb 12 '10 at 06:22
  • let me explain my actual problem. My problem is that i have table with 9lac record. i just want to measure the time query execution time on my web page. So i have decided to display only some records on page but query should manipulate all record. so how could i do it.???? – Avinash Feb 12 '10 at 06:29
0

yes, there is the limit clause.

Example:

    SELECT * FROM `your_table` LIMIT 0, 10 

This will display the first 10 results from the database.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • @Pascal: praising him, i like his comprehensive answers and he is also fast, not you man, you see i put my comment in pascal's answer :) – Sarfraz Feb 12 '10 at 06:25
0

mysql equivalent of top and you can find further more about LIMIT in MySql Doc

Gopi
  • 5,656
  • 22
  • 80
  • 146