with Cte_Test as
select Table1.ID,Table1.RollNo
from
Table1 inner join Table2 on Table1.RollNo = Table2.RollNo and Table1.ID = Table2.ID
where Table1.Subject='Maths' and Table2.Category='Complusary'
union
select Table1.ID,Table1.RollNo
from
Table1 inner join Table2 on Table1.RollNo = Table2.RollNo and Table1.ID = Table2.ID
where Table1.Subject='Maths' and Table2.Category='Elective')
select count(1) from cte_test
or something like this
Select count(1) from (select Table1.ID,Table1.RollNo
from
Table1 inner join Table2 on Table1.RollNo = Table2.RollNo and Table1.ID = Table2.ID
where Table1.Subject='Maths' and Table2.Category='Complusary'
union
select Table1.ID,Table1.RollNo
from
Table1 inner join Table2 on Table1.RollNo = Table2.RollNo and Table1.ID = Table2.ID
where Table1.Subject='Maths' and Table2.Category='Elective')as A