查询课程1比课程2成绩高的学生

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查询课程1比课程2成绩高的学生相关的知识,希望对你有一定的参考价值。

参考技术A

1. 查询“语文”课程比“数学”课程成绩高的所有学生的学号 查询如下课程成绩第 3 名到第 6 名的学生

select a.sid from sc as a,sc as b
where a.sid=b.sid and a.cid=\'语文 and b.cid=\'数学\' and a.score > b.score

2. 查询出其2号课程成绩比所有1号课程成绩都低那些学生的学号. 方法一(用ALL): 方法二(用Min):

SELECT Sno FROM SC WHERE Cno=\'1\' INTERSECT SELECT Sno FROM SC WHERE Cno=\'2\' *** 运算,列的数目、类型要一致如果还报错,那就考虑别的语法吧,not in或者not exists 列值不一样的记录,在 *** 想减时减不掉,所以要把cno和grade列去掉 就这么多了,希望能帮到你
更多建站问题,可搜一下“飘仙论坛”

3. 怎么查询“001”课程比“002”课程成绩高的所有学生的学号表结构如下:

select a.sid, a.score as score1,b.score as score2 from (select * from scoretable where cid=\'001\') a
left join
(select * from scoretable where cid=\'002\')b
on a.sid=b.sid
where a.score>b.score

4. 在mysql中怎么查 课程1总分比课程2总分多多少 结果能显示为一个数据吗, 学生表 s(

你这里面只有学生表,课程表和班级表没有分数的表啊

5. 查询出其2号课程成绩比所有1号课程成绩都低那些学生的学号. 方法一(用ALL)

select 学号 from 选课表 where 成绩< all(select 成绩 from 选课表 where 课程号=1) and 课程号=2;

6. 查询所有考试01号课程分数比所有02号课程分数高的学生学号,姓名

select 学号,姓名 from 学生表 where((select 成绩 from 成绩表 where 课程编号=\'01号\')>(select 成绩 from 成绩表 where 课程编号=\'02号\')
我也是初学的,应该这这样的吧.

7. oracle 怎么查询科目一比科目二成绩高的学生ID

select stu_id,avg(成绩) from 表 group by stu_id;

现在学生都喜欢网络知道呀,前两天遇见问和你一模一样的题的,除了提问的主题不一样,内容全是一样,呵呵!

8. 数据库查询 查询“001”课程比“002”课程成绩高的所有学生的学号

分析如下:

--1selectSC1.S#fromSCSC1JOINSCSC2ONSC1.S#=SC2.S#

WHERESC1.C#=\'001\'ANDSC2.C#=\'002\'ANDSC1.score>SC2.score

--2selectS#,AVG(score)

平均成绩fromSCgroupbyS#

havingAVG(score)>60--3selectStudent.S#,

Sname,COUNT(*)选课数,SUM(score)总成绩

fromStudentJOINSConStudent.S#=SC.S#

groupbyStudent.S#,Sname

(8)查询课程1比课程2成绩高的学生扩展阅读:

数据库操作的注意事项

1、对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。

2、应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:select id from t where num is null

最好不要给数据库留NULL,尽可能的使用NOT NULL填充数据库.

备注、描述、评论之类的可以设置为NULL,其他的,最好不要使用NULL。

不要以为NULL不需要空间,比如:char(100)型,在字段建立时,空间就固定了,不管是否插入值(NULL也包含在内),都是占用100个字符的空间的,如果是varchar这样的变长字段,null不占用空间。

可以在num上设置默认值0,确保表中num列没有null值,然后这样查询:select id from t where num = 0

3、应尽量避免在 where 子句中使用 != 或 <> 操作符,否则将引擎放弃使用索引而进行全表扫描。

4、应尽量避免在 where 子句中使用 or 来连接条件,如果一个字段有索引,一个字段没有索引,将导致引擎放弃使用索引而进行全表扫描,如:select id from t where num=10 or Name = \'admin\'

可以这样查询:
select id from t where num = 10
union all
select id from t where Name = \'admin\'

5、in 和 not in 也要慎用,否则会导致全表扫描,如:select id from t where num in(1,2,3)

对于连续的数值,能用 beeen 就不要用 in 了:select id from t where num beeen 1 and 3

很多时候用exists 代替 in是一个好的选择:select num from a where num in(select num from b)

用下面的语句替换:select num from a where exists(select 1 from b where num=a.num)

6、下面的查询也将导致全表扫描:select id from t where name like ‘%abc%’

若要提高效率,可以考虑全文检索。

7、如果在 where 子句中使用参数,也会导致全表扫描。因为SQL只有在运行时才会解析局部变量,但优化程序不能将访问计划的选择推迟到运行时;它必须在编译时进行选择。然 而,如果在编译时建立访问计划,变量的值还是未知的,因而无法作为索引选择的输入项。

9. 急求SQL:查询出其2号课程成绩比所有1号课程成绩都低那些学生的学号。

select 学号 from 选课表 where 课程号 = 2 and 成绩 <
(select min(成绩) from 选课表 where 课程号 = 1 )

这是习题吧 ?????

10. 有一张学生表 字段有:学生编号 ,学生成绩,科目 sql怎么查询科目一成绩比科目二高的 学生编号

SELECT a.编号 FROM(
SELECT 编号,成绩 FROM student WHERE 科目=‘科目一’
)AS a
LEFT JOIN (
SELECT 编号,成绩 FROM student WHERE 科目=‘科目二’
)AS b ON a.编号=b.编号
WHERE a.成绩>b.成绩

Mysql多表联查——经典50题

目录



1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数。

// 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
	SELECT stu.s_id, stu.s_name, s1.s_score FROM student stu, score s1, score s2
	WHERE stu.s_id = s1.s_id
	AND stu.s_id = s2.s_id
	AND s1.c_id = '01'
	AND s2.c_id = '02'
	AND s1.s_score > s2.s_score

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

// 方法一
SELECT c1.c_name, 
		( SELECT s1.s_id FROM score s1 
			WHERE s1.c_id = c1.c_id
			ORDER BY s1.s_score DESC LIMIT 0,1
		) 第一名,
		( SELECT s1.s_id FROM score s1 
			WHERE s1.c_id = c1.c_id
			ORDER BY s1.s_score DESC LIMIT 1,1
		) 第二名
	FROM course c1
// 方法二	
	SELECT s1.c_id, s1.s_id, s1.s_score
	FROM score s1 
	LEFT JOIN score s2 ON s1.c_id = s2.c_id
	AND s1.s_score < s2.s_score
	GROUP BY s1.c_id, s1.s_id
	HAVING COUNT(s1.c_id) < 2
	ORDER BY s1.c_id ASC, s1.s_score DESC
// 方法三	
SELECT sc.c_id, sc.s_score FROM score sc 
WHERE (
	SELECT COUNT(*) FROM score 
	WHERE sc.c_id = score.c_id 
	AND sc.s_score < score.s_score
) < 2 
ORDER BY sc.c_id ASC,sc.s_score DESC;

3.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩。

SELECT stu.s_id, stu.s_name, SUM(sc.s_score) / ( SELECT COUNT(*) FROM course) AS '平均分' FROM student stu, score sc
WHERE stu.s_id = sc.s_id
GROUP BY sc.s_id
HAVING SUM(sc.s_score) / ( SELECT COUNT(*) FROM course) >= 60 

-- AVG计算平均分,缺考会被忽略--
SELECT stu.s_id, stu.s_name, AVG(sc.s_score) AS '平均分' FROM student stu, score sc
WHERE stu.s_id = sc.s_id
GROUP BY sc.s_id
HAVING AVG(sc.s_score) >= 60 

-- 查询某一个学生平均分。
SELECT s_id, c_id, SUM(s_score) AS '总分', SUM(s_score) /5 AS '平均分' FROM score WHERE s_id = 3

4.查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩。(包括有成绩的和无成绩的)

SELECT stu.s_id, stu.s_name, SUM(sc.s_score) / ( SELECT COUNT(*) FROM course) AS '平均分' FROM student stu, score sc
WHERE stu.s_id = sc.s_id
GROUP BY sc.s_id
HAVING SUM(sc.s_score) / ( SELECT COUNT(*) FROM course) < 60 
UNION
SELECT stu.s_id, stu.s_name, avg(sc.s_score)
FROM student stu
LEFT JOIN score sc ON stu.s_id = sc.s_id
GROUP BY stu.s_id
HAVING avg(sc.s_score) IS NULL;

5.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩。

SELECT stu.s_id AS'学号', stu.s_name AS'姓名', COUNT(sc.c_id)AS'选课总数', SUM(sc.s_score) AS '总成绩' 
FROM student stu, score sc
WHERE stu.s_id = sc.s_id
GROUP BY stu.s_id

6.查询"李"姓老师的数量。

SELECT COUNT(t_id)AS '总数' FROM teacher WHERE t_name LIKE '李%' 

7.查询学过"张三"老师授课的同学的信息。

SELECT s_id, s_name FROM student
WHERE s_id IN (
	SELECT sc.s_id FROM score sc, course c, teacher t
	WHERE sc.c_id = c.c_id
	AND c.t_id = t.t_id
	AND t.t_name = '张三'
)

8.查询没学过"张三"老师授课的同学的信息。

SELECT s_id, s_name FROM student
WHERE s_id NOT IN (
	SELECT sc.s_id FROM score sc, course c, teacher t
	WHERE sc.c_id = c.c_id
	AND c.t_id = t.t_id
	AND t.t_name = '张三'
)

9.查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息。

/* -- EXISTS 检语法:
select 字段名|表达式 from 表名1 a
where exists (select * from 表名2 b where a. 字段名a=b.字段名b) ;
关键字EXISTS:构造子查询,子查询是否返回结果集,返回则才进行外层查询。

如果子查询的结果集不为空,则EXISTS返回的结果为TRUE,此时外层查询语句将进行查询;
如果子查询的结果集为空,则EXISTS返回的结果为FLASE,此时外层查询语句将不进行查询;
*/                 
SELECT stu.s_id, stu.s_name FROM student stu, score sc
WHERE stu.s_id = sc.s_id
AND sc.c_id = '01'
AND EXISTS (
	SELECT * FROM score sc 
	WHERE sc.s_id = stu.s_id 
	AND sc.c_id = '02'
	)

#10.查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息。

SELECT stu.s_id, stu.s_name FROM student stu, score sc
WHERE stu.s_id = sc.s_id
AND sc.c_id = '01'
AND stu.s_id NOT IN (
	SELECT s_id FROM score WHERE c_id = '02'
)

11.查询没有学全所有课程的同学的信息 。

SELECT s_id, s_name FROM student 
WHERE s_id NOT IN (
	SELECT s_id FROM score  
	GROUP BY s_id
	HAVING COUNT(c_id) = (SELECT COUNT(c_id) FROM course)
)

12.查询至少有一门课与学号为"01"的同学所学相同的同学的信息。

/**
* 关键字 DISTINCT 去重
*/
SELECT DISTINCT stu.s_id, stu.s_name FROM student stu, score sc
WHERE stu.s_id = sc.s_id
AND sc.c_id IN (
	SELECT c_id FROM score WHERE s_id = '1'
)

13.查询和"01"号的同学学习的课程完全相同的其他同学的信息。

SELECT * FROM student WHERE s_id IN (
    SELECT s_id FROM score 
    GROUP BY s_id
    HAVING group_concat(c_id) = (
        SELECT group_concat(c_id) FROM score
        WHERE s_id = 1
				) 
		AND s_id != 1
);

/*group_concat()函数:合并同列变成一行,默认以逗号分隔显示
*/
select group_concat(stu.s_name)AS '姓名' from student stu 

-- 上面原查询语句 --
select * from student where s_id in (
    select s_id from score 
    group by s_id
    having group_concat(c_id ORDER BY c_id) = (
        select group_concat(c_id ORDER BY c_id) as str2 from score
        where s_id = 1) and s_id != 1);
				
select * from student where s_id in (
	select s_id from score where s_id not in (
	select s_id from score where c_id not in (
	select c_id from score where s_id='01'))
group by s_id 
having count(*)=(select count(*) from score where s_id='01') and s_id != '01');

14.查询没学过"张三"老师讲授的任一门课程的学生姓名。

SELECT s_id,s_name FROM student 
WHERE s_id NOT IN (
	SELECT sc.s_id FROM score sc, course c, teacher t
	WHERE sc.c_id = c.c_id
	AND c.t_id = t.t_id
	AND t.t_name = '张三'
)

15.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩。

// 方法一
SELECT stu.s_id, stu.s_name, AVG(sc.s_score) 
FROM score sc JOIN student stu ON stu.s_id = sc.s_id
GROUP BY stu.s_id
HAVING count(sc.s_score<60 or null) >= 2
// 方法二
SELECT stu.s_id, stu.s_name, AVG(sc.s_score) 
FROM score sc, student stu
WHERE stu.s_id = sc.s_id
GROUP BY stu.s_id
HAVING count(sc.s_score<60 or null) > 1

16.检索"01"课程分数小于60,按分数降序排列的学生信息。

SELECT stu.s_id, stu.s_name, sc.s_score FROM student stu 
LEFT JOIN score sc ON stu.s_id = sc.s_id
WHERE sc.c_id = '01'
AND sc.s_score < 60
ORDER BY sc.s_score DESC

17.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩。

/*
* 函数:group_concat
* 作用:函数返回一个字符串结果,该结果由分组中的值连接组合而成。(一列多行数据,在一行显示。)
* group_concat( [DISTINCT] 连接字段 [Order BY 排序字段 ASC/DESC] [Separator ‘分隔符’] ) 
*/
SELECT stu.s_id, stu.s_name,
	GROUP_CONCAT(c.c_name) '课程',
	GROUP_CONCAT(sc.s_score) '分数',
	AVG(sc.s_score) '平均分'
FROM student stu
LEFT JOIN score sc ON stu.s_id = sc.s_id
JOIN course c ON sc.c_id = c.c_id
GROUP BY sc.s_id 
ORDER BY AVG(sc.s_score) DESC

18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分。

SELECT 
	c.c_id AS'课程id',
	c.c_name AS'课程名称',
	MAX(sc.s_score) AS'最高分',
	MIN(sc.s_score) AS'最低分',
	AVG(sc.s_score) AS'平均分'
FROM course c, score sc
WHERE c.c_id = sc.c_id
GROUP BY sc.c_id

-- 验证 --
SELECT s_score FROM score WHERE c_id = '04' ORDER BY s_score

19.查询出各科成绩总分,并按总分降序排序:以如下形式显示:课程ID,课程name,总分

SELECT 
	c.c_id AS'课程ID', 
	c.c_name AS'课程名称',
	SUM(sc.s_score) AS'总分'
FROM score sc, course c
WHERE sc.c_id = c.c_id
GROUP BY sc.c_id
ORDER BY SUM(sc.s_score) DESC

20.查询学生的总成绩及学生信息。

SELECT stu.s_id, stu.s_name,SUM(sc.s_score)AS'总成绩' FROM student stu, score sc
WHERE sc.s_id = stu.s_id
GROUP BY stu.s_id

21.查询不同老师所教不同课程平均分从高到低显示。

SELECT 
	t.t_id, 
	t.t_name AS'教师名称',
	c.c_name AS'课程名称',
	AVG(sc.s_score) AS'平均分'
FROM teacher t, course c, score sc
WHERE t.t_id = c.t_id
AND c.c_id = sc.c_id
GROUP BY t.t_id
ORDER BY AVG(sc.s_score) DESC

22.查询每门课程被选修的学生数。

SELECT c.c_id, c.c_name AS'课程名称', COUNT(c.c_id)AS'选修人数' 
FROM course c, score sc
WHERE c.c_id = sc.c_id
GROUP BY c.c_id

23.查询出只有两门课程的全部学生的学号和姓名。

SELECT stu.s_id, stu.s_name FROM student stu, score sc
WHERE stu.s_id = sc.s_id
GROUP BY sc.s_id
HAVING COUNT(sc.c_id) = 2

24.查询男生、女生人数

SELECT 
	s_sex AS'性别',
	COUNT(1) AS '人数'
FROM student 
GROUP BY s_sex

25.查询名字中含有"风"字的学生信息。

SELECT s_id, s_name FROM student
WHERE s_name LIKE '%风%'

26.查询1990年出生的学生名单。

SELECT * FROM student WHERE s_birth LIKE '1990%'
-- 方法二 --
/*
* 函数 BETWEEN ... AND 取介于两个值之间的数据范围。这些值可以是数值、文本或者日期。
*/
SELECT * FROM student WHERE s_birth BETWEEN '1990-1-1' AND '1990-12-31';

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

SELECT c.c_name, avg(s_score) FROM score sc, course c 
WHERE sc.c_id = c.c_id 
GROUP BY c.c_id ORDER BY AVG(sc.s_score) DESC,sc.c_id

28.查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩。

SELECT stu.s_id, stu.s_name, AVG(sc.s_score) FROM student stu, score sc
WHERE stu.s_id = sc.s_id
GROUP BY sc.s_id HAVING AVG(sc.s_score) > 85

29.查询课程名称为"数学",且分数低于60的学生姓名和分数。

SELECT stu.s_id, stu.s_name, sc.s_score FROM student stu, score sc, course c
WHERE stu.s_id = sc.s_id
AND c.c_id = sc.c_id
AND c.c_name = '数学'
AND sc.s_score < 60

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

SELECT stu.s_name, c.c_name, sc.s_score FROM student stu, course c, score sc
WHERE stu.s_id = sc.s_id
AND sc.c_id = c.c_id
GROUP BY sc.s_score HAVING sc.s_score >70

31.查询不及格的课程。

SELECT stu.s_name, c.c_name, sc.s_score FROM student stu, course c, score sc
WHERE stu.s_id = sc.s_id
AND sc.c_id = c.c_id
GROUP BY sc.s_score HAVING sc.s_score < 60

32.查询课程编号为01且课程成绩在80分以上的学生的学号和姓名。

SELECT stu.s_id,stu.s_name, c.c_name, sc.s_score FROM student stu, course c, score sc
WHERE stu.s_id = sc.s_id
AND sc.c_id = c.c_id
AND c.c_id = '01'
AND  sc.s_score > 80

33.求每门课程的学生人数。

SELECT c.c_name, COUNT(c.c_id) FROM score sc, course c
WHERE sc.c_id = c.c_id
GROUP BY c.c_id

34.查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩。

SELECT stu.s_id, stu.s_name, MAX(sc.s_score) 
FROM student stu, course c, score sc, teacher t
WHERE stu.s_id = sc.s_id
AND sc.c_id = c.c_id
AND c.t_id = t.t_id
AND t.t_name = '张无忌'
GROUP BY c.c_id 

-- 方法二 --
SELECT s.*,MAX(sc.s_score)
FROM student s,score sc
WHERE s.s_id=sc.s_id
AND sc.c_id in(
SELECT c_id
FROM teacher t,course c
WHERE t.t_id=c.t_id
AND t.t_name='张三');

35.统计每门课程的学生选修人数(超过5人的课程才统计)。

-- 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 --
SELECT c_id, COUNT(s_id) AS num FROM score 
GROUP BY c_id HAVING num > 5 
ORDER BY num DESC, c_id ASC

36.检索至少选修两门课程的学生学号。

SELECT stu.s_id, stu.s_name 
FROM student stu, score sc 
WHERE stu.s_id = sc.s_id 
GROUP BY s_id HAVING COUNT(c_id) > 2

37.查询选修了全部课程的学生信息。

SELECT stu.s_id, stu.s_name FROM score sc, student stu
WHERE sc.s_id = stu.s_id
GROUP BY s_id HAVING COUNT(c_id)=(SELECT COUNT(c_id) FROM course)

38.查询各学生的年龄。

-- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减1 --
/**
方法一:
TIMESTAMPDIFF() 函数:计算两个日期的时间差
CURDATE() 函数:返回当前的日期

语法结构  TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)

参数说明:
unit: 日期比较返回的时间差单位,常用可选值如下:
SECOND:秒    MINUTE:分钟   HOUR:小时
DAY:天       WEEK:星期     MONTH:月
QUARTER:季度
YEAR:年
datetime_expr1: 要比较的日期1    datetime_expr2: 要比较的日期2

TIMESTAMPDIFF函数返回datetime_expr2 - datetime_expr1的结果,其中datetime_expr1和datetime_expr2可以是DATE或DATETIME类型值
原文链接:https://blog.csdn.net/Hudas/article/details/124351790
**/
SELECT stu.s_id, stu.s_name, TIMESTAMPDIFF(YEARsql语句课程练习

sql语句课程练习

Mysql45题

学生课程成绩

sql查询作业答案

Mysql多表查询练习