161

How do I select only the first 10 results of a query?

I would like to display only the first 10 results from the following query:

SELECT a.names,
         COUNT(b.post_title) AS num
    FROM wp_celebnames a
    JOIN wp_posts b ON INSTR(b.post_title, a.names) > 0
    WHERE b.post_date > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
GROUP BY a.names
ORDER BY num DESC
niton
  • 8,771
  • 21
  • 32
  • 52
Mike
  • 4,041
  • 5
  • 21
  • 14

12 Answers12

236

In SQL server, use:

select top 10 ...

e.g.

select top 100 * from myTable
select top 100 colA, colB from myTable

In MySQL, use:

select ... order by num desc limit 10
Vicky
  • 16,679
  • 54
  • 139
  • 232
Derek Slager
  • 13,619
  • 3
  • 34
  • 34
66

Depends on your RDBMS

MS SQL Server

SELECT TOP 10 ...

MySQL

SELECT ... LIMIT 10

Sybase

SET ROWCOUNT 10
SELECT ...

Etc.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
38

The ANSI SQL answer is FETCH FIRST.

SELECT a.names,
         COUNT(b.post_title) AS num
    FROM wp_celebnames a
    JOIN wp_posts b ON INSTR(b.post_title, a.names) > 0
    WHERE b.post_date > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
GROUP BY a.names
ORDER BY num DESC
FETCH FIRST 10 ROWS ONLY

If you want ties to be included, do FETCH FIRST 10 ROWS WITH TIES instead.

To skip a specified number of rows, use OFFSET, e.g.

...
ORDER BY num DESC
OFFSET 20
FETCH FIRST 10 ROWS ONLY

Will skip the first 20 rows, and then fetch 10 rows.

Supported by newer versions of Oracle, PostgreSQL, MS SQL Server, Mimer SQL and DB2 etc.

jarlh
  • 42,561
  • 8
  • 45
  • 63
34

In MySQL:

SELECT * FROM `table` LIMIT 0, 10
Ben
  • 3,012
  • 1
  • 21
  • 25
  • 3
    What's the difference between limit 0, 10 and limit 10? And why doesn't limit 10, 20 give me 10 rows between the 10th and 20th rows? EDIT: Oh, so limit 10, 20 means give me 20 rows after the 10th row. If I want rows between 10 and 20 I have to do limit 10, 10. Thanks! – AbdurRehman Khan Aug 01 '19 at 12:26
28

In standard SQL you can use:

... FETCH FIRST 10 ROWS ONLY

This is supported in DB2, PostgreSQL and Oracle 12.1 (and later)

brabster
  • 42,504
  • 27
  • 146
  • 186
22

Oracle

WHERE ROWNUM <= 10  and whatever_else ;

ROWNUM is a magic variable which contains each row's sequence number 1..n.

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • 3
    This is wrong. "rownum" will fetch the top 'n' rows from the table. It will not filter from "whatever_else" condition! – Coder Jun 06 '14 at 10:58
  • 1
    http://stackoverflow.com/questions/874082/show-only-the-first-n-lines-of-output-of-a-sql-query Please follow the answer by stili for Oracle related DB – Coder Jun 06 '14 at 11:08
3
SELECT *  
  FROM (SELECT ROW_NUMBER () OVER (ORDER BY user_id) user_row_no, a.* FROM temp_emp a)  
 WHERE user_row_no > 1 and user_row_no <11  

This worked for me.If i may,i have few useful dbscripts that you can have look at

Useful Dbscripts

sayannayas
  • 764
  • 9
  • 15
3

What you're looking for is a LIMIT clause.

SELECT a.names,
         COUNT(b.post_title) AS num
    FROM wp_celebnames a
    JOIN wp_posts b ON INSTR(b.post_title, a.names) > 0
    WHERE b.post_date > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
GROUP BY a.names
ORDER BY num DESC
   LIMIT 10
Ali Almoullim
  • 1,028
  • 9
  • 30
AvatarKava
  • 15,245
  • 2
  • 27
  • 33
3

PostgreSQL:

SELECT ... LIMIT [num] OFFSET [num];
Henry
  • 1,077
  • 1
  • 16
  • 41
  • 2
    Limit is first answer but problem with Limit is, if you have 100,000 record and you want to fetch 10 with limit, it will still take whole time to fetch 100,000 record then will apply limit. so this way sql query get slower for larger record. thats why we are looking for alternate of limit. – Dheeraj Thedijje Aug 21 '20 at 16:19
2

Firebird:

SELECT FIRST 10 * FROM MYTABLE
JNYRanger
  • 6,829
  • 12
  • 53
  • 81
ne2dmar
  • 504
  • 4
  • 17
0
SELECT  Top(12) Month, Year, Code FROM TempEmp 
ORDER BY  Year DESC,month DESC
Code
  • 679
  • 5
  • 9
-1
SELECT* from <table name> WHERE rownum <= 10;
JNYRanger
  • 6,829
  • 12
  • 53
  • 81
  • according to here: https://www.w3schools.com/sql/sql_top.asp This is a listed answer for Oracle.. Up vote. – eaglei22 May 08 '17 at 16:12
  • 2
    This will get you 10 rows, but if you have an "order by" clause, it won't work how you expect. rownum filtering happens before any order by or group by clauses, so you'll get 10 rows, which will then be grouped, or then be ordered. rather than grouping the rows, ordering them how you expect and then giving you first 10 https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/ROWNUM-Pseudocolumn.html#GUID-2E40EC12-3FCF-4A4F-B5F2-6BC669021726 – pohart Feb 14 '23 at 17:15