SQL TOP



  SQL > Advanced SQL > Top

In the previous section, we saw how LIMIT can be used to retrieve a subset of records in MySQL. In Microsoft SQL Server, this is accomplished using the TOP keyword.

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. [N'] PERCENT: The number of records corresponding to N'% of all qualifying records are returned.

For example, we may wish to show the two highest sales amounts in Table Store_Information,

Table Store_Information
store_nameSalesDate
Los Angeles$1500Jan-05-1999
San Diego$250Jan-07-1999
San Francisco$300Jan-08-1999
Boston$700Jan-08-1999

we key in,

SELECT TOP 2 store_name, Sales, Date
FROM Store_Information
ORDER BY Sales DESC;


Result:

store_nameSalesDate
Los Angeles$1500Jan-05-1999
Boston$700Jan-08-1999

Alternatively, if we want to show the top 25% of sales amounts from Table Store_Information, we key in,

SELECT TOP 25 PERCENT store_name, Sales, Date
FROM Store_Information
ORDER BY Sales DESC;


Result:

store_nameSalesDate
Los Angeles$1500Jan-05-1999

Next: SQL Subquery




Copyright © 2013 1keydata.com   All Rights Reserved.     Privacy Policy


SQL UNION
SQL UNION ALL
SQL Inline View
SQL INTERSECT
SQL MINUS
SQL LIMIT
SQL TOP
SQL Subquery
SQL EXISTS
SQL CASE
SQL DECODE
SQL AUTO INCREMENT
SQL IDENTITY
SQL SEQUENCE And NEXTVAL
SQL NULL
SQL ISNULL
SQL IFNULL
SQL NVL
SQL COALESCE
SQL NULLIF
SQL Rank
SQL Median
SQL Running Totals
SQL Percent to Total
SQL Cumulative Percent to Total


SQL Video Tutorial
SQL Jobs




Site Map
Resources