当前位置:网站首页>Database interview questions + analysis
Database interview questions + analysis
2022-07-07 23:48:00 【Scorpio programmer】
Here are four tables ,1. Student list -t_student,2. Teachers list -t_teacher,3. The curriculum -t_course, 4. League tables -t_score
Here is the table design :
-- 1. Student list -t_student
-- sid Student number ,sname The student's name ,sage Student age ,ssex Student gender
create table t_student(
sid varchar(10) primary key comment ' Student number ',
sname varchar(20) not null comment ' The student's name ',
sage varchar(10) not null comment ' Student age ',
sex varchar(20) not null comment ' Student gender '
)comment ' Student information sheet ';
-- 2. Teachers list -t_teacher
-- tid Teacher number ,tname Teacher's name
create table t_teacher(
tid varchar(10) primary key comment ' Teacher number ',
tname varchar(20) not null comment ' Teacher's name '
)comment ' Teacher information sheet ';
-- 3. The curriculum -t_course
-- cid Course number ,cname Course name ,tid Teacher's name
create table t_course(
cid varchar(10) primary key comment ' Course number ',
cname varchar(20) not null comment ' Course name ',
tid varchar(10) not null comment ' Teacher's name ',
foreign key(tid) references t_teacher(tid)
)comment ' Course information sheet ';
-- 4. League tables -t_score
-- sid Student number ,cid Course number ,score achievement
create table t_score(
sid varchar(10) not null comment ' Student number ',
cid varchar(10) not null comment ' Course number ',
score float default 0 comment ' achievement ',
foreign key(sid) references t_student(sid),
foreign key(cid) references t_course(cid)
)comment ' Grade information sheet ';
select * from t_student;
select * from t_teacher;
select * from t_course;
select * from t_score;
Here are the interview questions + The result of the problem solving ( This is not the only way to solve problems , I wrote but only one of them )
#01) Inquire about " 01 " Course than " 02 " Information and course marks for students with high course grades
select s.*,temp.s1,temp.s2 from(
select t1.sid,t1.s1,t2.s2 from
(select sid,score as s1 from t_score where cid='01')t1,
(select sid,score as s2 from t_score where cid='02')t2
where t1.sid=t2.sid and t1.s1>t2.s2)temp,t_student s
where temp.sid=s.sid#02) Queries exist at the same time " 01 " Courses and " 02 " Course situation
select* from
(select sid,score as s1 from t_score where cid='01') t1,
(select sid,score as s2 from t_score where cid='02') t2
where t1.sid=t2.sid;#03) Query exists " 01 " The course may not exist " 02 " Course situation ( If it does not exist, it will be displayed as null )
select * from
(select sid,score from t_score where cid='01')t1 left join
(select sid,score from t_score where cid='02')t2 on t1.sid=t2.sid
#04) Query does not exist " 01 " Course but there is " 02 " Course situation
select * from t_score where sid not in (
select sid from t_score where cid='01') and cid='02';
select * from t_score where sid='07';#05) Check the grade average ( grouping group by, Screening :having avg) Greater than or equal to 60 Student number, student name and average score of each student
select s.sid,s.sname,
round(avg(sc.score),2) score
from
t_student s,t_score sc
where s.sid=sc.sid
group by s.sid,s.sname
having avg(sc.score)>=60;#06) The query in t_score There is student information about the scores in the table
select distinct s.*
from
t_score sc,t_student s where sc.sid=s.sid#07) Query student numbers of all students 、 The student's name 、 The total number of selected courses 、 Total grade of all courses ( No results are shown as null )
select s.sid,s.sname, count(sc.cid) cn,
sum(sc.cid) sm
from
t_student s left join t_score sc on sc.sid=s.sid
group by s.sid,s.sname
#08) Inquire about 「 Li 」 The number of teachers surnamed
select count(tid) from t_teacher where tname like ' Li %';
#09) I have learned to query 「 Zhang San 」 Information of students taught by teachers
select
s.*
from t_teacher t,
t_course c,
t_score sc,
t_student s
where
t.tid=c.tid and
c.cid=sc.cid and
sc.sid=s.sid and t.tname=' Zhang San '#10) Check the information of students who have not learned all the courses
select * from t_student where sid not in (
select sid from t_score
group by sid having count(cid)=(select count(cid) from t_course))
#11) I didn't learn how to query " Zhang San " The name of the student in any course taught by the teacher
select * from t_student where sid not in(
select sc.sid from t_teacher t,t_course c,t_score sc where
t.tid=c.tid and t.tname=' Zhang San ' and
c.cid=sc.cid)
#12) Check the student number of two or more failed courses , Name and average score
select
s.sid,
s.sname,
round(avg(sc.score),2) sc
from t_score sc,t_student s
where sc.sid=s.sid and sc.score<60
group by s.sid,s.sname
having count(sc.cid)>1#13) retrieval " 01 " The course score is less than 60, Student information in descending order of scores
select *
from t_student a,
t_score b
where a.sid = b.sid
and b.cid = '01'
and score < 60
order by score desc;#14) Show the grades of all courses and the average grades of all students from high to low
select sc.sid,
round(avg(sc.score),2) as ' Average score ',
sum(if(sc.cid='01',sc.score,0)) as ' Chinese language and literature ',
sum(if(sc.cid='02',sc.score,0)) as ' mathematics ',
sum(if(sc.cid='03',sc.score,0)) as ' English '
from
t_score sc
group by sc.sid,sc.cid
order by avg(sc.score) desc#15) Check the highest scores of all subjects 、 Minimum and average points : Show as follows : Course ID, Course name, The highest , Lowest score , average , pass rate , Medium rate , Excellent rate , The pass rate of excellence is >=60, The average is :70-80, Good is :80-90, Excellence is :>=90 Require output of course number and number of electives , The query results are arranged in descending order of the number of people , If the number of people is the same , In ascending order of course number
select
c.cid,
c.cname,
count(c.cid) as ' Number of electives ',
max(sc.score) as ' The highest ',
min(sc.score) as ' Lowest score ',
round(avg(sc.score),2) as ' average ',
concat(round(sum(if(sc.score>=60,1,0))/count(s.sid)*100,2),'%') as ' pass rate ',
concat(round(sum(if(sc.score>=70 and sc.score<80,1,0))/count(s.sid)*100,2),'%') as ' Medium rate ',
concat(round(sum(if(sc.score>=80 and sc.score<90,1,0))/count(s.sid)*100,2),'%') as ' Excellent rate ',
concat(round(sum(if(sc.score>=90,1,0))/count(s.sid)*100,2),'%') as ' Excellence rate '
from
t_score sc,t_course c,t_student s
where
sc.cid=c.cid and sc.sid=s.sid
group by c.cid,c.cnameThis is today's sharing , When the interviewer gives you a question , There is no computer in front of you , You need to conceive your watch design in your mind . This is a big problem in the interview , Need enough space to imagine .good luck!
边栏推荐
- The for loop realizes 1-100 addition and eliminates the 4-digit tail number
- P2141 [noip2014 popularization group] abacus mental arithmetic test
- Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function
- Gorm Association summary
- Chisel tutorial - 03 Combinatorial logic in chisel (chisel3 cheat sheet is attached at the end)
- Resolve the URL of token
- Traduction gratuite en un clic de plus de 300 pages de documents PDF
- Boost regex library source code compilation
- 数据库面试题+解析
- SAP HR family member information
猜你喜欢

Take you hand in hand to build Eureka client with idea

Archery installation test

95. (cesium chapter) cesium dynamic monomer-3d building (building)

一个测试工程师的7年感悟 ---- 致在一路独行的你(别放弃)

Restricted linear table

95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)

激光slam学习(2D/3D、偏实践)

At the age of 35, I made a decision to face unemployment

神奇快速幂

Dataguard 主备清理归档设置
随机推荐
企业应用需求导向开发之人力部门,员工考勤记录和实发工资业务程序案例
Aitm3.0005 smoke toxicity test
Wechat applet development beginner 1
Data analysis series 3 σ Rule / eliminate outliers according to laida criterion
Enterprise application demand-oriented development of human resources department, employee attendance records and paid wages business process cases
2022.7.7-----leetcode.648
@Detailed introduction of configuration annotation
One click free translation of more than 300 pages of PDF documents
ping报错:未知的名称或服务
Magic fast power
机器人(自动化)等专业课程创新的结果
Open source hardware small project: anxinco esp-c3f control ws2812
codeforces每日5题(均1500)-第八天
AWS AWS help error
蓝桥ROS中使用fishros一键安装
One of the anti climbing methods
通达信买基金安全吗?
Possible SQL for Oracle table lookup information
C number of words, plus ¥, longest word, average value
At the age of 35, I made a decision to face unemployment