0

I have two tables

BatchDetails

BatchCode    MaxStd
------------------
B001          12
B002          14
B003          10

AdmissionBatch

Batch  Rollno
---------------
B001    1
B001    2
B002    3
B003    4
B003    5

I need the BatchCode of those batches are not have max students. I wrote a query but it is not working

select 
    batchCode, MaxStd, count(rollno)  
from 
    BatchDetails as a 
join 
    AdmissionBatch as b on a.batchcode = b.batch  
group by  
    batchcode, maxStd 
having 
    count(rollno) < maxSTD

This query is not working because if there is not student in a particular batch then the batch will not appear

I tried subquery as well but no help

Please help

1
  • Maybe one of the answers are your solution already, but I do have doubts... What exactly are you trying to achieve? Let's say, that there is no row in BatchDetails with a BatchCode B002. What would be the expected output? What, if the MaxStd is zero (or NULL)? Commented Mar 1, 2016 at 9:53

2 Answers 2

1

Use left join instead of inner join.

select batchCode,MaxStd, count(rollno)  from BatchDetails as a 
LEFT JOIN AdmissionBatch  as b on a.batchcode=b.batch  
group by batchcode,maxStd 
having count(rollno)< maxSTD
Sign up to request clarification or add additional context in comments.

Comments

1

You need a left join to keep the results even when there's no match :

select batchCode,MaxStd, count(rollno)
from BatchDetails as a 
left outer join AdmissionBatch  as b
 on a.batchcode=b.batch  
group by batchcode,maxStd 
having count(rollno)< maxSTD

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.