T-SQL Tutorials - TSQLTutorials.com

TOP Option


Introduction

The TOP option is used for limiting the output of a query result set. The TOP SQL Statement is often used with the SELECT statement. TOP can either specify the number of rows to return, or the precentage of rows to return. The ORDER BY clause can be used successfully with the TOP option. In other words, the ORDER BY will be completed before the rows are limited.

Using TOP with SELECT Statements

Syntax for returning n number of rows:

SELECT TOP n *
FROM table

Example for returning 10 rows:

SELECT TOP 10 *
FROM employees

Syntax for returning n percentage of rows:

SELECT TOP n PERCENT *
FROM table

Example for returning 20 percent of rows:

SELECT TOP 20 PERCENT *
FROM employees


Using TOP with SELECT Statements using ORDER BY clause

Example for returning top 10 highest paid employees:

SELECT TOP 10 *
FROM employees
ORDER BY Salary DESC


Microsoft SQL Server Standard 2008 R2 10CAL 228-09180

Price: $2040
Buy It Now
SQL Server 2008 2005 Training Exam Test Developer DVD Enterprise Book mcts 5cal

Current Price: $16.99
Current Bids: 0
★ NEW ★ MS SQL 2005 Server Standard with CAL ★ L@@K PRICE REDUCTION★

Price: $395
Buy It Now
Microsoft SQL Server 2008 - Enterprise with 25 CAL's

Price: $13850
Buy It Now
228-04023 Microsoft SQL Server 2005 Standard 5 CAL

Price: $1450
Buy It Now
Microsoft SQL Server 2005 Workgroup Edition 5 CAL, New Retail, PN A5K-01017

Price: $699.95
Buy It Now







Copyright 2012 - TSQLTutorials.com