SQL Server BasicsWhat is SQL Server database?What is RDBMS?What is Normalization?Why we use Denormalization?What_is_SQL?What is PL/SQL?Difference between SQL and PL/SQLDatabase TableOne to One RelationshipOne to Many RelationshipMany to Many RelationshipMany to One RelationshipString Data TypesNumber Data TypesDate Data TypesOther Data TypesCreate DatabaseDrop DatabaseCreating and Managing Users in SQL ServerCreate TableAlter TableDrop TableConstraints in SQL serverPrimary KeyForeign KeyUnique KeyCandidate KeyComposite KeyDifference between primary key and candidate keyPrimary key and foreign key relationshipSurrogate KeyCascading Referential Integrity ConstraintsSelf Referential Integrity ConstraintsInsert into statementInsert multiple recordsUpdate statementDelete statementTruncate statementDifference between Delete and TruncateAlias in SQL ServerSelect statementSelect DistinctSelect TopSelect IntoNull Functions(ISNULL(),NULLIF(),COALESCE())Sub QueryIdentity ColumnSequence objectDifference between sequence and identity columnSQL Server ClausesWHERE ClauseOrder By ClauseTop N ClauseGroup By ClauseHaving ClauseDifference between Where and HavingSQL Server OperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAny OperatorsAll OperatorsUnion OperatorsUnion All OperatorsDifference between Union and Union AllIntersect OperatorExcept OperatorDifference between Except and IntersectJoinsInner JoinLeft JoinRight JoinFull JoinSelf JoinCross JoinViewsWhat are views?Create views using SSMSIndexed ViewsComplex ViewsCheck Option in ViewCheck Encryption in ViewSchema Binding Option in ViewRead-only ViewsUpdatable ViewsAdvantages and disadvantages of viewsCreate multiple views on one tableCan we implement index on views?Can we Perform Insert, update, delete operation on views?Stored Procedure and FunctionsWhat are Stored Procedures?Why we use stored procedures?Passing parameters to Stored procedureUser-Defined FunctionsDifference between UDF and Stored procedurePre-Defined Functions@@Indentity and Scope_IndentityNULLIF, ISNULL and COALESCE

Select Top in SQL Server

The SELECT TOP statement in SQL Server is used to retrieve a specified number of rows or a percentage of rows from a result set. It allows you to limit the number of rows returned by a query. Here's the basic syntax for using SELECT TOP:


SELECT TOP (expression) column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column;

Let's break down the components of the SELECT TOP statement:

  • SELECT TOP (expression): Specifies the number of rows to be returned. The expression can be a numeric value or a percentage.
  • column1, column2, ...: The columns you want to retrieve from the table.
  • FROM table_name: Specifies the table from which to retrieve the data.
  • WHERE condition: Optional clause that allows you to specify conditions to filter the rows before applying the TOP operation.
  • ORDER BY column: Optional clause that specifies the column(s) to sort the result set. The TOP operation is applied after the sorting.

Here are some examples of using SELECT TOP in SQL Server:

1- Retrieve a specific number of rows:


SELECT TOP 10 FirstName, LastName
FROM Employees;

This example retrieves the top 10 rows from the "Employees" table, returning only the "FirstName" and "LastName" columns.

2- Retrieve a percentage of rows:


SELECT TOP 25 PERCENT City, Country
FROM Customers;

In this example, the top 25 percent of rows from the "Customers" table are retrieved, including the "City" and "Country" columns.

3- Retrieve top rows with a condition:


SELECT TOP 5 ProductName, UnitPrice
FROM Products
WHERE Discontinued = 0
ORDER BY UnitPrice DESC;

This example retrieves the top 5 rows from the "Products" table where the "Discontinued" column is 0. The result is sorted in descending order based on the "UnitPrice" column.

The SELECT TOP statement is useful when you want to retrieve a specific number of rows or a percentage of rows from a table. It allows you to limit the result set and focus on the top rows based on a specified condition or sorting order. It's important to note that if there are ties in the sorting order, the exact number of rows returned by SELECT TOP may vary.