SQL SELECT TOP

SQL SELECT TOP Clause: A Comprehensive Guide

SQL SELECT TOP Clause: A Comprehensive Guide

Interested in above project ,Click Below
WhatsApp
Telegram
LinkedIn

SQL SELECT TOP Clause

The SELECT TOP statement in SQL is used to retrieve a limited number of records from a database table. It helps in fetching only the required data when dealing with large datasets, ensuring efficient query performance.

The TOP clause specifies how many rows should be returned in the output. It is particularly useful when working with thousands of records stored in a database table.

Complete Advance AI topics:- CLICK HERE
Complete Python Course with Advance topics:-Click here

Understanding the SELECT TOP Clause

Imagine you have a table containing a large number of student records. Using the SELECT TOP clause, you can control how many student details are retrieved at once, making data retrieval more manageable and efficient.

Note: Not all database systems support the TOP keyword for limiting records. Alternatives include:

  • Oracle: Uses the ROWNUM keyword.
  • MySQL: Uses the LIMIT keyword.

Syntax of the TOP Clause in SQL

SELECT TOP number | PERCENT column_name1, column_name2, ..., column_nameN  
FROM table_name  
WHERE [condition];  

  • number: Specifies the number of rows to be retrieved.
  • PERCENT: Retrieves a percentage of the total rows.
  • column_name: The columns to be displayed.
  • WHERE: Specifies conditions for filtering data (optional).

Examples of Using the TOP Clause

Example 1: Retrieving Car Data

Suppose we have a Cars table with the following columns and data:

Car_NameCar_ColorCar_Cost
Tesla Model 3Red50,00,000
BMW X5Black65,00,000
Audi Q7White80,00,000
Mercedes GLEBlack85,00,000
Ford EndeavourBlue40,00,000

SQL Query:

See also  SQL DROP DATABASE: A Comprehensive Guide

SELECT TOP 3 Car_Name, Car_Color FROM Cars;

Output:

Car_NameCar_Color
Tesla Model 3Red
BMW X5Black
Audi Q7White

Example 2: Retrieving Student Data

Consider the following Student table:

Stu_IDStu_NameStu_Marks
2001Raj90
2002Simran85
2003Rahul80
2004Anjali88
2005Rohit76

SQL Query:

SELECT TOP 4 * FROM Student;

Output:

Stu_IDStu_NameStu_Marks
2001Raj90
2002Simran85
2003Rahul80
2004Anjali88

Example 3: Filtering Employee Data

Emp_IdEmp_NameEmp_SalaryEmp_City
101Aakash50,000Mumbai
102Priya45,000Delhi
103Vikram40,000Mumbai
104Neha55,000Bangalore
105Arjun60,000Mumbai

SQL Query:

SELECT TOP 2 * FROM Employee WHERE Emp_City = 'Mumbai';

Output:

Emp_IdEmp_NameEmp_SalaryEmp_City
101Aakash50,000Mumbai
103Vikram40,000Mumbai

Example 4: Retrieving a Percentage of Bike Data

Bike_NameBike_ColorBike_Cost
Honda CBRRed3,00,000
Royal EnfieldBlack2,50,000
Bajaj PulsarBlue1,50,000
Yamaha FZWhite1,80,000
KTM DukeOrange2,70,000

SQL Query:

SELECT TOP 50 PERCENT * FROM Bikes;

Output:

Bike_NameBike_ColorBike_Cost
Honda CBRRed3,00,000
Royal EnfieldBlack2,50,000

Alternative Clauses in Other Databases

MySQL LIMIT Clause

Syntax:

SELECT column_name1, column_name2 FROM table_name LIMIT value;

Example:

SELECT * FROM Cars LIMIT 3;

Oracle ROWNUM Clause

Syntax:

SELECT column_name1, column_name2 FROM table_name WHERE ROWNUM <= value;

Example:

SELECT * FROM Cars WHERE ROWNUM <= 3;

Download New Real Time Projects :-Click here

Conclusion

The SELECT TOP clause is an efficient way to retrieve a specific number of records from large datasets. While SQL Server uses TOP, other databases like MySQL and Oracle have their respective alternatives, such as LIMIT and ROWNUM. By understanding and utilizing these clauses effectively, database management becomes more efficient and optimized.

See also  Understanding SQL DELETE Statement with Example


select top 10 records in sql server
select first 10 rows sql oracle
select top 10 records in mysql
select top 10 rows in oracle sql developer
select first row sql
sql top vs limit
select top 100 rows in oracle
sql limit
sql joinsql select top
sql select top oracle
sql select top rows
sql select top example
sql select top w3schools

🎓 Need Complete Final Year Project?

Get Source Code + Report + PPT + Viva Questions (Instant Access)

🛒 Visit UpdateGadh Store →
💬 Chat Now