数据库经典笔试题

发布于:2022-12-16 ⋅ 阅读:(555) ⋅ 点赞:(0)

一、分享背景:        

        由于最近测试这个行业,内卷的非常严重,不得不再得好好的学习一下,测试周边的一些知识,今天为大家整理一些数据库经典的笔试题,可以好好的备战一下测试在数据库方面的知识。

二、分享内容:

1.1 本题目的表结构
Student(S#,Sname,Sage,Ssex)学生表
Course(C#,Cname,T#)课程表
SC(S#,C#,score)成绩表
Teacher(T#,Tname)教师表
1.2本题目的建表及测试数据
1建表
create table Student(
   S# int,
   Sname varchar(32),
   Sage int,
   Ssex varchar(8)
)
create table Course(
   C# int ,
   Cname varchar(32),
   T# int 
)
create table Sc(
   S# int ,
   C# int ,
   score int ,
)
create table Teacher(
    T# int ,
    Tname varchar(32)
)    
(1)查询“001”课程比002课程成绩高的所有学生的学号;

select a.S# from 2 (select S#,Score from SC where C#='001') a, 3 (select S#,Score 
from SC where C#='002') b 4 where a.S#=b.S# and a.Score>b.Score 
  
(2) 查询平均成绩大于60分的同学的学号和平均成绩; 

 1 select S#,AVG(Score) as 
AvgScore  2 from SC 3 group by S# 4 having AVG(Score)>60   

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

 1 select s.S#,s.Sname,COUNT(sc.C#) as CourseCount,SUM(sc.Score) as ScoreSum 2 from 
Student s left outer join SC sc 3 on s.S# = sc.S# 4 group by s.S#,s.Sname 5 
order by s.S#   

(4)查询姓“李”的老师的个数; 

 1 select COUNT(distinct Tname) as 
count 2 from Teacher 3 where Tname like '李%'   

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

1 select s.S#,s.Sname 2 from 
Student s 3 where s.S# not in 4 ( 5     select distinct(sc.S#) from SC sc,Course 
c,Teacher t 6     where sc.C#=c.C# and c.T#=t.T# and t.Tname='叶平' 7 )
   
(6)查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;  

1 --解法一:求交集  2 select s.S#,s.Sname  3 from Student s,SC sc  4 where 
s.S#=sc.S# and sc.C#='001'  5 intersect  6 select s.S#,s.Sname  7 from Student 
s,SC sc  8 where s.S#=sc.S# and sc.C#='002'  9 
--解法二:使用exists 10 select 
s.S#,s.Sname 11 from Student s,SC sc 12 where s.S#=sc.S# and sc.C#='001' and 
exists 13 ( 14     select * from SC sc2 where sc.S#=sc2.S# and sc2.C#='002' 15 )
   
PS: 
EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是
返回值True或False。那么, 这里我们来看一下 in和exists的区别 :  ①in 
是把外表和内表作hash 
连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询。  ②一直以来认为 
exists比in效率高的说法是不准确的 。
  
-->如果查询的两个表大小相当,那么用in和exists差别不大。  
-->如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in。  

 三、拓展内容,下边有更详细的经典数据库笔试题和面试题

(1条消息) 经典的数据库笔试题你值得拥有-其它文档类资源-CSDN文库icon-default.png?t=M85Bhttps://download.csdn.net/download/m0_49428126/86540014

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到