tags:

views:

234

answers:

5

Hi,

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

I would like to display the first 10 results only 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

Thanks!

+4  A: 

Depends on your RDBMS

MS SQL Server

SELECT TOP 10 ...

MySQL

SELECT ... LIMIT 10

Sybase

SET ROWCOUNT 10
SELECT ...

Etc.

martin clayton
+7  A: 

In SQL server, use:

select top 10 ...

In MySQL, use:

select ... order by num desc limit 10
nullptr
Great. Thanks. MySQL solution worked!
Mike
A: 

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

AvatarKava
+2  A: 

DB2

... FETCH FIRST 10 ROWS ONLY

Brabster
+3  A: 

Oracle

WHERE ROWNUM <= 10  and whatever_else ;

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

wallyk