标签:union and group by 学习 公司 sql sum 数据分析 having
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,71select a.sid from 
(SELECT sid,score from sc where cid=‘001‘) a,
(select sid,score from sc where cid=‘002‘) bselect sid,avg(score) as avg_score from sc 
group by sid having avg(score)>60;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;select count(distinct(Tname)) 
  from Teacher 
  where Tname like ‘李%‘;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="叶平")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‘)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=‘叶平‘))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; select sid,Sname 
 from Student 
 where Sid  in (select S.sid from Student AS S,SC where S.Sid=SC.sid and score<60); 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
    )
)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 ascupdate 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=‘叶平‘
)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
欢迎加入数据分析交流群(加群备注博客园)

标签:union and group by 学习 公司 sql sum 数据分析 having
原文地址:https://www.cnblogs.com/linxiaochi/p/9650339.html