Introduction:
In this article I will explain how to write SQL query to use distinct keyword with Top statement in SQL Server.
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. |
|||
|
|||
1 comments :
Thanks for the insights on learning about distinct keywords in SQL.
Note: Only a member of this blog may post a comment.