oracle经典sql面试

  一个学校表,school,列class 班级,name  学生姓名,sex 性别,男为'M',女为'F',一个学校表很多的班级,查询出男生,女生数量相同的班级。

好久不搞sql,几乎都忘光了,特此纪念,

select tem.class from(  
select class,  
       (select count(sex)  
          from schools s1  
         where s1.class = s.class  
           and s1.sex = 'M') AS MCOUNT,  
       (select count(sex)  
          from schools s1  
         where s1.class = s.class  
           and s1.sex = 'F') AS FCOUNT  
  from schools s  
group by class  
) tem where tem.MCOUNT = tem.FCOUNT 

你可能感兴趣的:(oracle)