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
CREATE TABLE in SQL Server SQL Server provides a variety of data management tools such as querying, indexing, and transaction processing. It supports multiple programming languages and platforms, making it a versatile RDBMS for various applications. With its robust features and reliability, SQL Server is a popular choice for
4 min read
sp_columns - SQL Server In SQL Server, managing and understanding database schemas is crucial for effective database administration and development. The sp_columns stored procedure is a valuable tool for retrieving detailed metadata about the columns of a specified table or view. In this article, We will learn about sp_col
6 min read
Rank and Dense Rank in SQL Server In the world of SQL, the ability to rank data based on specific criteria is important for a wide range of analytical tasks. SQL Server offers powerful functions like RANK() and DENSE_RANK(), which allow users to assign ranks to rows in a dataset. These functions are particularly useful for tasks suc
7 min read
How to Update Top 100 Records in SQL Server SQL Server is a Relational database Management system which is developed by Microsoft and is one of the most used databases in the world. It provides the developer with a set of rich functionalities to create tables, insert data in them, and then manipulate and play with them as and when necessary.
5 min read
How to SELECT Top N Rows For Each Group in SQL Server SQL Serverâs ROW_Number() window function is often used to get the top number of rows in a group. In this article, weâll look at how to get the top N Row Counts in SQL Server using this function. Weâll provide step-by-step instructions, syntax examples, and examples to help you get the results you n
4 min read
How to Find the Maximum of Multiple Columns in SQL Server? When working with SQL Server databases, there are times when we need to find the maximum value among multiple columns. This task can be accomplished using various techniques within SQL queries. By using functions like CASE and GREATEST, SQL Server provides efficient ways to determine the maximum val
4 min read