sql常见题型

SQL面试题(学生表,课程表,成绩表,教师表)

1.建立表的骨架

CREATE TABLE student 
  ( 
     sid    INT, 
     sname nvarchar(32), 
     sage  INT, 
     ssex  nvarchar(8) 
  ) 

CREATE TABLE course 
  ( 
     cid    INT, 
     cname nvarchar(32), 
     tid    INT 
  ) 

CREATE TABLE sc 
  ( 
     sid    INT, 
     cid    INT, 
     score INT 
  ) 

CREATE TABLE teacher 
  ( 
     tid    INT, 
     tname nvarchar(16) 
  )

2.插入测试数据

insert into Student select 1,N‘刘一‘,18,N‘男‘ union all
 select 2,N‘钱二‘,19,N‘女‘ union all
 select 3,N‘张三‘,17,N‘男‘ union all
 select 4,N‘李四‘,18,N‘女‘ union all
 select 5,N‘王五‘,17,N‘男‘ union all
 select 6,N‘赵六‘,19,N‘女‘ 
 
 insert into Teacher select 1,N‘叶平‘ union all
 select 2,N‘贺高‘ union all
 select 3,N‘杨艳‘ union all
 select 4,N‘周磊‘
 
 insert into Course select 1,N‘语文‘,1 union all
 select 2,N‘数学‘,2 union all
 select 3,N‘英语‘,3 union all
 select 4,N‘物理‘,4
 
 insert into SC 
 select 1,1,56 union all 
 select 1,2,78 union all 
 select 1,3,67 union all 
 select 1,4,58 union all 
 select 2,1,79 union all 
 select 2,2,81 union all 
 select 2,3,92 union all 
 select 2,4,68 union all 
 select 3,1,91 union all 
 select 3,2,47 union all 
 select 3,3,88 union all 
 select 3,4,56 union all 
 select 4,2,88 union all 
 select 4,3,90 union all 
 select 4,4,93 union all 
 select 5,1,46 union all 
 select 5,3,78 union all 
 select 5,4,53 union all 
 select 6,1,35 union all 
 select 6,2,68 union all 
 select 6,4,71

3.sql常见试题

1.查询 课程“1” 比 课程“2” 成绩高 的所有学生的学号

select a.sid
from 
(select * from SC where cid=‘1‘) a,
(select * from SC where cid=‘2‘) b 
where 
a.sid=b.sid and a.score>b.score

2.查询平均成绩大于60分的同学的学号和平均成绩

select sid,avg(score)
from sc
group by sid having avg(score)>60

3、查询所有同学的学号、姓名、选课数、总成绩

select s.sid,sname,count(cid),sum(score)
from 
student s 
left join sc
on s.sid=sc.sid
group by s.sid

4.查询姓“周”的老师的个数

select count(distinct (tname))
from 
teacher
where tname like ‘周%‘

5.查询没学过“叶平”老师课的同学的学号、姓名

select s.sid,sname
from student s
where sid not in (
select sid 
from sc,course c,teacher t 
where sc.cid=c.cid and c.tid=t.tid and t.tname=‘叶平‘)

6.查询学过课程“1”和“2”的同学的学号、姓名

select sid,sname
from student
where sid in(
select a.sid 
from
(select * from sc where cid=1) a,
(select * from sc where cid=2) b
where a.sid=b.sid)

7.查询学过“叶平”老师所教的所有课的同学的学号、姓名

select s.sid,sname
from student s
where sid in (
select sid 
from sc,course c,teacher t 
where sc.cid=c.cid and c.tid=t.tid and t.tname=‘叶平‘)

8.查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名

select newtable.sid,newtable.sname 
from (
select  s.sid,s.sname,score score1,(select score from sc s2 where s2.sid=s.sid and s2.cid=‘002‘) score2 
from student s,sc where s.sid=sc.sid and cid=‘1‘) newtable 
where score2<score1

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

select sid,sname
from student 
where sid not in (
select sid from sc where score>60)

10.查询没有学全所有课的同学的学号、姓名

select s.sid,sname
from student s left join sc
on s.sid=sc.sid
group by s.sid 
having count(cid)<(select count(cid) from course)
//where 后面不能跟函数

11.查询至少有一门课与学号为“1”的同学所学相同 的同学的学号和姓名

select distinct s.sid,sname
from student s,sc
where s.sid=sc.sid and sc.cid in (select cid from sc where sid=1)

12.查询至少学过学号为“1”同学所有一门课的其他同学学号和姓名

//1.第一种
select sid,sname 
from student 
where sid in(
select sid
from sc 
where sid!=1 and cid in(
select cid from sc where sid=1)
group by sid
having count(cid)>=(select count(cid) from sc where sid=2)
)

//2.第二种
select sid ,sname
from student
where sid!=1 and sid in(
select a.sid
from (
(select * from sc where cid=1) a,
(select * from sc where cid=2) b,
(select * from sc where cid=3) c,
(select * from sc where cid=4) d )
where a.sid=b.sid and b.sid=c.sid and c.sid=d.sid
)

13.查询各科成绩的平均分

select cid, avg(score)
from sc 
group by cid

14.查询和“2”号的同学学习的课程完全相同的其他同学学号和姓名

select sid,sname 
from student 
where sid in(
select sid
from sc 
where sid!=2 and cid in(
select cid from sc where sid=1)
group by sid
having count(cid)=(select count(cid) from sc where sid=2)
)

15.按平均成绩从高到低显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分

select sid 学生id,
(select score from sc where sc.sid=s.sid and cid=1) 语文, 
(select score from sc where sc.sid=s.sid and cid=2) 数学,
(select score from sc where sc.sid=s.sid and cid=3) 英语,
count(*) 有效课程数,avg(s.score) 平均成绩
from 
sc s
group by sid

**16.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 **

select cid 课程ID, max(score)最高分,min(score)最低分
from sc
group by cid

**17.按各科平均成绩从低到高和及格率的百分数从高到低顺序 **

select cid 课程ID, avg(score) 平均分,
100 * SUM(CASE WHEN  score>=60 THEN 1 ELSE 0 END)/COUNT(*)  及格百分数 
from sc
group by cid
order by avg(score),及格百分数 desc

18.查询课程平均成绩和及格率的百分数( 用"1行"显示 ): 语文(1),数学(2),英语(3),物理(4)

select 
sum(case when cid=1 then score else 0 end)/sum(case when cid=1 then 1 else 0 end) 语文平均分,
sum(case when cid=1 and score>=60 then 1 else 0 end)/sum(case when cid=1 then 1 else 0 end)*100 语文及格率,
sum(case when cid=2 then score else 0 end)/sum(case when cid=2 then 1 else 0 end) 数学平均分,
sum(case when cid=2 and score>=60 then 1 else 0 end)/sum(case when cid=2 then 1 else 0 end)*100 数学及格率,
sum(case when cid=3 then score else 0 end)/sum(case when cid=3 then 1 else 0 end) 英语平均分,
sum(case when cid=3 and score>=60 then 1 else 0 end)/sum(case when cid=3 then 1 else 0 end)*100 英语及格率,
sum(case when cid=4 then score else 0 end)/sum(case when cid=4 then 1 else 0 end) 物理平均分,
sum(case when cid=4 and score>=60 then 1 else 0 end)/sum(case when cid=4 then 1 else 0 end)*100 物理及格率
from sc

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

select t.tname, c.cname, avg(s.score) 平均分
from
teacher t,sc s,course c
where t.tid=s.sid and s.sid=c.cid
group by c.cid 
order by 平均分 desc

20.查询如下课程成绩第 3 名到第 6 名的学生成绩单:企业管理(001),马克思(002),UML (003),数据库(004)
[学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩

select s.sname,newtable.*
from
student s,
(select e.*,a.score 企业管理,b.score 马克思,c.score UML,d.score 数据库
from  
(select * from sc where cid = 1) a
left join 
(select * from sc where cid = 2) b
on a.sid=b.sid
left join 
(select * from sc where cid = 3) c
on b.sid=c.sid
left join 
(select * from sc where cid = 4) d
on c.sid=d.sid
left join 
(select sid, avg(score) 平均成绩 from sc group by sid) e
on e.sid=a.sid) newtable
where s.sid=newtable.sid

**21.统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] **

(在这道题,换了编辑器SQLyog,之前是heidiSQL)

SELECT 
sc.`cid` 课程ID,
course.`cname` 课程名, 
SUM(CASE WHEN sc.`score`>85 AND sc.`score`<=100 THEN 1 ELSE 0 END) ‘[100-85]‘,
SUM(CASE WHEN sc.`score`>70 AND sc.`score`<=85 THEN 1 ELSE 0 END) ‘[85-70]‘,
SUM(CASE WHEN sc.`score`>60 AND sc.`score`<=70 THEN 1 ELSE 0 END) ‘[70-60]‘,
SUM(CASE WHEN sc.`score`<60 THEN 1 ELSE 0 END) ‘[60-0]‘
FROM
sc,course
WHERE sc.`cid`=course.`cid`
GROUP BY sc.`cid`

**22.查询各科成绩的记录: 学生ID,课程ID,分数 **

mysql中没有top关键字,使用的limit

SELECT  sid 学生ID,cid 课程ID,score 分数
FROM sc 
ORDER BY cid,score DESC

**23.查询每门课程被选修的学生数 **

SELECT cid 课程ID,COUNT(score) 人数
FROM sc
GROUP BY cid

**24.查询出只选修了一门课程的全部学生的学号和姓名 **

SELECT  s.sid 学生ID,st.`sname` 姓名,COUNT(cid)
FROM sc s,student st
WHERE s.`sid`=st.`sid`
GROUP BY s.`sid`
HAVING COUNT(cid)=1

sql常见题型

上一篇:mysql分组查询


下一篇:第2章 MySql数据库