Declare @FacultyName Nvarchar(100) SELECT C.ContentTypeId,C.Id,V.VersionId,V.VersionName,C.CourseName,C.DeptName,V.ContentId,V.VersionNote FROM [dbo].[ContentDetails] C inner join [dbo].[VersionDetails] V on C.Id=V.ContentId where V.VersionId in ( select Max(V.VersionId) FROM [dbo].[ContentDetails] C inner join [dbo].[VersionDetails] V on C.Id=V.ContentId where c.FacultyName like @FacultyName group by V.ContentId
) order by V.VersionId desc