May 16, 2021 SQL
2. SQL SELECT TOP in MySQL and Oracle is equivalent
5. SQL SELECT TOP PERCENT instance
6. SQL TOP, LIMIT, and ROWNUM examples
Note: Not all database systems support the SELECT TOP clause. MySQL supports LIMIT clauses to select a limited number of records, while Oracle uses ROWNUM.
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
SELECT *
FROM Persons
LIMIT 5;
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
SELECT *
FROM Persons
WHERE ROWNUM <=5;
In this tutorial, we'll use the famous Northwind sample database.
Here's the data 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 | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
The following SQL statement selects the first two records from the Customers table:
The following SQL statement selects the top 50% of records from the "Customers" table:
The following SQL statement selects the first three records from the Customers table:
SELECT TOP 3 * FROM Customers;
The following SQL statement shows an equivalent example of using the LIMIT clause:
SELECT * FROM Customers
LIMIT 3;
The following SQL statement shows an equivalent example of using ROWNUM:
SELECT * FROM Customers
WHERE ROWNUM <= 3;
The following SQL statement selects the first 50% of records from the Customers table:
SELECT TOP 50 PERCENT * FROM Customers;
The following SQL statement selects the first three records with the country as "Germany" from the "Customers" table:
SELECT TOP 3 * FROM Customers
WHERE Country='Germany';
The following SQL statement shows an equivalent example of using the LIMIT clause:
SELECT * FROM Customers
WHERE Country='Germany'
LIMIT 3;
The following SQL statement shows an equivalent example of using ROWNUM:
SELECT * FROM Customers
WHERE Country='Germany' AND ROWNUM <= 3;
LIMIT as a simple picing method, mainly to reduce the time of data return, if you query a very large table (for example, a table with hundreds of thousands or millions of rows) without using limits, then you may wait a long time to display all the results, so using LIMIT can reduce the time and efficiency of query data return.
Now, use the following topics to measure your mastery of the content of this chapter!
SQL LIMIT keywords: In this section of the quiz, you will practice using the SQL LIMIT keyword to return all results that meet the criteria specified in the SQL statement.
Note: The above tests are paid and premium VIP is free of charge