Search code examples
sqlinner-joindistinct

SQL Distinct not working in inner join


I am joining two tables "tblQuestion" and "tblSubskill", i need distinct SubSkillName in result as shown in query

SELECT DISTINCT ss.SubSkillName , ss.SkillId , ss.Id As SubSkillId 
FROM dbo.tblQuestion AS q INNER JOIN
dbo.tblSubSkill AS ss ON q.SubSkillId = ss.Id

which is working fine untill i add a column from tblQuestion and returns

enter image description here

now i want to add a column from tblQuestion as well so query is as follow

SELECT DISTINCT ss.SubSkillName , ss.SkillId , ss.Id As SubSkillId , q.EadLevel
 FROM dbo.tblQuestion AS q INNER JOIN
 dbo.tblSubSkill AS ss ON q.SubSkillId = ss.Id

but what it returns have not distinct SubSkillName as below

enter image description here


Solution

  • Well, as i observed Distinct keyword is working fine as it judges the difference between two or more rows on the basis of all columns including and since for 1st two rows EadLevel is different which makes the entire combination different from one another. So It's working fine. However if you want only one of them then you have to decide which EadLevel you want to show.