Skip to content

Commit

Permalink
Merge pull request #1807 from souvikpramanikgit/add-sqltop
Browse files Browse the repository at this point in the history
Add SQL select Top
  • Loading branch information
ajay-dhangar authored Nov 4, 2024
2 parents 93b071b + e92fdd1 commit b512c7e
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions docs/languages/SQL/sql-22.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
id: sql-select-top
sidebar_position: 22
title: "SQL SELECT TOP"
sidebar_label: "SQL SELECT TOP"
description: "The SQL SELECT TOP clause is used to limit the number of rows returned in a result set."
tags: [sql, dbms, database, select top]
---

The `SELECT TOP` clause is used in SQL to limit the number of rows returned in a query result. It is commonly used when you want to retrieve a specific number or percentage of records.

### Syntax

In SQL Server:

```sql
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
```
In MySQL and PostgreSQL, you can use LIMIT instead:

```sql
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
```

### Key Points

* The SELECT TOP clause is supported in SQL Server, while MySQL and PostgreSQL use the LIMIT clause instead.
* You can specify either a fixed number or a percentage of rows to return.
* This clause is especially useful for retrieving a subset of rows from large datasets, such as the top-performing items or recent entries.

### Examples

**Example 1: Retrieve the top 5 most expensive products**

```sql
SELECT TOP 5 ProductName, Price
FROM Products
ORDER BY Price DESC;
```
In MySQL/PostgreSQL, use:

```sql
SELECT ProductName, Price
FROM Products
ORDER BY Price DESC
LIMIT 5;
```

**Example 2: Retrieve the top 10% of employees with the highest salaries**

```sql
SELECT TOP 10 PERCENT EmployeeName, Salary
FROM Employees
ORDER BY Salary DESC;
```

In MySQL/PostgreSQL, you would need to calculate the percentage manually, as they don’t support the PERCENT keyword in the same way.

**Note:**
The SELECT TOP clause is typically used with ORDER BY to control which records are selected based on specific criteria, such as descending order of price or date. Keep in mind that different SQL databases have variations in syntax for limiting rows.

0 comments on commit b512c7e

Please sign in to comment.