Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

SQL Server - Query to use Distinct Keyword with Top Statement in SQL Server

Jun 28, 2012
Introduction:

In this article I will explain how to write SQL query to use distinct keyword with Top statement in SQL Server.

Description:

In previous posts I explained Replace function, substring function to get particular part of string and many articles relating to SQL Server. Now I will explain how to use distinct keyword with top statement in sql server.

Generally by using Distinct keyword we can get distinct records from datatable that declaration will be like this


SELECT DISTINCT EmpId,EmpName,Roles FROM EmployeeDetails
Here in above query distinct will apply for all the mentioned columns it will return distinct column values of EmpId, EmpName and Roles. In this way we can get distinct records from datatable by using DISTINCT keyword. In one situation I got requirement like use Distinct keyword with Top statement in SQL Server for that I written query like this


SELECT DISTINCT TOP 10 * FROM EmployeeDetails
And we can declare distinct keyword with column values as show below.


SELECT DISTINCT TOP 10 EmpId,EmpName,Roles FROM EmployeeDetails
Note: DISTINCT keyword always operates on all columns in the final result

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

1 comments :

Alin said...

Thanks for the insights on learning about distinct keywords in SQL.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.