Home > Database > Mysql Tutorial > Database query questions that appear frequently in Java interviews

Database query questions that appear frequently in Java interviews

little bottle
Release: 2019-04-04 18:02:47
Original
3025 people have browsed it

Programmers who have experienced interviews all know that during the interview process, the interviewer may ask you all kinds of strange questions, but they always remain the same. In the end, you have to ask the key points, such as The basic operation steps of a certain job and how to write the code, etc., this article talks about the most classic database query problems.

Database query questions that appear frequently in Java interviews

Basic table structure:

teacher(tno,tname) teacher table

student(sno,sname,sage, ssex) student table

course(cno,cname,tno) course table

sc(sno,cno,score) grade table

NO.1 query course The student numbers of all students whose grades in 1 are higher than those in course 2

select a.sno from(select sno,score from sc where cno=1) a,(select sno,score from sc where cno=2) bwhere a.score>b.score and a.sno=b.sno
Copy after login

NO.2 Query the student numbers and average grades of students whose average grades are greater than 60 points

select a.sno as "学号", avg(a.score) as "平均成绩" from(select sno,score from sc) a group by sno having avg(a.score)>60
Copy after login

NO.2 Query All students’ student numbers, names, number of courses taken, and total grades

select a.sno as 学号, b.sname as 姓名,count(a.cno) as 选课数, sum(a.score) as 总成绩from sc a, student bwhere a.sno = b.snogroup by a.sno, b.sname
Copy after login

or:

selectstudent.sno as 学号, student.sname as 姓名, count(sc.cno) as 选课数, sum(score) as 总成绩from student left Outer join sc on student.sno = sc.snogroup by student.sno, sname
Copy after login

NO.3 Query the number of teachers with the surname “Zhang”

selectcount(distinct(tname)) from teacher where tname like '张%‘
Copy after login

or :

select tname as "姓名", count(distinct(tname)) as "人数" from teacher where tname like'张%'group by tname
Copy after login

NO.4 Query the student number and name of the students who have not studied "Zhang San"'s class

select student.sno,student.sname from student
where sno not in (select distinct(sc.sno) from sc,course,teacher
where sc.cno=course.cno and teacher.tno=course.tno and teacher.tname='张三')
Copy after login

NO.5 Query the students who have studied both course 1 and course 2 Student ID, name

select sno, sname from studentwhere sno in (select sno from sc where sc.cno = 1)and sno in (select sno from sc where sc.cno = 2)
Copy after login

or:

selectc.sno, c.sname from(select sno from sc where sc.cno = 1) a,(select sno from sc where sc.cno = 2) b,student cwhere a.sno = b.sno and a.sno = c.sno
Copy after login

or:

select student.sno,student.sname from student,sc where student.sno=sc.sno and sc.cno=1and exists( select * from sc as sc_2 where sc_2.sno=sc.sno and sc_2.cno=2)
Copy after login

NO.6 Query the student IDs of all students who have studied all courses taught by "Li Si" , name

select a.sno, a.sname from student a, sc bwhere a.sno = b.sno and b.cno in(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '李四')
Copy after login

or:

select a.sno, a.sname from student a, sc b,(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '李四') ewhere a.sno = b.sno and b.cno = e.cno
Copy after login

NO.7 Query the student numbers and names of all students whose grades in course number 1 are higher than those in course number 2

<p style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif; white-space: normal;">select a.sno, a.sname from student a,</p><p style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif; white-space: normal;">(select sno, score from sc where cno = 1) b,</p><p style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif; white-space: normal;">(select sno, score from sc where cno = 2) c</p><p style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif; white-space: normal;">where b.score > c.score and b.sno = c.sno and a.sno = b.sno<br/></p>
Copy after login

NO.8 Query the student numbers and names of all students whose grades are less than 60 points

select sno,sname from studentwhere sno not in (select distinct sno from sc where score > 60)
Copy after login

NO.9 Query the courses taken by students with at least one course and student number 1 The student number and name of the same classmate

select distinct a.sno, a.snamefrom student a, sc bwhere a.sno <> 1 and a.sno=b.sno andb.cno in (select cno from sc where sno = 1)
Copy after login

Or:

select s.sno,s.sname from student s,(select sc.sno from scwhere sc.cno in (select sc1.cno from sc sc1 where sc1.sno=1)and sc.sno<>1group by sc.sno)r1where r1.sno=s.sno
Copy after login

The above are the questions you may often encounter in interviews for database-related jobs, hurry up and collect them Get up and take a good look!

【Recommended course: MYSQL learning video

The above is the detailed content of Database query questions that appear frequently in Java interviews. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template