The most comprehensive encyclopedia of MySQL interview questions - bis

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/nihui123/article/details/90605772

Continue on the content of the blog post, detailed analysis and query operations.

5. Query all students student number, student name, total enrollment, a total score of all the courses (results not shown as null)

Analysis: This question is to see, first of all need to be considered to get the student's name and student number must be in the student table, to obtain a total score must be in the results table so that it also involves the issue of multi-table queries, first of all let's do a simple look at the results of the query.

select s.sid,s.sname ,count(cid) as zs FROM Student as s, SC;

Will find that is not the result we want, here is the analysis of the problem.
First, let's look at the results of their separate
Here Insert Picture Description

Here Insert Picture Description
Can be seen from the above two pictures above, at the time of the query result is not equal two, but we want to do is to get the second result is divided according to the first results. So how should we write this statement does, that we want to use the first result of operating conditions.

select s.sid,s.sname,count(cid) as xkzs,sum(score) as zcj
from Student as s LEFT JOIN SC 
on s.sid = SC.sid
group BY s.sid;

Here Insert Picture Description

6. The number of conditional statistics teacher

Analysis: According to statistics the number of a teacher's last name, so that it comes to vague query, use fuzzy queries in MySQL mainly use% to match, for example, the following statement is to count the number of Zhang

select count(tname)FROM Teacher WHERE tname LIKE '张%'
The conditions of the query results table for all students.

Analysis: First, students need to query the information related to the problem is a multi-table queries, followed by the difference between the query and the previous query, the query is information that all students. So how to search.

Here to give a result

select s.* ,a.score as yuwen,b.score as shuxue,c.score as yingyu
from Student s,
	(SELECT sid ,score from SC WHERE cid=01)  a,
	(SELECT sid ,score from SC WHERE cid=02)  b,
	(SELECT sid ,score from SC WHERE cid=03)  c
WHERE s.sid = a.sid and s.sid = b.sid and s.sid = c.sid;

Here Insert Picture Description
You will find the above result is not what we want, so why such a result would happen then? We are the first to be analyzed, first of all, the idea is to give the students start with the table found in all student information, and then the results obtained from the subjects table to all corresponding results. Combining the two together. But we will find a problem, can meet the three conditions of the data in the table only the first four grades, the following data is not the fourth condition. And with that analysis, the results were the following changes

select s.* ,a.score as yuwen,b.score as shuxue,c.score as yingyu
from Student s,
	(SELECT sid ,score from SC WHERE cid=01)  a,
	(SELECT sid ,score from SC WHERE cid=02)  b,
	(SELECT sid ,score from SC WHERE cid=03)  c
WHERE s.sid = a.sid and s.sid = b.sid or s.sid = c.sid;

But this result is unreasonable, so the use of a clever way. as follows

select s.sid,s.sname,count(cid) as xkzs,sum(score) as zcj,
  SUM(case WHEN cid = 01 THEN score else null end) as yuwen,
  SUM(case WHEN cid = 02 THEN score else null end) as shuxue,
  SUM(case WHEN cid = 03 THEN score else null end) as yingyu
from Student as s ,SC 
WHERE s.sid = SC.sid
group BY s.sid;

Here Insert Picture Description
Based on the results above, we find that in fact three of the latter part of the English is the language of mathematics courses each have a no results, so if you use the first method above, or the second way are in fact takes too much effort to complete .

8. query learned a student teacher program information.

Analysis: First up, we know that you want to query the information available through a student, you must first find the table to SC students have grades, get into the curriculum and student id cid corresponding id sid. So the question is, this is a multi-table query it? Or multi-table subqueries it? The need to analyze, for example, we need to find the students went to Joe Smith's class teacher too, and find the corresponding sid cid in the SC, and then find the corresponding sid corresponding students in the student table. After analyzing the table structure found in fact corresponds to a table SC is not directly Tid. The first joint SC table, Teacher Course table and the table to find Sid.

select sid FROM SC,Course,Teacher
WHERE SC.cid = Course.cid
AND Course.tid = Teacher.tid
AND Teacher.tname='张三'

Here Insert Picture Description
Joe Smith to see a lot of lessons the students actually. After finding student id is required to table the students to find these students fall within the scope of the. Mentioned before, SQL is used in a range indicated in three ways

  1. Use inequality judge
  2. Use between and
  3. Use keywords in
SELECT * from Student WHERE sid in(
	select sid FROM SC,Course,Teacher
	WHERE SC.cid = Course.cid
	AND Course.tid = Teacher.tid
	AND Teacher.tname='张三'
)

Here Insert Picture Description

9. query selects all courses of student information.

Analysis: Firstly select all the courses students can by sid. SC to get the table, then how to select students for all courses is counted it? Encountered in the previous example of a query for all grades, can we learn from the example of it? The answer is not, first, you need to know all the courses students choose those, followed by a group of students from the sid, then ask what you want to group it, first of all we know sid grouped according to those students who can know is to choose three courses, and the statistics given here to the first statement.

select sid, count(sid) from SC  GROUP BY sid 

Here Insert Picture Description
By results of further analysis, if this result as a temporary table for further inquiries should be how to do it?

select * from Student as s,
	(select sid, count(sid) as cnt from SC  GROUP BY sid) a
 WHERE s.sid = a.sid AND a.cnt = 3;

The above statement is obviously to achieve a function. So there are no more statements to optimize it? Here introduce a keyword having, Why Having, Having and Where in the end what is the difference.
Queries do not select students for all courses

select s.* from Student as s,
	(select sid, count(sid) as cnt from SC  GROUP BY sid) a
 WHERE s.sid = a.sid AND a.cnt != 3;
 
Having 和Where

Where first key is used for filtering conditions for selecting matching records. But can in fact use where and under certain conditions Having obtained the same results, for example,

select sid,count(1) from SC
GROUP BY sid
HAVING sid = 01;
select sid,count(1) from SC 
where sid = 01;
GROUP BY sid

First, analyze the difference in what place

  1. Where sub-statement is used to specify the condition "line", and Having a sub statement is a condition for the specified group.
  2. When the specified conditions using Where the child statement due to the data filtering Bureau before sorting, so reducing the amount of data sorted, but Having clause will group the data after sorting.
  3. Where the use of higher speed sub statement is because Where the conditions specified in sub-statements can be used as an index.
  4. where clause aggregate functions can not be used, and may be used in Having clause.

to sum up

In time before the interview the interviewer asked why not use Having statements, and now for Having conducted an in-depth understanding. It shows a combination of several criteria query and sub-queries. Also provides a new way to statistics each column is empty.

Guess you like

Origin blog.csdn.net/nihui123/article/details/90605772