MySQL

时间:2020-05-30
本文章向大家介绍MySQL,主要包括MySQL使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

什么是SQL?

结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。
结构化查询语言是高级的非过程化编程语言,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以具有完全不同底层结构的不同数据库系统, 可以使用相同的结构化查询语言作为数据输入与管理的接口。结构化查询语言语句可以嵌套,这使它具有极大的灵活性和强大的功能。

常用数据库有?

常用:Oracle, Sybase, SQL Server, DB2, MySQL, Access 等等SQL语句在大部分的数据库里面都是通用的,只是部分有些特别。例如:Oracle里面没有Limit等等,其他可以自己使用之后再去了解。

下面是我在学习 SELECT的案例,以及SELECT使用的关键词

SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr, ...
[INTO OUTFILE 'file_name' export_options | INTO DUMPFILE 'file_name']

[FROM table_references

[WHERE where_definition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_definition]

[ORDER BY {col_name | expr | position}
[ASC | DESC] , ...]

[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[FOR UPDATE | LOCK IN SHARE MODE]]

  

例子:表

/*
Navicat MySQL Data Transfer

Source Server         : localhost_3306
Source Server Version : 50719
Source Host           : localhost:3306
Source Database       : chen

Target Server Type    : MYSQL
Target Server Version : 50719
File Encoding         : 65001

Date: 2013-05-30 15:11:52
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
  `cid` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '课程表id',
  `cname` varchar(100) NOT NULL COMMENT '名称',
  `tid` int(11) NOT NULL COMMENT '老师表id',
  PRIMARY KEY (`cid`),
  KEY `idx_cname` (`cname`) USING BTREE,
  KEY `idx_tid` (`tid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='课程表';

-- ----------------------------
-- Table structure for sc
-- ----------------------------
DROP TABLE IF EXISTS `sc`;
CREATE TABLE `sc` (
  `sid` int(11) NOT NULL COMMENT '成绩表id',
  `cid` int(11) NOT NULL COMMENT '课程表id',
  `score` int(5) NOT NULL COMMENT '分数',
  KEY `idx_cid` (`cid`) USING BTREE,
  KEY `idx_score` (`score`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='成绩表';

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `sid` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '学生表id',
  `sname` varchar(100) NOT NULL COMMENT '姓名',
  `sage` int(3) NOT NULL COMMENT '年龄',
  `ssex` varchar(3) NOT NULL COMMENT '性别',
  PRIMARY KEY (`sid`),
  KEY `idx_sname` (`sname`) USING BTREE,
  KEY `idx_sage` (`sage`) USING BTREE,
  KEY `idx_ssex` (`ssex`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='学生表';

-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
  `tid` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '老师表id',
  `tname` varchar(100) NOT NULL COMMENT '姓名',
  PRIMARY KEY (`tid`),
  KEY `idx_tname` (`tname`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='老师表';

 

练习例子:

查询所有学生姓名、性别、科目、成绩、总成绩排名信息
/*获取所有学生的信息*/
select student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', 
course.cid AS '课程号', course.cname AS '学科', sc.score AS '分数',
sc.cid, sc.sid,count(sc.cid), sum(sc.score)
from course, sc, student
GROUP BY student.sid, student.sname, student.sage, sc.cid, sc.sid, course.cid, course.cname, sc.score
HAVING student.sid = sc.sid AND sc.cid = course.cid;

/*成绩是打印出来了,重复的数据太多了*/
SELECT student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', 
course.cid AS '课程号', 
(CASE course.cname WHEN '语文' THEN sc.score ELSE 0 END) AS '语文',
(CASE course.cname WHEN '数学' THEN sc.score ELSE 0 END) AS '数学',
(CASE course.cname WHEN '英语' THEN sc.score ELSE 0 END) AS '英语',
(CASE course.cname WHEN '物理' THEN sc.score ELSE 0 END) AS '物理',
(CASE course.cname WHEN '化学' THEN sc.score ELSE 0 END) AS '化学',
sc.cid, sc.sid,count(sc.cid), sum(sc.score)
FROM course, sc, student
GROUP BY student.sid, student.sname, student.sage, sc.cid, sc.sid, course.cid, course.cname, sc.score
HAVING student.sid = sc.sid AND sc.cid = course.cid
ORDER BY student.sid ASC, sc.cid ASC;

/*用表的联结join*/
SELECT student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', 
course.cid AS '课程号', 
SUM(CASE course.cname WHEN '语文' THEN sc.score ELSE 0 END) AS '语文',
SUM(CASE course.cname WHEN '数学' THEN sc.score ELSE 0 END) AS '数学',
SUM(CASE course.cname WHEN '英语' THEN sc.score ELSE 0 END) AS '英语',
SUM(CASE course.cname WHEN '物理' THEN sc.score ELSE 0 END) AS '物理',
SUM(CASE course.cname WHEN '化学' THEN sc.score ELSE 0 END) AS '化学',
sc.cid, sc.sid,count(sc.cid), sum(sc.score) AS '总分'
FROM student
LEFT OUTER JOIN sc ON student.sid = sc.sid
LEFT OUTER JOIN course ON sc.cid = course.cid
GROUP BY student.sid, student.sname, student.sage
ORDER BY student.sid;

/*成绩排序*/
SELECT student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', 
course.cid AS '课程号', 
SUM(CASE course.cname WHEN '语文' THEN sc.score ELSE 0 END) AS '语文',
SUM(CASE course.cname WHEN '数学' THEN sc.score ELSE 0 END) AS '数学',
SUM(CASE course.cname WHEN '英语' THEN sc.score ELSE 0 END) AS '英语',
SUM(CASE course.cname WHEN '物理' THEN sc.score ELSE 0 END) AS '物理',
SUM(CASE course.cname WHEN '化学' THEN sc.score ELSE 0 END) AS '化学',
sc.cid, sc.sid,count(sc.cid), sum(sc.score) AS '总分'
FROM student
INNER JOIN sc ON student.sid = sc.sid
INNER JOIN course ON sc.cid = course.cid
GROUP BY student.sid, student.sname, student.sage
ORDER BY sum(sc.score) DESC;

1、查询“001”课程比“002”课程成绩高的所有学生的学号; 
SELECT a.sid
FROM
(SELECT sid, score FROM sc WHERE cid = '001') AS a,
(SELECT sid, score FROM sc WHERE cid = '002') AS b
WHERE 
a.score > b.score AND a.sid = b.sid;

2、查询平均成绩大于60分的同学的学号和平均成绩; 
SELECT sid, avg(score)
FROM sc
GROUP BY sid
HAVING avg(score) > 60;

3、查询所有同学的学号、姓名、选课数、总成绩; 
  SELECT 
	student.sid, student.sname, count(sc.cid), sum(score)
FROM student
LEFT OUTER JOIN sc ON student.sid = sc.sid
GROUP BY student.sid, sname;

4、查询姓“李”的老师的个数; 
  SELECT count(DISTINCT(tname)) FROM teacher WHERE tname LIKE '李%';

5、查询没学过“叶平”老师课的同学的学号、姓名; 
SELECT student.sid, student.sname
FROM student
WHERE	sid NOT IN (		
	SELECT DISTINCT(sc.sid)
	FROM sc, course, teacher 
	WHERE sc.cid = course.cid AND teacher.tid = course.tid AND teacher.tname = '叶平');

6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; 
SELECT student.sid, student.sname
FROM student, sc
WHERE	
student.sid = sc.sid AND sc.cid = '001' AND EXISTS (
	SELECT * FROM sc AS SC_2 WHERE SC_2.sid = sc.sid AND SC_2.cid = '002');
7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; 
SELECT sid,	sname
FROM student
WHERE	sid IN (
		SELECT sid FROM sc, course, teacher
		WHERE sc.cid = course.cid AND teacher.tid = course.tid AND teacher.Tname = '叶平'		
		GROUP BY sid
		HAVING count(sc.cid) = (
				SELECT count(cid) FROM course, teacher WHERE teacher.tid = course.tid AND Tname = '叶平'
		)
) ORDER BY sid ASC;
8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名; 
SELECT sid, sname, score, score2
FROM (		
		SELECT student.sid, student.sname, score, 
(SELECT score FROM sc AS SC_2 WHERE SC_2.sid = student.sid AND SC_2.cid = '002') score2, 
(SELECT score FROM sc AS SC_2 WHERE SC_2.sid = student.sid AND SC_2.cid = '003') score3,
(SELECT score FROM sc AS SC_2 WHERE SC_2.sid = student.sid AND SC_2.cid = '004') score4,
(SELECT score FROM sc AS SC_2 WHERE SC_2.sid = student.sid AND SC_2.cid = '005') score5
		FROM student, sc
		WHERE student.sid = sc.sid AND cid = '001' ORDER BY sid ASC
) AS S_2
WHERE score2 < score ORDER BY sid ASC;

9、查询所有课程成绩小于60分的同学的学号、姓名;
SELECT sid, sname FROM student
WHERE 
sid NOT IN (SELECT S.sid FROM student AS S, sc WHERE S.sid = sc.sid AND score > 60);

10、查询没有学全所有课的同学的学号、姓名; 
SELECT student.sid,	student.sname
FROM student,	sc
WHERE	student.sid = sc.sid
GROUP BY student.sid,	student.sname
HAVING count(sc.sid) < (SELECT count(cid) FROM course);

11、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名; 
SELECT DISTINCT student.sid, student.sname
FROM student, sc
WHERE	student.sid = sc.sid AND sc.cid IN (SELECT cid	FROM sc	WHERE sid = '008');

12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名; 
SELECT DISTINCT sc.sid, sname
FROM student, sc
WHERE student.sid = sc.sid AND cid IN (SELECT cid	FROM sc	WHERE sid = '008');

13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩; 
UPDATE sc SET sc.score=
(
	-- 这里开始算叶平的平均值
	SELECT AVG(tt.aa) FROM 
(
SELECT  sc.score aa FROM sc WHERE
sc.cid=
( 
	SELECT course.cid FROM course WHERE course.cid=(
	SELECT teacher.tid FROM teacher WHERE teacher.tname='叶平' )
	))tt -- 算出叶平教得课程的平均值
)
WHERE sc.cid=
(
	SELECT course.cid FROM course WHERE course.cid=(SELECT teacher.tid FROM teacher WHERE teacher.tname='叶平' )
);-- 叶平上课的id

14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名; 
SELECT * FROM student WHERE sid IN
(
SELECT sid
FROM sc
WHERE cid IN (SELECT cid FROM sc WHERE sid = '002')
GROUP BY sid
HAVING count(*) = (SELECT count(*) FROM sc WHERE sid = '002'));

15、删除学习“叶平”老师课的SC表记录; 
    DELETE FROM sc WHERE cid=
(
	SELECT course.cid FROM course WHERE course.cid=(SELECT teacher.tid FROM teacher WHERE teacher.tname='叶平' ));

16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2号课的平均成绩; 
INSERT sc
SELECT sid, '2',
	(SELECT avg(score) FROM sc WHERE cid = '2')
FROM student
WHERE sid NOT IN (SELECT sid FROM sc WHERE cid = '3');

17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程数,有效平均分 
    SELECT S# as 学生ID 
        ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='004') AS 数据库 
        ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='001') AS 企业管理 
        ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='006') AS 英语 
        ,COUNT(*) AS 有效课程数, AVG(t.score) AS 平均成绩 
    FROM SC AS t 
    GROUP BY S# 
    ORDER BY avg(t.score)  

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 
方法1:SELECT L.cid AS 课程ID, L.score AS 最高分, R.score AS 最低分
FROM sc L, sc AS R
WHERE	L.cid = R.cid 
AND L.score = (
	SELECT MAX(IL.score) FROM sc AS IL, student AS IM	WHERE L.cid = IL.cid	AND IM.sid = IL.sid	GROUP BY IL.cid
)
AND R.score = (	
  SELECT MIN(IR.score) FROM sc AS IR WHERE R.cid = IR.cid GROUP BY IR.cid
) GROUP BY L.cid ORDER BY L.cid ASC;
方法2:SELECT cid, max(score) AS 最高分, min(score) AS 最低分 FROM sc GROUP BY cid;
SELECT a.cid, a.big AS 最高分, b.samll AS 最低分 FROM 
(SELECT	cid, MAX(score) AS big FROM sc GROUP BY cid) AS a
INNER JOIN (SELECT	cid, MIN(score) AS samll FROM sc GROUP BY cid) AS b ON a.cid = b.cid;

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序 
SELECT c.cname AS 课程名, a.cid, a.total, a.avgnum, (b.a / 9) * 100 FROM
(SELECT cid,SUM(score) AS total, avg(score) AS avgnum, count(sid) AS num FROM sc GROUP BY cid) AS a
INNER JOIN (select cid,count(score) AS a from sc WHERE score >= 60 group by cid) AS b ON a.cid = b.cid
INNER JOIN course AS c ON c.cid = a.cid
ORDER BY a.avgnum ASC; 

/*按各科平均成绩从低到 */
SELECT cid,SUM(score) AS total, avg(score) AS avgnum, count(sid) AS num
FROM sc GROUP BY cid ORDER BY cid;
/*及格率的百分数*/
select cid,count(score) AS a from sc WHERE score >= 60 group by cid;

20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004) 
SELECT SUM(CASE WHEN cid ='001' THEN score ELSE 0 END)/SUM(CASE cid WHEN '001' THEN 1 ELSE 0 END) AS 企业管理平均分 
,100 * SUM(CASE WHEN cid = '001' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN cid = '001' THEN 1 ELSE 0 END) AS 企业管理及格百分数 
,SUM(CASE WHEN cid = '002' THEN score ELSE 0 END)/SUM(CASE cid WHEN '002' THEN 1 ELSE 0 END) AS 马克思平均分 
,100 * SUM(CASE WHEN cid = '002' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN cid = '002' THEN 1 ELSE 0 END) AS 马克思及格百分数 
,SUM(CASE WHEN cid = '003' THEN score ELSE 0 END)/SUM(CASE cid WHEN '003' THEN 1 ELSE 0 END) AS UML平均分 
,100 * SUM(CASE WHEN cid = '003' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN cid = '003' THEN 1 ELSE 0 END) AS UML及格百分数 
,SUM(CASE WHEN cid = '004' THEN score ELSE 0 END)/SUM(CASE cid WHEN '004' THEN 1 ELSE 0 END) AS 数据库平均分 
,100 * SUM(CASE WHEN cid = '004' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN cid = '004' THEN 1 ELSE 0 END) AS 数据库及格百分数 
FROM sc 

21、查询不同老师所教不同课程平均分从高到低显示 
SELECT max(Z.tid) AS 教师ID, MAX(Z.Tname) AS 教师姓名, C.cid AS 课程ID,MAX(C.cname) AS 课程名称, AVG(score) AS 平均成绩 
FROM sc AS T,course AS C ,teacher AS Z 
WHERE T.cid=C.cid AND C.tid=Z.tid 
GROUP BY C.cid 
ORDER BY AVG(score) DESC;

SELECT cid,SUM(score) AS total, avg(score) AS avgnum, count(sid) AS num
FROM sc GROUP BY cid ORDER BY cid;
22、查询如下课程成绩第 3 名到第 6 名的学生成绩单:企业管理(001),马克思(002),UML (003),数据库(004) 
   SELECT * FROM 
(SELECT student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', 
course.cid AS '课程号', 
SUM(CASE course.cname WHEN '语文' THEN sc.score ELSE 0 END) AS '语文',
SUM(CASE course.cname WHEN '数学' THEN sc.score ELSE 0 END) AS '数学',
SUM(CASE course.cname WHEN '英语' THEN sc.score ELSE 0 END) AS '英语',
SUM(CASE course.cname WHEN '物理' THEN sc.score ELSE 0 END) AS '物理',
SUM(CASE course.cname WHEN '化学' THEN sc.score ELSE 0 END) AS '化学',
sc.cid, sc.sid,count(sc.cid), sum(sc.score) AS '总分'
FROM student
INNER JOIN sc ON student.sid = sc.sid
INNER JOIN course ON sc.cid = course.cid
GROUP BY student.sid, student.sname, student.sage
ORDER BY sum(sc.score) DESC) AS b
LIMIT 3,3;

23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] 
SELECT sc.cid AS 课程ID, course.cname AS 课程名称 
,SUM(CASE WHEN sc.score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS '100 - 85' 
,SUM(CASE WHEN sc.score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS '85 - 70' 
,SUM(CASE WHEN sc.score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS '70 - 60' 
,SUM(CASE WHEN sc.score < 60 THEN 1 ELSE 0 END) AS '60 -' 
FROM sc, course 
where sc.cid = course.cid 
GROUP BY sc.cid, course.cname; 

24、查询学生平均成绩及其名次 
SELECT 1+(SELECT COUNT( distinct 平均成绩) FROM (SELECT sid, AVG(score) AS 平均成绩 FROM sc GROUP BY sid) AS T1 WHERE 平均成绩 > T2.平均成绩) as 名次, sid as 学生学号, 平均成绩 
FROM (SELECT sid, AVG(score) 平均成绩 FROM sc GROUP BY sid) as T2 
ORDER BY 平均成绩 desc; 

/*不管数据相同与否,排名依次排序(1,2,3,4,5,6,7.....)*/
SELECT a.sid, a.number, @rownum := @rownum + 1 AS rownum
FROM
(SELECT sid, AVG(score) AS number FROM sc GROUP BY sid ORDER BY number DESC) AS a,
(SELECT @rownum := 0) r;

/*只要数据有相同的排名就一样,排名依次排序(1,2,2,3,3,4,5.....)*/
SELECT a.sid, a.number, 
CASE 
WHEN @rowtotal = a.number THEN @rownum
WHEN @rowtotal := a.number THEN @rownum := @rownum + 1
WHEN @rowtotal := 0 THEN @rownum := @rownum + 1 END AS rownum
FROM
(SELECT sid, AVG(score) AS number FROM sc GROUP BY sid ORDER BY number DESC) AS a,
(SELECT @rownum := 0, @rowtotal := NULL) r

/*只要数据有相同的排名就一样,但是相同排名也占位,排名依次排序(1,2,2,4,5,5,7.....)*/
SELECT b.sid, b.number, b.rownum, b.num_tmp
FROM
(
	SELECT a.sid, a.number, 
	@rownum := @rownum + 1 AS num_tmp,
	@incrnum := CASE
	WHEN @rowtotal = a.number THEN @incrnum
	WHEN @rowtotal := a.number THEN @rownum END AS rownum
	FROM
	(SELECT sid, AVG(score) AS number FROM sc GROUP BY sid ORDER BY number DESC) AS a,
	(SELECT @rownum := 0, @rowtotal := NULL, @incrnum := 0) r
) AS b
  
25、查询各科成绩前三名的记录:(不考虑成绩并列情况) 
SELECT * FROM 
(SELECT student.sid, student.sname, student.sage, student.ssex, sc.cid, sc.score, course.cname
 FROM sc
 INNER JOIN student ON sc.sid = student.sid
 INNER JOIN course ON course.cid = sc.cid) AS t
WHERE
	EXISTS (
		SELECT count(1) FROM (SELECT student.sid, student.sname, student.sage, student.ssex, sc.cid, sc.score, course.cname
 FROM sc
 INNER JOIN student ON sc.sid = student.sid
 INNER JOIN course ON course.cid = sc.cid) AS d WHERE t.cid = d.cid AND t.score < d.score HAVING count(1) < 3
	)
ORDER BY t.cid ASC, t.score DESC;

SELECT t.sid AS 学生ID, t.cid AS 课程ID, t.score AS 分数 
FROM sc t
WHERE 	
EXISTS (
		SELECT count(1) FROM (SELECT student.sid, student.sname, student.sage, student.ssex, sc.cid, sc.score, course.cname
 FROM sc
 INNER JOIN student ON sc.sid = student.sid
 INNER JOIN course ON course.cid = sc.cid) AS d WHERE t.cid = d.cid AND t.score < d.score HAVING count(1) < 3
)
ORDER BY t.cid ASC, t.score DESC;

26、查询每门课程被选修的学生数 
SELECT cid, count(sid) FROM sc GROUP BY cid;

27、查询出只选修了一门课程的全部学生的学号和姓名 
SELECT sc.sid, student.sname, count(cid) AS 选课数
FROM sc, student
WHERE sc.sid = student.sid
GROUP BY sc.sid, student.sname
HAVING count(cid) = 1;

SELECT a.*, b.cid, b.num FROM
student AS a
INNER JOIN (SELECT sid, cid, count(cid) AS num FROM sc GROUP BY sid) AS b ON a.sid = b.sid AND b.num = 1
28、查询男生、女生人数 
SELECT count(ssex) AS 男生人数 FROM student GROUP BY ssex HAVING ssex='男'; 
SELECT count(ssex) AS 女生人数 FROM student GROUP BY ssex HAVING ssex='女';

29、查询姓“张”的学生名单 
SELECT sname FROM student WHERE sname like '张%'; 

30、查询同名同性学生名单,并统计同名人数 
SELECT sname, count(*) FROM student GROUP BY sname HAVING count(*) > 1;

-------------------------------------------------------------------------------------------------------16:32 2015/1/6
31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime) 
select sname, sage from student where left(sage, 4)=1981

32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列 
SELECT cid, Avg(score) FROM sc GROUP BY cid ORDER BY Avg(score), cid DESC;

33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩 
SELECT sname, sc.sid, avg(score) FROM student, sc WHERE student.sid = sc.sid GROUP BY sc.sid, sname HAVING avg(score) > 85;

34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数 
SELECT sname, score
FROM student, sc, course
WHERE sc.sid = student.sid AND sc.cid = course.cid AND course.cname = '语文' AND score < 60;

35、查询所有学生的选课情况; 
SELECT sc.sid, sc.cid, sc.score, student.sname, course.cname 
FROM sc, student, course 
WHERE sc.sid = student.sid AND sc.cid = course.cid
ORDER BY sc.sid ASC;

SELECT student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', 
course.cid AS '课程号', 
SUM(CASE course.cname WHEN '语文' THEN sc.score ELSE 0 END) AS '语文',
SUM(CASE course.cname WHEN '数学' THEN sc.score ELSE 0 END) AS '数学',
SUM(CASE course.cname WHEN '英语' THEN sc.score ELSE 0 END) AS '英语',
SUM(CASE course.cname WHEN '物理' THEN sc.score ELSE 0 END) AS '物理',
SUM(CASE course.cname WHEN '化学' THEN sc.score ELSE 0 END) AS '化学',
sc.cid, sc.sid, count(sc.cid) AS '选课数量', sum(sc.score) AS '总分'
FROM student
INNER JOIN sc ON student.sid = sc.sid
INNER JOIN course ON sc.cid = course.cid
GROUP BY student.sid, student.sname, student.sage
ORDER BY sum(sc.score) DESC

36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; 
SELECT sc.sid, sc.cid, sc.score, student.sname, course.cname 
FROM sc, student, course 
WHERE sc.sid = student.sid AND sc.cid = course.cid AND sc.score >= 70
ORDER BY sc.sid ASC;

SELECT student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', 
SUM(CASE course.cname WHEN '语文' THEN sc.score ELSE 0 END) AS '语文',
SUM(CASE course.cname WHEN '数学' THEN sc.score ELSE 0 END) AS '数学',
SUM(CASE course.cname WHEN '英语' THEN sc.score ELSE 0 END) AS '英语',
SUM(CASE course.cname WHEN '物理' THEN sc.score ELSE 0 END) AS '物理',
SUM(CASE course.cname WHEN '化学' THEN sc.score ELSE 0 END) AS '化学'
FROM student
INNER JOIN sc ON student.sid = sc.sid
INNER JOIN course ON sc.cid = course.cid
WHERE sc.score >= 70
GROUP BY student.sid, student.sname, student.sage
ORDER BY sum(sc.score) DESC

37、查询不及格的课程,并按课程号从大到小排列 -
    SELECT * FROM sc WHERE score < 60 ORDER BY cid DESC;

38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名; 
    SELECT student.*, sc.cid, sc.score FROM sc, student WHERE sc.sid = student.sid AND score > 80 and cid = '3';

39、求选了课程的学生人数 
SELECT COUNT(DISTINCT sid) AS number FROM sc; 
选课的学生和课程数
SELECT sid, COUNT(cid) AS number FROM sc GROUP BY sid HAVING (COUNT(cid) > 0);

40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩 
SELECT student.sname, score 
FROM student, sc, course AS c, teacher 
WHERE student.sid = sc.sid AND sc.cid = c.cid AND c.tid = teacher.tid AND teacher.tname = '叶平' AND sc.score = (SELECT max(score) FROM sc WHERE cid = c.cid); 
41、查询各个课程及相应的选修人数 
SELECT cid, COUNT(sid) AS number FROM sc GROUP BY cid;
42、查询不同课程成绩相同的学生的学号、课程号、学生成绩 
SELECT DISTINCT A.sid, B.score FROM sc A, sc B WHERE A.score = B.score AND A.cid <> B.cid ORDER BY sid ASC; 

43、查询每门功成绩最好的前两名 
如果考虑例如有两个同分数的第二名可以用下面的语句:
SELECT a.sid, a.cid, a.score 
FROM sc AS a 
WHERE (SELECT COUNT(DISTINCT score) FROM sc AS b WHERE b.cid = a.cid AND b.score >= a.score) <= 2 
ORDER BY a.cid ASC, a.score DESC;

如果有同分数的两个第二名则删除第二名:
SELECT a.sid, a.cid, a.score 
FROM sc AS a      
WHERE (SELECT count(1) FROM sc b WHERE b.cid=a.cid AND b.score >= a.score) <=2 ORDER BY a.cid ASC;

SELECT a.sid, a.cid, a.score 
FROM sc AS a 
WHERE (SELECT COUNT(*) FROM sc AS b WHERE b.cid = a.cid AND b.score >= a.score) <= 2 
ORDER BY a.cid ASC, a.score DESC;

44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列  
SELECT sc.cid, count(sc.sid) AS num FROM sc GROUP BY cid ORDER BY num DESC;

45、检索至少选修两门课程的学生学号 
select sid from sc group by sid having count(sid) >= 2;

46、查询全部学生都选修的课程的课程号和课程名 
SELECT cid, cname FROM course
WHERE cid = (SELECT cid FROM sc GROUP BY cid HAVING count(cid) >= (SELECT COUNT(*) AS b FROM student));

47、查询没学过“叶平”老师讲授的任一门课程的学生姓名 
SELECT * FROM student WHERE sid NOT IN (SELECT sid FROM course, teacher, sc WHERE course.tid = teacher.tid AND sc.cid = course.cid AND tname = '叶平');

48、查询两门以上不及格课程的同学的学号及其平均成绩 
 SELECT sid, avg(score) FROM	sc WHERE sid IN (SELECT sid FROM sc WHERE score < 60 GROUP BY sid HAVING count(sid) >= 2) GROUP BY sid;

49、检索“004”课程分数小于60,按分数降序排列的同学学号 
SELECT sid FROM sc WHERE cid = '4' AND score < 60 ORDER BY score DESC;

50、删除“002”同学的“001”课程的成绩 
DELETE FROM sc WHERE sid = '2' AND cid = '1';

  有些事情不需要经历也能明白,成功没有捷径,希望大家多多练习,知识学到是自己,它会伴随你的一生,好运!加油!

 

查询所有学生姓名、性别、科目、成绩、总成绩排名信息/*获取所有学生的信息*/select student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', course.cid AS '课程号', course.cname AS '学科', sc.score AS '分数',sc.cid, sc.sid,count(sc.cid), sum(sc.score)from course, sc, studentGROUP BY student.sid, student.sname, student.sage, sc.cid, sc.sid, course.cid, course.cname, sc.scoreHAVING student.sid = sc.sid AND sc.cid = course.cid;
/*成绩是打印出来了,重复的数据太多了*/SELECT student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', course.cid AS '课程号', (CASE course.cname WHEN '语文' THEN sc.score ELSE 0 END) AS '语文',(CASE course.cname WHEN '数学' THEN sc.score ELSE 0 END) AS '数学',(CASE course.cname WHEN '英语' THEN sc.score ELSE 0 END) AS '英语',(CASE course.cname WHEN '物理' THEN sc.score ELSE 0 END) AS '物理',(CASE course.cname WHEN '化学' THEN sc.score ELSE 0 END) AS '化学',sc.cid, sc.sid,count(sc.cid), sum(sc.score)FROM course, sc, studentGROUP BY student.sid, student.sname, student.sage, sc.cid, sc.sid, course.cid, course.cname, sc.scoreHAVING student.sid = sc.sid AND sc.cid = course.cidORDER BY student.sid ASC, sc.cid ASC;
/*用表的联结join*/SELECT student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', course.cid AS '课程号', SUM(CASE course.cname WHEN '语文' THEN sc.score ELSE 0 END) AS '语文',SUM(CASE course.cname WHEN '数学' THEN sc.score ELSE 0 END) AS '数学',SUM(CASE course.cname WHEN '英语' THEN sc.score ELSE 0 END) AS '英语',SUM(CASE course.cname WHEN '物理' THEN sc.score ELSE 0 END) AS '物理',SUM(CASE course.cname WHEN '化学' THEN sc.score ELSE 0 END) AS '化学',sc.cid, sc.sid,count(sc.cid), sum(sc.score) AS '总分'FROM studentLEFT OUTER JOIN sc ON student.sid = sc.sidLEFT OUTER JOIN course ON sc.cid = course.cidGROUP BY student.sid, student.sname, student.sageORDER BY student.sid;
/*成绩排序*/SELECT student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', course.cid AS '课程号', SUM(CASE course.cname WHEN '语文' THEN sc.score ELSE 0 END) AS '语文',SUM(CASE course.cname WHEN '数学' THEN sc.score ELSE 0 END) AS '数学',SUM(CASE course.cname WHEN '英语' THEN sc.score ELSE 0 END) AS '英语',SUM(CASE course.cname WHEN '物理' THEN sc.score ELSE 0 END) AS '物理',SUM(CASE course.cname WHEN '化学' THEN sc.score ELSE 0 END) AS '化学',sc.cid, sc.sid,count(sc.cid), sum(sc.score) AS '总分'FROM studentINNER JOIN sc ON student.sid = sc.sidINNER JOIN course ON sc.cid = course.cidGROUP BY student.sid, student.sname, student.sageORDER BY sum(sc.score) DESC;
1、查询“001”课程比“002”课程成绩高的所有学生的学号; SELECT a.sidFROM(SELECT sid, score FROM sc WHERE cid = '001') AS a,(SELECT sid, score FROM sc WHERE cid = '002') AS bWHERE a.score > b.score AND a.sid = b.sid;
2、查询平均成绩大于60分的同学的学号和平均成绩; SELECT sid, avg(score)FROM scGROUP BY sidHAVING avg(score) > 60;
3、查询所有同学的学号、姓名、选课数、总成绩;   SELECT student.sid, student.sname, count(sc.cid), sum(score)FROM studentLEFT OUTER JOIN sc ON student.sid = sc.sidGROUP BY student.sid, sname;
4、查询姓“李”的老师的个数;   SELECT count(DISTINCT(tname)) FROM teacher WHERE tname LIKE '李%';
5、查询没学过“叶平”老师课的同学的学号、姓名; SELECT student.sid, student.snameFROM studentWHEREsid NOT IN (SELECT DISTINCT(sc.sid)FROM sc, course, teacher WHERE sc.cid = course.cid AND teacher.tid = course.tid AND teacher.tname = '叶平');
6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; SELECT student.sid, student.snameFROM student, scWHEREstudent.sid = sc.sid AND sc.cid = '001' AND EXISTS (SELECT * FROM sc AS SC_2 WHERE SC_2.sid = sc.sid AND SC_2.cid = '002');7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; SELECT sid,snameFROM studentWHEREsid IN (SELECT sid FROM sc, course, teacherWHERE sc.cid = course.cid AND teacher.tid = course.tid AND teacher.Tname = '叶平'GROUP BY sidHAVING count(sc.cid) = (SELECT count(cid) FROM course, teacher WHERE teacher.tid = course.tid AND Tname = '叶平')) ORDER BY sid ASC;8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名; SELECT sid, sname, score, score2FROM (SELECT student.sid, student.sname, score, (SELECT score FROM sc AS SC_2 WHERE SC_2.sid = student.sid AND SC_2.cid = '002') score2, (SELECT score FROM sc AS SC_2 WHERE SC_2.sid = student.sid AND SC_2.cid = '003') score3,(SELECT score FROM sc AS SC_2 WHERE SC_2.sid = student.sid AND SC_2.cid = '004') score4,(SELECT score FROM sc AS SC_2 WHERE SC_2.sid = student.sid AND SC_2.cid = '005') score5FROM student, scWHERE student.sid = sc.sid AND cid = '001' ORDER BY sid ASC) AS S_2WHERE score2 < score ORDER BY sid ASC;
9、查询所有课程成绩小于60分的同学的学号、姓名;SELECT sid, sname FROM studentWHERE sid NOT IN (SELECT S.sid FROM student AS S, sc WHERE S.sid = sc.sid AND score > 60);
10、查询没有学全所有课的同学的学号、姓名; SELECT student.sid,student.snameFROM student,scWHEREstudent.sid = sc.sidGROUP BY student.sid,student.snameHAVING count(sc.sid) < (SELECT count(cid) FROM course);
11、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名; SELECT DISTINCT student.sid, student.snameFROM student, scWHEREstudent.sid = sc.sid AND sc.cid IN (SELECT cidFROM scWHERE sid = '008');
12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名; SELECT DISTINCT sc.sid, snameFROM student, scWHERE student.sid = sc.sid AND cid IN (SELECT cidFROM scWHERE sid = '008');
13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩; UPDATE sc SET sc.score=(-- 这里开始算叶平的平均值SELECT AVG(tt.aa) FROM (SELECT  sc.score aa FROM sc WHEREsc.cid=( SELECT course.cid FROM course WHERE course.cid=(SELECT teacher.tid FROM teacher WHERE teacher.tname='叶平' )))tt -- 算出叶平教得课程的平均值)WHERE sc.cid=(SELECT course.cid FROM course WHERE course.cid=(SELECT teacher.tid FROM teacher WHERE teacher.tname='叶平' ));-- 叶平上课的id
14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名; SELECT * FROM student WHERE sid IN(SELECT sidFROM scWHERE cid IN (SELECT cid FROM sc WHERE sid = '002')GROUP BY sidHAVING count(*) = (SELECT count(*) FROM sc WHERE sid = '002'));
15、删除学习“叶平”老师课的SC表记录;     DELETE FROM sc WHERE cid=(SELECT course.cid FROM course WHERE course.cid=(SELECT teacher.tid FROM teacher WHERE teacher.tname='叶平' ));
16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2号课的平均成绩; INSERT scSELECT sid, '2',(SELECT avg(score) FROM sc WHERE cid = '2')FROM studentWHERE sid NOT IN (SELECT sid FROM sc WHERE cid = '3');
17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程数,有效平均分     SELECT S# as 学生ID         ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='004') AS 数据库         ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='001') AS 企业管理         ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='006') AS 英语         ,COUNT(*) AS 有效课程数, AVG(t.score) AS 平均成绩     FROM SC AS t     GROUP BY S#     ORDER BY avg(t.score)  
18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 方法1:SELECT L.cid AS 课程ID, L.score AS 最高分, R.score AS 最低分FROM sc L, sc AS RWHEREL.cid = R.cid AND L.score = (SELECT MAX(IL.score) FROM sc AS IL, student AS IMWHERE L.cid = IL.cidAND IM.sid = IL.sidGROUP BY IL.cid)AND R.score = (  SELECT MIN(IR.score) FROM sc AS IR WHERE R.cid = IR.cid GROUP BY IR.cid) GROUP BY L.cid ORDER BY L.cid ASC;方法2:SELECT cid, max(score) AS 最高分, min(score) AS 最低分 FROM sc GROUP BY cid;SELECT a.cid, a.big AS 最高分, b.samll AS 最低分 FROM (SELECTcid, MAX(score) AS big FROM sc GROUP BY cid) AS aINNER JOIN (SELECTcid, MIN(score) AS samll FROM sc GROUP BY cid) AS b ON a.cid = b.cid;
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序 SELECT c.cname AS 课程名, a.cid, a.total, a.avgnum, (b.a / 9) * 100 FROM(SELECT cid,SUM(score) AS total, avg(score) AS avgnum, count(sid) AS num FROM sc GROUP BY cid) AS aINNER JOIN (select cid,count(score) AS a from sc WHERE score >= 60 group by cid) AS b ON a.cid = b.cidINNER JOIN course AS c ON c.cid = a.cidORDER BY a.avgnum ASC; 
/*按各科平均成绩从低到 */SELECT cid,SUM(score) AS total, avg(score) AS avgnum, count(sid) AS numFROM sc GROUP BY cid ORDER BY cid;/*及格率的百分数*/select cid,count(score) AS a from sc WHERE score >= 60 group by cid;
20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004) SELECT SUM(CASE WHEN cid ='001' THEN score ELSE 0 END)/SUM(CASE cid WHEN '001' THEN 1 ELSE 0 END) AS 企业管理平均分 ,100 * SUM(CASE WHEN cid = '001' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN cid = '001' THEN 1 ELSE 0 END) AS 企业管理及格百分数 ,SUM(CASE WHEN cid = '002' THEN score ELSE 0 END)/SUM(CASE cid WHEN '002' THEN 1 ELSE 0 END) AS 马克思平均分 ,100 * SUM(CASE WHEN cid = '002' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN cid = '002' THEN 1 ELSE 0 END) AS 马克思及格百分数 ,SUM(CASE WHEN cid = '003' THEN score ELSE 0 END)/SUM(CASE cid WHEN '003' THEN 1 ELSE 0 END) AS UML平均分 ,100 * SUM(CASE WHEN cid = '003' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN cid = '003' THEN 1 ELSE 0 END) AS UML及格百分数 ,SUM(CASE WHEN cid = '004' THEN score ELSE 0 END)/SUM(CASE cid WHEN '004' THEN 1 ELSE 0 END) AS 数据库平均分 ,100 * SUM(CASE WHEN cid = '004' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN cid = '004' THEN 1 ELSE 0 END) AS 数据库及格百分数 FROM sc 
21、查询不同老师所教不同课程平均分从高到低显示 SELECT max(Z.tid) AS 教师ID, MAX(Z.Tname) AS 教师姓名, C.cid AS 课程ID,MAX(C.cname) AS 课程名称, AVG(score) AS 平均成绩 FROM sc AS T,course AS C ,teacher AS Z WHERE T.cid=C.cid AND C.tid=Z.tid GROUP BY C.cid ORDER BY AVG(score) DESC;
SELECT cid,SUM(score) AS total, avg(score) AS avgnum, count(sid) AS numFROM sc GROUP BY cid ORDER BY cid;22、查询如下课程成绩第 3 名到第 6 名的学生成绩单:企业管理(001),马克思(002),UML (003),数据库(004)    SELECT * FROM (SELECT student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', course.cid AS '课程号', SUM(CASE course.cname WHEN '语文' THEN sc.score ELSE 0 END) AS '语文',SUM(CASE course.cname WHEN '数学' THEN sc.score ELSE 0 END) AS '数学',SUM(CASE course.cname WHEN '英语' THEN sc.score ELSE 0 END) AS '英语',SUM(CASE course.cname WHEN '物理' THEN sc.score ELSE 0 END) AS '物理',SUM(CASE course.cname WHEN '化学' THEN sc.score ELSE 0 END) AS '化学',sc.cid, sc.sid,count(sc.cid), sum(sc.score) AS '总分'FROM studentINNER JOIN sc ON student.sid = sc.sidINNER JOIN course ON sc.cid = course.cidGROUP BY student.sid, student.sname, student.sageORDER BY sum(sc.score) DESC) AS bLIMIT 3,3;
23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] SELECT sc.cid AS 课程ID, course.cname AS 课程名称 ,SUM(CASE WHEN sc.score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS '100 - 85' ,SUM(CASE WHEN sc.score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS '85 - 70' ,SUM(CASE WHEN sc.score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS '70 - 60' ,SUM(CASE WHEN sc.score < 60 THEN 1 ELSE 0 END) AS '60 -' FROM sc, course where sc.cid = course.cid GROUP BY sc.cid, course.cname; 
24、查询学生平均成绩及其名次 SELECT 1+(SELECT COUNT( distinct 平均成绩) FROM (SELECT sid, AVG(score) AS 平均成绩 FROM sc GROUP BY sid) AS T1 WHERE 平均成绩 > T2.平均成绩) as 名次, sid as 学生学号, 平均成绩 FROM (SELECT sid, AVG(score) 平均成绩 FROM sc GROUP BY sid) as T2 ORDER BY 平均成绩 desc; 
/*不管数据相同与否,排名依次排序(1,2,3,4,5,6,7.....)*/SELECT a.sid, a.number, @rownum := @rownum + 1 AS rownumFROM(SELECT sid, AVG(score) AS number FROM sc GROUP BY sid ORDER BY number DESC) AS a,(SELECT @rownum := 0) r;
/*只要数据有相同的排名就一样,排名依次排序(1,2,2,3,3,4,5.....)*/SELECT a.sid, a.number, CASE WHEN @rowtotal = a.number THEN @rownumWHEN @rowtotal := a.number THEN @rownum := @rownum + 1WHEN @rowtotal := 0 THEN @rownum := @rownum + 1 END AS rownumFROM(SELECT sid, AVG(score) AS number FROM sc GROUP BY sid ORDER BY number DESC) AS a,(SELECT @rownum := 0, @rowtotal := NULL) r
/*只要数据有相同的排名就一样,但是相同排名也占位,排名依次排序(1,2,2,4,5,5,7.....)*/SELECT b.sid, b.number, b.rownum, b.num_tmpFROM(SELECT a.sid, a.number, @rownum := @rownum + 1 AS num_tmp,@incrnum := CASEWHEN @rowtotal = a.number THEN @incrnumWHEN @rowtotal := a.number THEN @rownum END AS rownumFROM(SELECT sid, AVG(score) AS number FROM sc GROUP BY sid ORDER BY number DESC) AS a,(SELECT @rownum := 0, @rowtotal := NULL, @incrnum := 0) r) AS b  25、查询各科成绩前三名的记录:(不考虑成绩并列情况) SELECT * FROM (SELECT student.sid, student.sname, student.sage, student.ssex, sc.cid, sc.score, course.cname FROM sc INNER JOIN student ON sc.sid = student.sid INNER JOIN course ON course.cid = sc.cid) AS tWHEREEXISTS (SELECT count(1) FROM (SELECT student.sid, student.sname, student.sage, student.ssex, sc.cid, sc.score, course.cname FROM sc INNER JOIN student ON sc.sid = student.sid INNER JOIN course ON course.cid = sc.cid) AS d WHERE t.cid = d.cid AND t.score < d.score HAVING count(1) < 3)ORDER BY t.cid ASC, t.score DESC;
SELECT t.sid AS 学生ID, t.cid AS 课程ID, t.score AS 分数 FROM sc tWHERE EXISTS (SELECT count(1) FROM (SELECT student.sid, student.sname, student.sage, student.ssex, sc.cid, sc.score, course.cname FROM sc INNER JOIN student ON sc.sid = student.sid INNER JOIN course ON course.cid = sc.cid) AS d WHERE t.cid = d.cid AND t.score < d.score HAVING count(1) < 3)ORDER BY t.cid ASC, t.score DESC;
26、查询每门课程被选修的学生数 SELECT cid, count(sid) FROM sc GROUP BY cid;
27、查询出只选修了一门课程的全部学生的学号和姓名 SELECT sc.sid, student.sname, count(cid) AS 选课数FROM sc, studentWHERE sc.sid = student.sidGROUP BY sc.sid, student.snameHAVING count(cid) = 1;
SELECT a.*, b.cid, b.num FROMstudent AS aINNER JOIN (SELECT sid, cid, count(cid) AS num FROM sc GROUP BY sid) AS b ON a.sid = b.sid AND b.num = 128、查询男生、女生人数 SELECT count(ssex) AS 男生人数 FROM student GROUP BY ssex HAVING ssex='男'; SELECT count(ssex) AS 女生人数 FROM student GROUP BY ssex HAVING ssex='女';
29、查询姓“张”的学生名单 SELECT sname FROM student WHERE sname like '张%'; 
30、查询同名同性学生名单,并统计同名人数 SELECT sname, count(*) FROM student GROUP BY sname HAVING count(*) > 1;
-------------------------------------------------------------------------------------------------------16:32 2015/1/631、1981年出生的学生名单(注:Student表中Sage列的类型是datetime) select sname, sage from student where left(sage, 4)=1981
32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列 SELECT cid, Avg(score) FROM sc GROUP BY cid ORDER BY Avg(score), cid DESC;
33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩 SELECT sname, sc.sid, avg(score) FROM student, sc WHERE student.sid = sc.sid GROUP BY sc.sid, sname HAVING avg(score) > 85;
34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数 SELECT sname, scoreFROM student, sc, courseWHERE sc.sid = student.sid AND sc.cid = course.cid AND course.cname = '语文' AND score < 60;
35、查询所有学生的选课情况; SELECT sc.sid, sc.cid, sc.score, student.sname, course.cname FROM sc, student, course WHERE sc.sid = student.sid AND sc.cid = course.cidORDER BY sc.sid ASC;
SELECT student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', course.cid AS '课程号', SUM(CASE course.cname WHEN '语文' THEN sc.score ELSE 0 END) AS '语文',SUM(CASE course.cname WHEN '数学' THEN sc.score ELSE 0 END) AS '数学',SUM(CASE course.cname WHEN '英语' THEN sc.score ELSE 0 END) AS '英语',SUM(CASE course.cname WHEN '物理' THEN sc.score ELSE 0 END) AS '物理',SUM(CASE course.cname WHEN '化学' THEN sc.score ELSE 0 END) AS '化学',sc.cid, sc.sid, count(sc.cid) AS '选课数量', sum(sc.score) AS '总分'FROM studentINNER JOIN sc ON student.sid = sc.sidINNER JOIN course ON sc.cid = course.cidGROUP BY student.sid, student.sname, student.sageORDER BY sum(sc.score) DESC
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; SELECT sc.sid, sc.cid, sc.score, student.sname, course.cname FROM sc, student, course WHERE sc.sid = student.sid AND sc.cid = course.cid AND sc.score >= 70ORDER BY sc.sid ASC;
SELECT student.sid AS '学号',student.sname AS '姓名', student.sage AS '年龄', SUM(CASE course.cname WHEN '语文' THEN sc.score ELSE 0 END) AS '语文',SUM(CASE course.cname WHEN '数学' THEN sc.score ELSE 0 END) AS '数学',SUM(CASE course.cname WHEN '英语' THEN sc.score ELSE 0 END) AS '英语',SUM(CASE course.cname WHEN '物理' THEN sc.score ELSE 0 END) AS '物理',SUM(CASE course.cname WHEN '化学' THEN sc.score ELSE 0 END) AS '化学'FROM studentINNER JOIN sc ON student.sid = sc.sidINNER JOIN course ON sc.cid = course.cidWHERE sc.score >= 70GROUP BY student.sid, student.sname, student.sageORDER BY sum(sc.score) DESC
37、查询不及格的课程,并按课程号从大到小排列 -    SELECT * FROM sc WHERE score < 60 ORDER BY cid DESC;
38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;     SELECT student.*, sc.cid, sc.score FROM sc, student WHERE sc.sid = student.sid AND score > 80 and cid = '3';
39、求选了课程的学生人数 SELECT COUNT(DISTINCT sid) AS number FROM sc; 选课的学生和课程数SELECT sid, COUNT(cid) AS number FROM sc GROUP BY sid HAVING (COUNT(cid) > 0);
40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩 SELECT student.sname, score FROM student, sc, course AS c, teacher WHERE student.sid = sc.sid AND sc.cid = c.cid AND c.tid = teacher.tid AND teacher.tname = '叶平' AND sc.score = (SELECT max(score) FROM sc WHERE cid = c.cid); 41、查询各个课程及相应的选修人数 SELECT cid, COUNT(sid) AS number FROM sc GROUP BY cid;42、查询不同课程成绩相同的学生的学号、课程号、学生成绩 SELECT DISTINCT A.sid, B.score FROM sc A, sc B WHERE A.score = B.score AND A.cid <> B.cid ORDER BY sid ASC; 
43、查询每门功成绩最好的前两名 如果考虑例如有两个同分数的第二名可以用下面的语句:SELECT a.sid, a.cid, a.score FROM sc AS a WHERE (SELECT COUNT(DISTINCT score) FROM sc AS b WHERE b.cid = a.cid AND b.score >= a.score) <= 2 ORDER BY a.cid ASC, a.score DESC;
如果有同分数的两个第二名则删除第二名:SELECT a.sid, a.cid, a.score FROM sc AS a      WHERE (SELECT count(1) FROM sc b WHERE b.cid=a.cid AND b.score >= a.score) <=2 ORDER BY a.cid ASC;
SELECT a.sid, a.cid, a.score FROM sc AS a WHERE (SELECT COUNT(*) FROM sc AS b WHERE b.cid = a.cid AND b.score >= a.score) <= 2 ORDER BY a.cid ASC, a.score DESC;
44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列  SELECT sc.cid, count(sc.sid) AS num FROM sc GROUP BY cid ORDER BY num DESC;
45、检索至少选修两门课程的学生学号 select sid from sc group by sid having count(sid) >= 2;
46、查询全部学生都选修的课程的课程号和课程名 SELECT cid, cname FROM courseWHERE cid = (SELECT cid FROM sc GROUP BY cid HAVING count(cid) >= (SELECT COUNT(*) AS b FROM student));
47、查询没学过“叶平”老师讲授的任一门课程的学生姓名 SELECT * FROM student WHERE sid NOT IN (SELECT sid FROM course, teacher, sc WHERE course.tid = teacher.tid AND sc.cid = course.cid AND tname = '叶平');
48、查询两门以上不及格课程的同学的学号及其平均成绩  SELECT sid, avg(score) FROMsc WHERE sid IN (SELECT sid FROM sc WHERE score < 60 GROUP BY sid HAVING count(sid) >= 2) GROUP BY sid;
49、检索“004”课程分数小于60,按分数降序排列的同学学号 SELECT sid FROM sc WHERE cid = '4' AND score < 60 ORDER BY score DESC;
50、删除“002”同学的“001”课程的成绩 DELETE FROM sc WHERE sid = '2' AND cid = '1';

$flag 上一页 下一页