SQL PRACTICE: SQL SERVER - Unique Records
Q2: How to select unique values in SQL?
N.B: I am using the following:
Database: MSSQL
SQL Dialect: T-SQL
Below is an employee table given. It has 3 fields and 7 records.
Table1: Employee Table
To find unique values of the table, I am going to use three different ways:
1. Using distinct clause
select distinct EmpId, Name, Salary from employee;
Table2: Distinct Clause Method
2. Using Group by (Without using distinct keyword)
select EmpId, Name, Salary from employee
group by EmpId, Name, Salary;
Table3. Group By Method
select Empid, Name, Salary, ROW_NUMBER() over(partition by empid, Name, Salary order by empid) as Row_Num
from employee;
Result:
Table4. Row_Number Function
Comments
Post a Comment