网传的经典SQL面试题(多解法)(一)
程序员文章站
2022-05-11 09:52:28
网上有很多SQL笔试经典50题及答案解析,并且答案和详解也有很多了,这里只是从巩固知识的角度从多方位想出解法,也算是将SQL知识进行系统的巩固,在不考虑运行效率的基础上,对于大部分题目本文将提供不同的解法。本文尽量基于标准SQL语句进行编写。首先建立四张表:学生信息表 student、课程信息表 course、学生成绩信息表sc、教师信息表 teacher。create table Student(sid varchar(10),sname varchar(10),sage datetime,ssex...
网上有很多SQL笔试经典50题及答案解析,并且答案和详解也有很多了,这里只是从巩固知识的角度从多方位想出解法,也算是将SQL知识进行系统的巩固,在不考虑运行效率的基础上,对于大部分题目本文将提供不同的解法。本文尽量基于标准SQL语句进行编写。
首先建立四张表:学生信息表 student、课程信息表 course、学生成绩信息表sc、教师信息表 teacher。
create table Student(sid varchar(10),sname varchar(10),sage datetime,ssex nvarchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
create table Course(cid varchar(10),cname varchar(10),tid varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
create table Teacher(tid varchar(10),tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
create table SC(sid varchar(10),cid varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
1、查询“01”课程比“02”课程成绩高的所有学生的学号;
-- 关联子查询
SELECT sid
FROM sc A
WHERE cid='01'
AND A.score>(SELECT score
FROM sc B
WHERE cid='02'
AND A.sid=B.sid);
-- 联结
SELECT
A.sid
FROM (SELECT * FROM sc WHERE cid = '01') A
JOIN (SELECT * FROM sc WHERE cid = '02') B
ON A.sid = B.sid
AND A.score > B.score;
SELECT A.sid
FROM sc A
WHERE EXISTS (SELECT *
FROM sc B
WHERE A.sid = B.sid
AND A.cid = '01'
AND B.cid = '02'
AND A.score > B.score
)
2、查询平均成绩大于60分的同学的学号和平均成绩;
SELECT sid,CAST(AVG(score) AS DECIMAL(15,2)) 均分
FROM sc
GROUP BY sid
HAVING AVG(score) > 60;
3、查询所有同学的学号、姓名、选课数、总成绩
SELECT B.sid, B.sname,
COUNT(*) 选课数,
SUM(score) 总成绩
FROM sc A ,student B
WHERE A.sid = B.sid
GROUP BY B.sid , B.sname
4、查询姓“李”的老师的个数;
SELECT
COUNT(distinct tid) teacher_cnt
FROM teacher
WHERE tname like '李%'
5、查询没学过“张三”老师课的同学的学号、姓名;
--使用with表达式
WITH new_sc AS
(
SELECT A.sid,A.cid,A.score,C.tname
FROM sc A,course B,teacher C
WHERE A.cid=B.cid
AND B.tid=C.tid
)
SELECT DISTINCT B.sid,B.sname
FROM student B
LEFT JOIN new_sc A
ON A.sid=B.sid
WHERE NOT EXISTS(SELECT *
FROM new_sc C
WHERE A.sid=C.sid
AND C.tname='张三');
本文地址:https://blog.csdn.net/m0_46412065/article/details/107644728