sql高级查询50例

发布时间:2021-01-07编辑:脚本学堂
sql server高级查询语句的例子,五十个sql查询语句的实例代码,学习下sql语句中统计、排序、汇总等函数的用法。
26、查询每门课程被选修的学生数
 

复制代码 代码示例:
select cid,count(sid) from t_score group by cid;

27、查询出只选修了一门课程的全部学生的学号和姓名
 

复制代码 代码示例:
select t1.sid,t2.sname,count(t1.cid) AS 选课数 from t_score t1,t_student t2
where t1.sid=t2.sid
group by t1.sid ,t2.sname
having count(t1.cid)=1;

28、查询男生、女生人数
 

复制代码 代码示例:
Select count(ssex) as 男生人数 from t_student group by ssex having ssex='男';
Select count(ssex) as 女生人数 from t_student group by ssex having ssex='女';

29、查询姓“张”的学生名单
 

复制代码 代码示例:
SELECT sname FROM t_student WHERE sname like '张%';

30、查询同名同性学生名单,并统计同名人数
 

复制代码 代码示例:
select sname,count(*) from t-student group by sname having count(*)>1;

31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
 

复制代码 代码示例:
select sname, CONVERT(char(11),DATEPART(year,sage)) as age
from t_tudent
where CONVERT(char(11),DATEPART(year,Sage))='1981';

32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
 

复制代码 代码示例:
Select cid,Avg(grade ) from t_score group by cid order by Avg(grade ),cid DESC

33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
 

复制代码 代码示例:
select t1.sname, t2.sid ,avg(t2.grade ) from t_student t1, t_score t2
where t1.sid=t2.sid group by t2.sid,t1.sname
having avg(t2.grade )>85;

34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
 

复制代码 代码示例:
Select t1.sname,isnull(t2.grade ,0) from t_student t1, t_score t2,t_course t3
where t2.sid = t1.sid and t2.cid = t3.cid
and t3.cname='数据库'and t2.grade <60;

35、查询所有学生的选课情况;
 

复制代码 代码示例:
SELECT t1.sid, t1.cid,t2.sname,t3.cname FROM t_score t1,t_student t2,t_course t3
where t1.sid = t2.sid and t1.cid = t3.cid ;

36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
 

复制代码 代码示例:
SELECT distinct t1.sid,t1.sname,t2.cid, t2.grade FROM t_student t1, t_score t2
WHERE t2.grade >=70 AND t2.sid = t1.sid;

37、查询不及格的课程,并按课程号从大到小排列
 

复制代码 代码示例:
select cid from t_score where grade < 60 order by cid ;

38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
 

复制代码 代码示例:
select t1.sid,t2.sname from t_score t1,t_student t2
where t1.sid = t2.sid and t1.grade > 80 and t1.cid = '003';

39、求选了课程的学生人数
 

复制代码 代码示例:
select count(*) from t_score;

40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
 

复制代码 代码示例:
select t1.sname,t2.grade from t_student t1, t_score t2,t_course t3,t_teacher t4
where t1.sid = t2.sid
and t2.cid = t3.cid
and t3.tid = t4.tid
and t4.Tname = '叶平'
and t2.grade = (select max(grade )from t_score where cid=t3.cid );

41、查询各个课程及相应的选修人数
 

复制代码 代码示例:
select count(*) from t_score group by cid

42、查询不同课程成绩相同的学生的学号、课程号、学生成绩
 

复制代码 代码示例:
select distinct t1.sid,t2.grade from t_score t1 , t_score t2
where t1.grade = t2.grade and t1.cid <>t2.cid ;

43、查询每门功成绩最好的前两名
 

复制代码 代码示例:
SELECT t1.sid as 学生ID,t1.cid as 课程ID,grade as 分数 FROM t_score t1
WHERE grade IN (
SELECT TOP 2 grade FROM t_score
WHERE t1.cid = cid ORDER BY grade DESC
)
ORDER BY t1.cid;

44、统计每门课程的学生选修人数(超过10人的课程才统计)。
要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列
 

复制代码 代码示例:
select cid as 课程号,count(*) as 人数 from t_score
group by cid
order by count(*) desc,cid

45、检索至少选修两门课程的学生学号
 

复制代码 代码示例:
select sid from t_score group by sid having count(*) > = 2

46、查询全部学生都选修的课程的课程号和课程名
 

复制代码 代码示例:
select cid,cname from t_course where cid in (select cid from t_score group by cid)

47、查询没学过“叶平”老师讲授的任一门课程的学生姓名
 

复制代码 代码示例:
select sname from t_student
where sid not in (
select sid from t_course t1,t_teacher t2, t_score t3
where t1.tid = t2.tid
and t3.cid =t 1.cid
and t2.tname = '叶平'
);

48、查询两门以上不及格课程的同学的学号及其平均成绩
 

复制代码 代码示例:
select sid,avg(isnull(grade ,0)) from t_score
where sid in (
select sid from t_score
where grade <60 group by sid having count(*)>2
)
group by sid;

49、检索“004”课程分数小于60,按分数降序排列的同学学号
 

复制代码 代码示例:
select sid from t_score where cid='004'and grade <60 order by grade desc;

50、删除“002”同学的“001”课程的成绩
 

复制代码 代码示例:
delete from t_score where sid='001'and cid = '001';