TOP Clause in Microsoft SQL Server
Last Updated :
13 Jun, 2024
TOP clause in Microsoft SQL Server fetches a limited number of rows from a database.
The SELECT TOP clause is very useful when dealing with large databases. The TOP clause is useful for fetching the data records in larger datasets as it reduces the complexity.
Syntax
TOP clause syntax in Microsoft SQL Server is:
SELECT TOP value column1, column2
FROM table_name;
Syntax Using Percent
SELECT TOP value PERCENT column1, column2
FROM table_name;
Microsoft SQL Server TOP Clause Examples
Let's look at some examples of the TOP clause in Microsoft SQL Server.
First let's create a demo table, on which we will run the TOP clause query.
Demo Table
Query:
CREATE TABLE Customer(
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
(2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
(3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
(4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
(5, 'Nishant. Salchichas S.A.', 'Jain', 'Spain','22','xxxxxxxxxx');
Output:
Customer TableUsing TOP Clause in Microsoft SQL Server Example
To fetch the first two data sets from the Customer table.
SELECT TOP 2 * FROM Customer;
Output
OutputSELECT TOP with WHERE clause example
We can fetch data records by using a where clause with some condition was well.
Query:
SELECT TOP 1 * FROM Customers
WHERE Country='Spain';
Output:
OutputNote: To get the same functionality on MySQL and Oracle databases there is a bit of difference in the basic syntax.
For MySQL databases
SELECT column1,column2
FROM table_name LIMIT value;
For Oracle databases
SELECT column1,column2
FROM table_name
WHERE ROWNUM <= value;
Similar Reads
How to Limit Rows in a SQL Server? To limit rows in SQL Server, use the TOP clause in the SELECT statement. Using the TOP clause in SQL Server, users can limit the number of rows in the results set. Here, we will understand how to limit rows in SQL Server with the help of different examples. Steps to Limit Rows in SQL ServerLet's che
3 min read
Select top in MS SQL Server Prerequisite - Select in MS SQL Server Suppose that a user wants to extract the top students from the whole institution but has to use some complex queries to extract the data. To avoid complexity, the user can use 'Select Top'. 'Select Top' extracts the limited number of rows. This results in accur
2 min read
SQL TOP, LIMIT, FETCH FIRST Clause SQL TOP, LIMIT, and FETCH FIRST clauses are used to retrieve a specific number of records from a table. These clauses are especially useful in large datasets with thousands of records.Each of these SQL clauses performs a similar operation of limiting the results returned by a query, but different da
8 min read
Joins in MS SQL Server A database comprises tables and each table in case of RDBMS is called a relation. Let us consider a sample database named University and it has two tables named Student and Marks. If a user wants to transfer a certain set of rows, insert into select statement is used along with the query. But if a u
2 min read
Introduction of MS SQL Server Data is a collection of facts and figures and we have humungous data available to the users via the internet and other sources. To manipulate the data, Structured Query Language (SQL) in short has been introduced years ago. There are different versions of SQL available in the market provided by diff
2 min read