Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

SELECT TOP Clause



The SQL SELECT TOP Clause


In SQL Server, the SELECT TOP clause is used to limit the number of rows returned by a query. It allows you to specify the maximum number of rows to be returned from the result set.

Here's the basic syntax of the SELECT TOP clause:


SELECT TOP expression column1, column2, ...
FROM table_name
WHERE condition;

In this syntax:

Example:

Let's say we have a table named customers with columns customer_id, first_name, and last_name, and we want to retrieve the top 5 customers based on their customer_id:


SELECT TOP 5 customer_id, first_name, last_name
FROM customers
ORDER BY customer_id;


This query will return the top 5 customers from the customers table, sorted by their customer_id.

You can also use the SELECT TOP clause with a percentage to retrieve a specified percentage of rows from the result set. For example, to retrieve the top 10% of customers:


SELECT TOP 10 PERCENT customer_id, first_name, last_name
FROM customers
ORDER BY customer_id;


The SELECT TOP clause is useful when you need to retrieve a limited number of rows from a large result set, especially for pagination or displaying a subset of data. It's important to note that the specific rows returned by the SELECT TOP clause may vary depending on the order of the data, so it's often used in conjunction with an ORDER BY clause to ensure consistent results. Additionally, the behavior of SELECT TOP may vary slightly depending on the SQL database management system you're using.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java