当前位置:网站首页>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.cname

This 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!

原网站

版权声明
本文为[Scorpio programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207072108575067.html