1叶平 语文 数学 英语 化学
2王老师 语文 , 数学
3陈老师 语文 英语
4郭老师 数学 英语
5谢老师 英语
6何老师 数学
7周老师 化学
8孔老师 语文
– 查询所有学生的学号, 姓名, 选课数, 总成绩
SELECT st.sid, st.sname , AVG(sc.score) ‘平均分’, COUNT(sc.cid) ‘选课数’ , SUM(sc.score
) ‘总分’
FROM stu
st , scores
sc WHERE st.sid=sc.sid
GROUP BY st.sid
– 查询平均成绩大于60分的学号, 姓名
– 题目一:查询有课程得分小于80分的学生情况
SELECT * FROM score WHERE scores<80
SELECT s.sid, s.sname, sc.cid,c.cname, sc.scores FROM stu s LEFT JOIN score sc ON s.sid=sc.sid
LEFT JOIN course c ON sc.cid=c.cid
WHERE sc.scores<80
ORDER BY s.sid,sc.cid
– 题目一:查询所有课程得分都小于80分的学生情况(但不是平均分小于80, 比如 某某的平均分小于80, 数学 1分,语文1分,英语1分,化学81分,这个情况就不符合了)
SELECT * FROM scores sc
– left join stu s on sc.sid = s.sid
WHERE sc.score<80 GROUP BY sc.sid HAVING COUNT(sc.cid)>=4
– 题目一:查询所有课程得分都大于90分的学生情况
SELECT * FROM scores sc
LEFT JOIN stu s ON sc.sid = s.sid
WHERE sc.score>90 GROUP BY sc.sid HAVING COUNT(sc.cid)>=4
– 查询平均分大于90的学生
SELECT * FROM scores sc
LEFT JOIN stu s ON sc.sid = s.sid
GROUP BY sc.sid HAVING AVG(sc.score)>90
– 查询平均分
SELECT * ,AVG(sc.score)FROM scores sc
LEFT JOIN stu s ON sc.sid = s.sid
GROUP BY sc.sid
– 查询总分大于300的学生
SELECT * ,SUM(sc.score) AS s FROM scores sc
LEFT JOIN stu s ON sc.sid = s.sid
GROUP BY sc.sid
HAVING s>300
/*
SQLyog Ultimate v13.1.1 (64 bit)
MySQL - 5.5.27 : Database - studyy
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`studyy` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `studyy`;
/*Table structure for table `course` */
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cid` INT(11) NOT NULL AUTO_INCREMENT,
`cname` VARCHAR(55) DEFAULT NULL,
PRIMARY KEY (`cid`)
) ENGINE=INNODB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
/*Data for the table `course` */
INSERT INTO `course`(`cid`,`cname`