ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 数据库:SQL“查询同名同姓学生名单,并统计同名人数”延伸

数据库:SQL“查询同名同姓学生名单,并统计同名人数”延伸

原创 Linux操作系统 作者:果果的空间 时间:2013-08-14 10:49:51 0 删除 编辑
/*30、查询同名同姓学生名单,并统计同名人数*/
Select Sname,count(*) as NUM from Student
 group by Sname having count(*)>1

/*30、查询同名同性学生名单,并统计同名男生人数*/
Select Sname,Ssex,count(*) as num from Student
 group by Sname,Ssex having count(*)>1 and Ssex='男'
 
\*39求选了课程的学生人数 *\
select COUNT(*), s.Sname from Student s, SC c where s.S# = c.S#
select  COUNT(distinct S#) from SC;

/*计算每个学生选了什么课程*/
select count(aa.S#) , aa.C# from (select SC1.S#, SC1.C# from SC SC1 where SC1.S# in
(select SC2.S# from SC SC2 group by SC2.S#))  as aa group by aa.S#,aa.C#
 
select S#,C# from SC SC2 group by S#,C#
select count(s.Sname) from Student s where s.S# in (select distinct c.S# from SC c)
select count(distinct s.Sname) from Student s;

\*39求选了(不同)课程的学生人数 *\
select C#, count(distinct S#) from SC group by C# order by C#

select COUNT(distinct S#) from SC where C#=23;
select * from SC c where c.S#=1;

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29056818/viewspace-768458/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-07-18

  • 博文量
    60
  • 访问量
    114928