搞定SQL面试(1)

SQL是数据分析的第一技能,很多人转行数据分析,说自己学python,学R,但是却忽略了sql的重要性,去面试数据分析岗,专业点的公司,基本都会有sql的面试题,整理了下网上的sql面试题,分享给大家。

创建表,包括学生表,课程表,关系表,教师表
CREATE TABLE student 
  ( 
     sid    INT, 
     sname varchar(32), 
     sage  INT, 
     ssex  varchar(8) 
  ) 

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

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

CREATE TABLE teacher 
  ( 
     tid    INT, 
     tname varchar(16) 
  ) 
插入数据
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
  • 1、查询“001”课程比“002”课程成绩高的所有学生的学号;
select a.sid from 
(SELECT sid,score from sc where cid='001') a,
(select sid,score from sc where cid='002') b
  • 2、查询平均成绩大于60分的同学的学号和平均成绩;
select sid,avg(score) as avg_score from sc 
group by sid having avg(score)>60;
  • 3、查询所有同学的学号、姓名、选课数、总成绩;
select a.sid,a.sname,count(b.cid) as nums_course,sum(score) as total_score
from student a left join sc b on a.sid=b.sid group by 1,2;
  • 4、查询姓“李”的老师的个数;
select count(distinct(Tname)) 
  from Teacher 
  where Tname like '李%';
  • 5、查询没学过“叶平”老师课的同学的学号、姓名;
select a.sid,a.sname from student a where a.sid not in 
(select distinct sc.sid from sc,course,teacher where sc.cid=course.cid 
and course.tid=teacher.tid and teacher.tname="叶平")
  • 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
select a.sid,a.sname from student a,sc b
where a.sid=b.sid and b.cid='001' and exists
(select 1 from sc c where b.sid=c.sid and c.cid='002')
  • 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
select sid,sname from student where sid in
(select sid from sc,course,teacher where 
sc.cid=course.cid and course.tid=teacher.tid and teacher.tname='叶平' 
group by sid
having count(sid)=
(select count(distinct cid) from course a,teacher b where 
a.tid=b.tid and b.tname='叶平'))
  • 8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名
Select sid,Sname from 
(select Student.sid,Student.Sname,score ,
(select score from SC SC_2 where 
SC_2.sid=Student.sid and SC_2.cid='002') score2 
 from Student,SC where Student.sid=SC.sid and cid='001') S_2 
where score2 <score;
  • 9、查询所有课程成绩小于60分的同学的学号、姓名
 select sid,Sname 
 from Student 
 where Sid  in (select S.sid from Student AS S,SC where S.Sid=SC.sid and score<60); 
  • 10、查询没有学全所有课的同学的学号、姓名;
select s.sid,s.sname from student s
where s.sid not in(
    select sc.sid from sc sc
    group by sc.sid
    having count(distinct sc.cid)=(
        select count(distinct c.cid) from course c
    )
)
  • 11、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名
select distinct(s.sid),s.Sname
from Student s,SC sc
where s.sid=sc.sid and sc.sid in
(
    select distinct(sc2.cid) from SC sc2
    where sc2.sid='001'
)
order by s.sid asc
  • 12、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
update sc set score=
(
    select avg(score) from sc sc,course c, teacher t
    where sc.cid=c.cid and c.tid=t.tid and  t.tname='叶平'
)
where cid in
(
 select distinct(sc.cid) from sc sc,course c,teacher t
 where sc.cid=c.cid and c.tid=t.tid and t.tname='叶平'
)
  • 13、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名
select student.sid, student.sname
from sc,student 
where sc.cid in (select sc.cid from sc where sc.sid='002') 
and sc.sid = student.sid and sc.sid != '002'
group by student.sid, student.sname
having count(*)=(select count(*) from sc where sc.sid='002');

14、删除学习“叶平”老师课的SC表记录;

delete from sc where cid in
(select c.cid from course c,teacher t
    where c.tid=t.tid and t.tname='叶平'
)

15、向SC表中插入一些记录,这些记录要求符合以下条件:
①没有上过编号“002”课程的同学学号;
②插入“002”号课程的平均成绩;

insert into sc
select s.sid,'002',(select avg(score) from sc where cid='002')
from student s
where s.sid not in (select distinct(sid) from sc where cid!='002')

文章转载自https://www.cnblogs.com/edisonchou/p/3878135.html
欢迎加入数据分析交流群(加群备注博客园)
1335083-20180915111519706-883627347.png

转载于:https://www.cnblogs.com/linxiaochi/p/9650339.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值