Suppose we have a table says tbl_students whose structure is given below:-
Create table tbl_students(Studentid int identity(1,1) , Studentname nvarchar(150))
Create table tbl_students(Studentid int identity(1,1) , Studentname nvarchar(150))
Suppose it contains the following data:-
Studentid Studentname
1 Vivek Johari
2 Chandra Singh
4 Avinash Dubey
6 Akhil
7 Sunny Rana
10 Saurabh
As one can see from the data that some rows are deleted from the table as some studentids are missing.
So write a query which gives the studentid which are deleted from this table.
WITH DeletedRows (missnum, maxid)
AS
(
SELECT 1 AS missnum, (select max(id) from tbl_Students) UNION ALL SELECT missnum + 1, maxid FROM DeletedRows
WHERE missnum < maxid
)
SELECT missnum FROM DeletedRows
LEFT OUTER JOIN EMPLOYEE e on e.id = DeletedRows.missnum
WHERE e.id is NULL OPTION (MAXRECURSION 0)
Hi Avinash
Really Appreciate your response. Just a modification in your answer and also @Arjan good catch:-)
WITH DeletedRows (missnum, maxid)
AS
(
SELECT 1 AS missnum, (select max(Studentid) from tbl_Students) UNION ALL SELECT missnum + 1, maxid FROM DeletedRows
WHERE missnum < maxid
)
SELECT missnum FROM DeletedRows
LEFT OUTER JOIN tbl_Students e on e.Studentid = DeletedRows.missnum
WHERE e.Studentid is NULL OPTION (MAXRECURSION 0)
Just changing the table name from employee to tbl_students.:-)
very good Keep Going………
I really appreciate your effort…great work….keep it up… 🙂
Thanks Avinash For your Valuable comments 🙂
The solution Avinash came up with is missing one thing… what if student 11 was removed?
I checked for the current identity in my solution to make sure there are no "hidden" removed students:
WITH Numbers AS (
SELECT 1 AS Number
UNION ALL
SELECT Number + 1
FROM Numbers
WHERE Number < IDENT_CURRENT('tbl_students')
)
SELECT Number
FROM Numbers N
LEFT JOIN tbl_students S ON N.Number = S.StudentId
WHERE S.StudentId IS NULL
Please comment.
@Arjan Dingemans- Good catch 🙂
Thanks Arjan for the correct answer. Good going………..
100% Correct Answer
select number from master..spt_values s
left join tbl_students on s.number = tbl_students.studentid
where s.type='P'
and s.number < (select MAX(studentid) from tbl_students)
and tbl_students.studentid is null order by number asc
@arjan Dingemans Good solution
select number from master..spt_values s
left join tbl_students on s.number = tbl_students.studentid
where s.type='P'
and s.number < (select MAX(studentid) from tbl_students)
and tbl_students.studentid is null and number > 0
This query will also run when the studentid column is not an identity column
Good Solution Aviral…… Keep going …:-)
select number from master..spt_values s
left join tbl_students on s.number = tbl_students.studentid
where s.type='P'
and s.number <= IDENT_CURRENT('tbl_students')
and tbl_students.studentid is null and number > 0
This is the working solution by using master table @aviral lamba as in your solution too if the last rows are deleted by the user then your solution will be incorrect. 🙂
@chandra prakash singh Good catch :-p