0

The specific question is select rollno from class table who where present in dbms and not present in maths.

rollno sts sub
1      1    dbms
1      0    maths
2      1    dbms
2      0    maths
3      0    dbms
3      1    maths
4      0    dbms
4      0    maths
5      1    dbms
5      1    maths
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76

2 Answers2

0

You can use conditional aggregation for this:

select rollno
from mytable
group by rollno
having sum(sub = 'dbms' and sts = 1) > 0
   and sum(sub = 'maths' and sts = 1) = 0

This uses the fact that true evaluates to 1 and false evaluates to 0 in MySQL.

  • sum(sub = 'dbms' and sts = 1) finds number of rows where sub is dbms for the given rollno, which must be atleast 1
  • sum(sub = 'maths' and sts = 1) finds number of rows where sub is maths for the given rollno, which must be 0.

Demo

Community
  • 1
  • 1
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
0

you could use two subselect with id and not in

select roolno
from my_table 
where sub in (select roolno from my_table  where sub ='dbms')
and sub not in (select roolno from my_table  where sub ='maths' )
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107