top of page

selecting TOP N rows in SQL

Writer: Ekta AggarwalEkta Aggarwal

Suppose you have a large dataset but you want to view only first 5 rows. How do you do it?


In this tutorial we will provide 3 different ways to select top N rows depending whether you are using Microsoft SQL server or MS Access or MySQL or Oracle.


Microsoft SQL server or MS Access

SELECT TOP 5 *
FROM my_table;


MySQL

SELECT * FROM my_table
LIMIT 5;

If you want to get next 5 rows starting from 3rd row in MySQL then

SELECT * FROM my_table
LIMIT 2,5;

LIMIT X,Y in MySQL means: Get next Y rows starting from (X+1)th row.


Oracle

SELECT * FROM my_table
WHERE ROWNUM <= 5;
 

Comentarios


Don't Miss Out

Sign Up and Get All Notifications

Thanks for submitting!

  • Facebook
  • LinkedIn

©2023 by Analytics is Normal

bottom of page