50道SQL练习题(刷完直接进大厂)

Posted 林夕07

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了50道SQL练习题(刷完直接进大厂)相关的知识,希望对你有一定的参考价值。

目录

表结构

设有学生表(T_Student)、教师表(T_teacher)、课程表(T_Course)和成绩表(T_score)。
表结构及其关系如下UML图所示。

创建表

下面列出了Oracle数据库表的创建代码。我并没有设主键。

create table T_Student
(
      Student_ID int, 
      Student_name varchar2(32),
      Student_age int,
      Student_sex char(1)
);
//学生表由于涉及到个人信息,这里不做展示。请自行补全。
create table T_teacher
(
       t_id int,
       t_name varchar2(20)
);
insert into T_Teacher values(101,'葛老师');
insert into T_Teacher values(102,'五木老师');
insert into T_Teacher values(103,'华仔老师');
insert into T_Teacher values(104,'罗老师');
insert into T_Teacher values(105,'黄老师');
create table T_Course
(
     Course_ID int,
     Course_name varchar2(30), 
     T_ID int 
);
//某一个老师教的课程可以是多种的。
insert into T_Course values(101,'数据库',101);
insert into T_Course values(102,'QT',102);
insert into T_Course values(103,'C++',103);
insert into T_Course values(104,'C语言',104);
insert into T_Course values(105,'linux',105);

create table T_score
(
      Student_ID int,  
      Course_ID int,
      score number(5,2)
);
insert into T_score values(1,101,80);

insert into T_score values(9,101,76);
insert into T_score values(9,102,44);
insert into T_score values(9,104,83);
insert into T_score values(9,105,70);

insert into T_score values(11,101,83);
insert into T_score values(11,102,75);
insert into T_score values(11,103,98);
insert into T_score values(11,104,66);
insert into T_score values(11,105,83);

insert into T_score values(13,104,83);
insert into T_score values(13,105,91);

insert into T_score values(14,101,79);
insert into T_score values(14,102,100);
insert into T_score values(14,103,81);
insert into T_score values(14,104,77);

insert into T_score values(15,101,84);
insert into T_score values(15,103,88);
insert into T_score values(15,104,60);
insert into T_score values(15,105,70);
//只列出部分成绩表的数据 可自行补齐。

练习题

这里列出了大约50个SQL练习题,解法未考虑效率只是为了尽可能多的练习语法。下面解法为Oracle数据库语法格式。

1、查询“001”课程比“002”课程成绩高的所有学生的学号

--解法一
create or replace view v11 
as
select *
from T_score
where course_id = 101;

create or replace view v12 
as
select *
from T_score
where course_id = 102;

select a.student_id 学号
from v11 a, v12 b
where a.student_id = b.student_id and a.score > b.score; 
--解法二  
select a.student_id,
       max(case when b.course_id = 101 then b.score else NULL end) aa,
       max(case when b.course_id = 102 then b.score else NULL end) bb
from T_student a, T_score b
where a.student_id = b.student_id and
group by a.student_id;       

select student_id 学号
from v13
where aa > bb;
--解法三
select a.student_id 学号
from t_score a, t_score b
where a.student_id = b.student_id and a.course_id = '101'
      and b.course_id = '102' and a.score > b.score;

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

--解法一
select a.student_id 学号, 
       a.student_name 姓名, 
       count(nvl(b.course_id, 0)) 选课数, 
       sum(b.score) 总成绩
from T_student a, T_score b
where a.student_id = b.student_id
group by a.student_id, a.student_name;

3、查询平均成绩大于60分的同学的 学号和平均成绩

select student_id 学号,
       trunc(avg(score), 2) 平均成绩 
from T_score 
group by student_id
having avg(score) > 60;

4、查询姓“葛”的老师的个数

select count(1) 姓葛老师的人数
from T_teacher
where t_name like '葛%'; 

5、查询没学过“五木”老师课的同学的学号、姓名

--解法一
create or replace view v51
as
select b.course_id
from T_teacher a, T_course b
where a.t_id = b.t_id and a.t_name = '五木老师';

create or replace view v52
as
select a.student_id aa, 
       sum(case when b.course_id = c.course_id then 1 else 0 end) bb,
       a.student_name cc
from T_student a left join T_score b
                 on a.student_id = b.student_id ,v51 c
group by a.student_id, a.student_name;

select aa 学号, cc  姓名
from v52
where bb = 0;
--解法二
create or replace view v53
as
select c.student_id
from T_teacher a, T_course b, T_score c
where a.t_id = b.t_id and b.course_id = c.course_id and a.t_name = '五木老师';

select student_id 学号
from T_student a
where student_id not in (select student_id from v53)
--解法三
create or replace view v53
as
select c.student_id
from T_teacher a, T_course b, T_score c
where a.t_id = b.t_id and b.course_id = c.course_id and a.t_name = '五木老师';

select student_id 学号
from T_student a
where not exists (select 1 from v53 where v53.student_id = a.student_id)

6、查询学过“101”并且也学过编号“102”课程的同学的学号、姓名

--方法一
select a.student_id 学号, c.student_name 姓名
from (select * from T_score where course_id = 101) a,
     (select * from T_score where course_id = 102) b, 
     T_student c
where a.student_id = b.student_id and b.student_id = c.student_id;
--方法二
select a.student_id 学号, b.student_name 姓名
from (select student_id from T_score where course_id = 101
      intersect
      select student_id from T_score where course_id = 102)a, T_student b
where a.student_id = b.student_id

7、查询学过“五木”老师所教的所有课的同学的学号、姓名

--v52请见第五题第二种解法的视图
select aa 学号, cc  姓名
from v52
where bb = 1;

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

select a.student_id 学号, c.student_name 姓名
from (select * from T_score where course_id = 101) a,
     (select * from T_score where course_id = 102) b, T_student c
where a.student_id = b.student_id and b.student_id = c.student_id and b.score < a.score

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

create or replace view v91
as
select student_id, 
       sum(case when score < 85 then 1 else 0 end) aa,
       count(1) bb
from T_score
group by student_id;

select b.student_id 学号,  b.student_name 姓名
from v91 a, T_student b
where a.student_id = b.student_id and a.aa = a.bb;

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

create or replace view v92
as
select student_id, count(1) aa
from T_score
group by student_id;

select b.student_id 学号,  b.student_name 姓名
from T_student b left join v92 a 
                 on a.student_id = b.student_id 
where nvl(a.aa, 0) != 5;

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

create or replace view v112
as
select a.student_id
from T_score a
where a.student_id != 1 
      and a.course_id in (select course_id from T_score where student_id = 1)
group by a.student_id;

select distinct a.student_id 学号, b.student_name 姓名
from v112 a, T_student b
where a.student_id = b.student_id;

12、查询至少学过学号为“001”同学所有课的其他同学学号和姓名

create or replace view v121
as
select count(1) aa from T_score where student_id = 1;

create or replace view v122
as    select student_id aa, 
          sum(case when course_id in (select course_id
                                      from T_score
                                      where student_id = 1) then 1 else 0 end) bb
from T_score
where student_id != 1
group by student_id;

select a.aa
from v122 a, v121 b
where bb = b.aa;

13、把“成绩”表中“五木”老师教的课的成绩都更改为此课程的平均成绩

create or replace view v131
as  select course_id
    from T_teacher a, t_course b
    where a.t_id = b.t_id and t_name = '五木老师'; 

create or replace view v132
as  select avg(score) bb
    from T_score a, v131 b
    where a.course_id = b.course_id;

update T_score a 
set score = (select * from v132)
where (select course_id
      from T_teacher a, t_course b
      where a.t_id = b.t_id and t_name = '五木老师') = a.course_id;

select * from T_score
where course_id = 102;

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

create or replace view v141
as  select student_id, course_id, score
    from T_score
    where student_id = 2;

create or replace view v142  
as  select * 
    from T_score
    where student_id != 2;

create or replace view v143  
as 
select
      student_id,
      sum(case when a.course_id in (select course_id
                                    from v141) then 1 else -1 end) aa
from v142 a
group by student_id;

select a.student_id 学号, b.student_name 姓名
from v143 a, t_Student b
where a.student_id = b.student_id 
      and a.aa = (select count(1) aa from v141);

15、删除学习“五木”老师课的SC表记录

create or replace view v151
as  select b.course_id
    from T_teacher a, T_course b
    where a.t_id = b.t_id and a.t_name = '五木老师';

delete from T_score
where course_id in (select course_id from v151);

select * from T_score;

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

create or replace view v161
as
select student_id, max(case when course_id = 103 then 1 else 0 end) aa
from T_score
group by student_id;

create or replace view v162
as
select b.student_id
from T_student b left join v161 a
                 on a.student_id = b.student_id 
where a.aa = 0 or a.aa is NULL;

create or replace view v163
as
select trunc(avg(score), 2) a
from T_score
where course_id = 102

insert into T_score  (select a.student_id, 103, b.a
                      from v162 a, v163 b)

17、按平均成绩从高到低显示所有学生的“数据库”、“Python”、两门的课程成绩,(数据库,Linux)按如下形式显示: 学生ID,数据库,Python,有效课程数,有效平均分

create or replace view v171
as   
select course_id
from t_course
where course_name = '数据库';

create or replace view v172
as
select course_id
from t_course
where course_name = 'linux';

select student_id 学号,
       sum(case when course_id = (select course_id from v171) 
                then score else NULL end) 数据库,
       sum(case when course_id = (select course_id from v172) 
                then score else NULL end) Linux,
       count(1) 有效课程数,
       avg(score) 有效平均分                                                                        
from T_score
where course_id in (select course_id
                    分析了上百份大厂面经,我总结了这些最最最常问的面试题,刷完直接进大厂!

刷完HashMap源码,我们一起进大厂

为了进大厂,我深扒了阿里字节等大厂面经,总结了50道必考题

2019大厂Java岗面试题全曝光,刷完这1020道,金三银四大厂等你

刷完500道BAT面试题,我能去面试大厂了吗?

硬核!刷完这份《Java面试突击宝典》,90%的程序员进了大厂!