The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause is useful for large tables with thousands of records.
Note: Not all database systems support the SELECT TOP clause.
SQL server/ms Access Syntax
SELECT TOP number|percent column_name (s) from table_name;
The SQL SELECT TOP in MySQL and Oracle is equivalent
MySQL syntax
SELECT column_name (s) from table_namelimit number;
Instance
SELECT *from Personslimit 5;
Oracle syntax
SELECT column_name (s) from Table_namewhere ROWNUM <= number;
Instance
SELECT *from personswhere ROWNUM <=5;
Demo Database
In this tutorial, we will use the well-known Northwind sample database.
The following is the data selected from the "Customers" table:
CustomerID |
CustomerName |
ContactName |
Address |
| City
PostalCode |
Country |
1
|
Alfreds Futterkiste |
Maria Anders |
Obere Str. 57 |
Berlin |
12209 |
Germany |
2 |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. De la Constitución 2222 |
México D.F. |
05021 |
Mexico |
3 |
Antonio Moreno Taquería |
Antonio Moreno |
Mataderos 2312 |
México D.F. |
05023 |
Mexico |
4
|
Around the Horn |
Thomas Hardy |
Hanover Sq. |
London |
WA1 1DP |
UK |
5 |
Berglunds Snabbköp |
Christina Berglund |
Berguvsvägen 8 |
Across |
S-958 22 |
Sweden |
SQL SELECT TOP instance
The following SQL statement selects the first two records from the "Customers" table:
SELECT TOP 2 * from Customers;
SQL SELECT TOP PERCENT instance
The following SQL statement selects the previous 50% records from the "Customers" table:
SELECT TOP PERCENT * from Customers;
Original address: http://www.manongjc.com/sql/sql_top.html
SQL-related learning materials:
- SQL Advanced
- SQL SELECT TOP Statement
- SQL like statement
- SQL wildcard characters
- SQL in operator
- SQL between and statements
- SQL alias (Aliases)
- SQL Join Connection
- SQL INNER JOIN
- SQL LEFT Join
- SQL Right Join
- SQL Full JOIN
- SQL UNION
- SQL SELECT into replicate table data
- INSERT into SELECT to copy table data to another table
- SQL CREATE DATABASE
- SQL CREATE TABLE Statement
- SQL constraints (Constraints)
- SQL not NULL constraint
- SQL UNIQUE constraints
- SQL PRIMARY KEY Constraint
- SQL FOREIGN KEY Constraint
- SQL CHECK constraints
- SQL DEFAULT constraints
- SQL CREATE INDEX Statement
- SQL DROP
- SQL ALTER
- SQL AUTO INCREMENT Tutorial
- SQL View (views)
- SQL Dates Date function
- SQL null and NOT NULL
- SQL ISNULL (), NVL (), Ifnull (), and COALESCE () functions
- SQL Common data types
- SQL for data from various databases
SQL SELECT TOP Statement