Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SQL select Top #1807

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading