SQL Top | Top Command in SQL - sql - sql tutorial - learn sql




  • The TOP keyword restricts the number of results returned from a SQL statement in Microsoft SQL Server.
  • The SELECT TOP clause is used to return the top X numbers or N Percent row from the table.
  • Only MSSQL server and MS Access database support the SELECT TOP clause.
  • To fetch limited number of records, LIMIT clause is used in MySQL database & ROWNUM in Oracle database.
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Syntax

The syntax for TOP is as follows:

SELECT TOP [TOP argument] "column_name"
FROM "table_name";

where [TOP argument] can be one of two possible types:

    1. [N]: The first N records are returned.
    2. [M] PERCENT: The number of records corresponding to M% of all qualifying records are returned.
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Examples

sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

We use the following table for our examples.

Table Store_Information

Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
San Francisco 300 Jan-08-1999
Boston 700 Jan-08-1999

Example 1: [TOP argument] is an integer

  • To show the two highest sales amounts in Table Store_Information, we key in,
SELECT TOP 2 Store_Name, Sales, Txn_Date
FROM Store_Information
ORDER BY Sales DESC;

sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Result:

Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
Boston 700 Jan-08-1999

Example 2: [TOP argument] is a percentage

  • To show the top 25% of sales amounts from Table Store_Information, we key in
SELECT TOP 25 PERCENT Store_Name, Sales, Txn_Date
FROM Store_Information
ORDER BY Sales DESC;
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Result:

Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999

SQL Top Example

SQL Top Example

This tutorial provides more the basic needs and informations on sql tutorial , pl sql tutorial , mysql tutorial , sql server , sqlcode , sql queries , sql , sql formatter , sql join , w3schools sql , oracle tutorial , mysql , pl sql , learn sql , sql tutorial for beginners , sql server tutorial , sql query tutorial , oracle sql tutorial , t sql tutorial , ms sql tutorial , database tutorial , sql tutorial point , oracle pl sql tutorial , oracle database tutorial , oracle tutorial for beginners , ms sql server tutorial , sql tutorial pdf

Related Searches to SQL Top | Top Command in SQL