LIMIT or TOP keyword in SQL

In SQL, the TOP or Limit keyword is used to limit the number of rows returned in a result set. It is commonly used when we want to retrieve only a specified number of records, for example, the top 10 highest salaries or the top 5 latest orders.

SQL Server Syntax

SELECT TOP (n) [ PERCENT ]
column1, column2, ...
FROM table_name
[WHERE conditions]
[ORDER BY column_name [ ASC | DESC ]]; 

MySQL Syntax

SELECT column1, column2, ...
FROM table_name  
ORDER BY column_name DESC  
LIMIT n;  

Example: Let's say we have a table named Employees with the following data:

Table: Employees

EmployeeIDFirstNameLastNameAge
1AshishGoel25
2KirshanGupta30
3AnjaliSharma25
4ManjuSaini15
5KatrinaKaif18
6EshaDixit20
7AnkitaVerma19
8MeenakshiChikkara35
9AliaBhatt16

Query to get the top 3 highest Ages:

SQL

SELECT EmployeeID, FirstName, Age
FROM Employees
ORDER BY Age DESC Limit 3; 

The result of the above query is shown below:

EmployeeIDFirstNameAge
8Meenakshi35
2Kirshan30
1Ashish25

This query returns the top 3 employees with the highest ages by ordering the data in descending order of the Age column.