博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL语句复习
阅读量:6080 次
发布时间:2019-06-20

本文共 2849 字,大约阅读时间需要 9 分钟。

表结构:
SQL练习:
/*--1、查询课程1的成绩 比 课程2的成绩 高 的所有学生的学号;*/
SELECT A.sno FROM(SELECT sno,score FROM sc WHERE cno=1 )as A,(SELECT sno,score FROM sc WHERE cno=2 )as bWHERE A.score>b.score or A.score = b.score
/*--2、查询平均成绩大于60分的同学的学号和平均成绩;*/
SELECT sno,AVG(score) FROM sc GROUP BY sno HAVING AVG(score) >60
/*--3、查询所有同学的学号、姓名、选课数、总成绩;*/
/*我的回答1*/
SELECT A.sno,A.score,cno,B.sname FROM(SELECT sc.sno,score,COUNT(cno) cno FROM sc GROUP BY sc.sno )as A,(SELECT student.sno,sname FROM student) as BWHERE A.sno = B.sno
/*我的回答2*/
SELECT A.sno,sum(A.score),count(A.cno),B.sname	FROM sc as A,student as B	WHERE A.sno = B.sno	GROUP BY A.sno  
/*其他回答*/
SELECT A.sno,A.score,COUNT(A.cno),B.sname FROMsc as A, student as B WHERE A.sno = B.sno GROUP BY A.cno

/*--4、查询姓“李”的老师的个数;*/

SELECT COUNT(*) FROM teacher where tname like '%李%'

/*--5、查询没学过“叶平”老师课的同学的学号、姓名;*/

/*我的回答1*/

SELECT distinct A.sno,D.sname FROM(SELECT sno,cno FROM sc ) as A,(SELECT tno FROM teacher WHERE tname='叶平') as B,(SELECT cno,tno FROM course) as C,(SELECT sname,sno FROM student) as DWHERE B.tno != C.tno AND A.cno = C.cno AND A.sno = D.sno

/*我的回答2*/

SELECT distinct D.sno,D.sname FROM sc as A, teacher as B,course as C,student as DWHERE B.tno != C.tno AND A.cno = C.cno AND A.sno = D.snoAND B.tname = '叶平'

/*我的回答3*/

SELECT sno,sname FROM student WHERE sno NOT in (    SELECT sno FROM sc WHERE cno in(        SELECT cno FROM course WHERE tno in(            SELECT tno FROM teacher WHERE tname='叶平'            )        )    )

/*--6、查询同时学过课程1和课程2的同学的学号、姓名;*/

/*我的回答1*/

SELECT A.sno , C.sname FROM (SELECT sno FROM sc WHERE cno = 1) as A ,(SELECT sno FROM sc WHERE cno = 2) as B,(SELECT sno,sname FROM student ) as CWHERE A.sno = B.sno AND A.sno = C.sno

/*我的回答2*/

SELECT sno,sname FROM student WHERE sno in (    SELECT sno FROM sc WHERE cno = 2 and cno in (        SELECT sno FROM sc WHERE cno = 1    ))

/*--7、查询学过“叶平”老师所教所有课程的所有同学的学号、姓名;*/

SELECT sname,sno FROM student WHERE sno in(    SELECT A.sno     FROM sc A,teacher B,course C    WHERE B.tno = C.tno     AND A.cno = C.cno    AND B.tname = '叶平')

/*--8、查询 课程编号1的成绩 比 课程编号2的成绩 高的所有同学的学号、姓名;*/

SELECT sname,C.sno FROM    (SELECT sno,score FROM sc WHERE cno = 1) as A,    (SELECT sno,score FROM sc WHERE cno = 2) as B,    (SELECT sno,sname FROM student ) as CWHERE A.score>B.score AND A.sno = B.sno AND C.sno = A.sno

 

SELECT sname,sno FROM student WHERE sno in (    SELECT A.sno FROM        (SELECT sno,score FROM sc WHERE cno = 1) as A,        (SELECT sno,score FROM sc WHERE cno = 2) as BWHERE A.score>B.score AND A.sno = B.sno)

/*--9、查询所有课程成绩小于60分的同学的学号、姓名;*/

SELECT sno FROM sc WHERE MAX(score)>60 GROUP BY sno ASC /*这样是错误的写法,注意!!!!!!!*/SELECT sno FROM sc GROUP BY sno ASC HAVING(MAX(score)<60)

  

/*--10、查询所有课程成绩大于60分的同学的学号、姓名;*/

SELECT sno,MIN(score) FROM sc GROUP BY sno ASC HAVING(MIN(score)>60)

  

 

转载于:https://www.cnblogs.com/H---/p/9101032.html

你可能感兴趣的文章
Android开发学习笔记(五):Starting an Activity
查看>>
xcode cocos2sx make
查看>>
SQL SERVER数据库服务器排序规则修改
查看>>
/etc/profile和/etc/profile.d/的区别
查看>>
POI支持的颜色对比
查看>>
linux内核编译(fedora16_x86_64)
查看>>
iOS 键盘自适应(IQKeyboardManager)使用小结
查看>>
解决vsftpd用户登录出现500 OOPS: cannot change directory问题
查看>>
修改CentOS发行信息以绕过磁盘阵列多路合并软件的操作系统检查
查看>>
阅读JDK8 CopyOnWriteArraySet应该了解什么
查看>>
Objective-c和Java下DES加密保持一致的方式
查看>>
RequireJS + AngularJS Seed 13 _Grunt-plugin
查看>>
python3统计文件中字符或字符串出现的次数,支持同时多个字符串
查看>>
Redis 的 8 大应用场景!
查看>>
nginx 安装
查看>>
Django学习笔记(1)---引入静态资源
查看>>
鸟哥的Linux笔记----20180113
查看>>
quick-cocos2d-x开发工具sublime text及其强力插件QuickXDev
查看>>
CURL模拟HTTP浏览器动作常用命令-作弊投票利器
查看>>
mysql集群方案整理之Galera Cluster简介
查看>>