SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. TOP clause

Text copied!

« Previous
Next »

TOP clause

May 14, 2023, 4:40 p.m. under SQL

  • Hi! You should check UNION ALL clause post first.

    SQL clause is the specific part of a SQL statement which is used to perform various operations. It can be combined to create more complex queries to retrieve and manipulate data.

    TOP clause :

    TOP clause is used to fetch specified number or percentage of rows from a table.

    It is often used with other clauses such as 'SELECT' and 'FROM' etc. to retrieve, specify table and manipulate data.

    The syntax of the TOP clause generally looks like this :
    SELECT TOP n column_name(s)
    FROM table_name;
            

    OR

    SELECT TOP n PERCENT column_name(s)
    FROM table_name;
            

    • Specify the column(s) name after the 'SELECT' keyword.

    • Specify Asterisk (*) symbol to selects all columns from the table after the 'SELECT' keyword.

    • Specify the table name after the 'FROM' keyword.

    • Specify number or percentage of rows that you want to retrieve after the 'TOP' keyword.

    Here's an example of how you might use the TOP clause :

    1. Let's assume we have a table named "[Employees]".

    TOP clause
    Case - 1 :

    Let's assume you want to retrieve only specific number of rows from [Employees] table.

    Run below SQL statement :

    SELECT TOP 3 *
    FROM [Employees];
            

    Above statement will select only top 3 rows from the [Employees] table.

    TOP clause

    That's it! You have successfully retrieved top 3 rows from [Employees] table.

    Case - 2 :

    Let's assume you want to retrieve only specific percentage of rows from [Employees] table.

    Run below SQL statement :

    SELECT TOP 40 PERCENT *
    FROM [Employees];
            

    Above statement will select only top 2 rows, considering that [Employees] table contained total 5 rows. 40 percent of 5 rows is equates to 2 rows.

    TOP clause

    That's it! You have successfully retrieved top 40 percent of rows from [Employees] table.

    Frequently Asked Questions :

    What does the top clause do in SQL?

    The TOP clause in SQL specifies the number of rows to return from a query result.

    What is the top n clause in SQL Server?

    The TOP n clause in SQL Server limits the result set to the specified number of rows.

    What is the alternative for top clause?

    An alternative to the TOP clause in other database systems like MySQL is the LIMIT clause.

    How do I select top 5 records in SQL?

    To select the top 5 records in SQL, you can use the "SELECT TOP 5" statement in SQL Server.
    Thank You! You should check DISTINCT clause post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy