Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQL SELECT TOP, LIMIT, ROWNUM

SQL SELECT TOP, LIMIT, ROWNUM Clauses


SQL SELECT TOP Clause

The SELECT TOP statement is used in SQL to limit the number of rows in a returned result set. It is typically used when only the first few rows are needed, which can significantly improve query performance, especially with very large datasets.

The SELECT TOP clause is very useful for large tables with thousands of records.

** Note:**

SQL Server / MS Access Syntax

SELECT TOP number|percent column1, column2, ...
FROM table_name;

number|percent: Specifies the number or percentage of rows to return.

MySQL Syntax

SELECT column1, column2, ...
FROM table_name
LIMIT number;

Oracle Syntax

SELECT column1, column2, ...
FROM table_name
FETCH FIRST number ROWS ONLY;

PostgreSQL Syntax

SELECT column1, column2, ...
FROM table_name
LIMIT number;

Example

Assume we have a table named Employees with the following data:

EmployeeID EmployeeName Salary
1 John Smith 50000
2 Maria Garcia 60000
3 Liam Johnson 70000
4 Emma Wilson 80000
5 Oliver Brown 90000

** SQL Server and MS Access return the first 3 rows:**

SELECT TOP 3 EmployeeName, Salary
FROM Employees;

Return the first 10% of the data:


SELECT TOP 10 PERCENT EmployeeName, Salary
FROM Employees;

MySQL returns the first 3 rows:


SELECT EmployeeName, Salary
FROM Employees
LIMIT 3;

** PostgreSQL returns the first 3 rows:**


SELECT EmployeeName, Salary
FROM Employees
LIMIT 3;

** Oracle returns the first 3 rows:**

SELECT EmployeeName, Salary
FROM Employees
FETCH FIRST 3 ROWS ONLY;

Demonstration Database

In this tutorial, we will use the RUNOOB sample database.

Below is the data selected from the "Websites" table:


mysql> SELECT * FROM Websites;
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  3 | Runoob Tutorial       | http://www.runoob.com/    |  5000 | USA     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
|  5 | Facebook      | https://www.facebook.com/ |     3 | USA     |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

MySQL SELECT LIMIT Example

The following SQL statement selects the first two records from the "Websites" table:

Example

SELECT

FROM

Websites

LIMIT

2

;

Executing the above SQL, the data is as follows:


SQL SELECT TOP PERCENT Example

In Microsoft SQL Server, you can also use a percentage as a parameter.

The following SQL statement selects the first 50 percent of records from the websites table:

Example

The following operation can be performed in the Microsoft SQL Server database.

SELECT

TOP

50

PERCENT

FROM

Websites

;