50 classic sql query topic and ideas (a)

Because of the need to improve the bit sql query capabilities, of course, the quickest way is to do some actual topic. Select the 50 questions the sql, this time probably did that for about the first 10 questions, to impress the idea, but also a summary.

Specific topics, see:

https://zhuanlan.zhihu.com/p/72223558

 

The first part of the main technical problems are even using table queries and sub-queries, stumped is not difficult, the main idea is to make the conversion over.

First, draw a relationship between the respective tables (relations not always confused before drawing)

 

 

 1. Query "01" than "02" high course grade student information and course grade curriculum

Information among students in Table 1, course grades in Table 4 which, of course, to use even the table query.

Here's a very universal problem: from Table 4 to find the same Sid, but Cid Cid to score 1 is greater than the record score of 2 . To use the subqueries herein, define the conditions for Cid = '1', Cid = '2', into two tables, and then check the condition is very simple.

the SELECT Student.SId, Student.Sname, Student.Sage, Student.Ssex, r. subject a result, r. two subjects score
 from study.dbo.Student 
 right  the Join 
( the SELECT t1.SId AS student ID, t1.score AS subjects a score, t2.score AS two subjects score
 from  
( SELECT the SId, score from study.dbo.SC WHERE Cld = ' 01 ' ) AS T1,
(select SId,score from study.dbo.SC where CId='02')as t2
where t1.SId=t2.SId and t1.score>t2.score) as r
on Student.SId=r.学生ID

 

join - on this train of thought is also commonly used when you want to connect a column of two related tables.

 

1.1 Query existence of "01" may not be the case of course, but the course of "02" exists (when displayed as null does not exist)

And the first question is similar ideas, pay attention to the 01 courses prevail, so use left join

select *
from    
(select SId,score from study.dbo.SC where CId='01') as t1
left join
(select SId,score from study.dbo.SC where CId='02') as t2
on t1.SId=t2.SId

 


1.2 queries the presence of 01 and 02 courses at the same time

Very simple, with inner join, find the intersection of two tables

select t1.SId,t1.score,t2.score
from    
(select SId,score from study.dbo.SC where CId='01') as t1
inner join
(select SId,score from study.dbo.SC where CId='02') as t2
on t1.SId=t2.SId


1.3 query selects the 02 courses 01 courses but no case

My thinking is still with a right join, and then determine the NULL value, do not know will not be higher than not in efficiency .

select t2.SId,t2.score
from    
(select SId,score from study.dbo.SC where CId='01') as t1
right join
(select SId,score from study.dbo.SC where CId='02') as t2
on t1.SId=t2.SId
where t1.score is null

 

2. Query average score of 60 points or greater number of students and the student's name and student grade point average

Will certainly be even table, there is a table with Table IV. The average scores related to the group by, restrictions on the average scores of statements involve having

select  t1.SId,t1.avg_score,t2.Sname
from
(
select SId,AVG(score) as avg_score
from study.dbo.SC
group by SId
having AVG(score)>60
) as t1
inner join study.dbo.Student as t2
on t1.SId=t2.SId

 

3. Query student achievement information exists in the SC table
remains even table query, a table of four sid sid equal to the table, removing duplicates can use DISTINCT

select DISTINCT Student.SId,Student.Sname,Student.Sage,Student.Ssex
from study.dbo.SC
inner join Student
on SC.SId=Student.SId

 

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

Even still table query, left join

select Student.*,t2.count_id,t2.avg_score
from
Student
left join
(select SId,count(CId) as count_id ,avg(score)as avg_score from study.dbo.SC group by SId) as t2
on Student.SId=t2.SId

 

4.1 There are scores of student information check

inner join, do not repeat them

select Student.*,t2.count_id,t2.avg_score
from
Student
inner join
(select SId,count(CId) as count_id ,avg(score)as avg_score from study.dbo.SC group by SId) as t2
on Student.SId=t2.SId

 

5. The number of inquiries "Lee" teacher surnamed

The simplest one question, know like this fuzzy queries on the line

select COUNT(*)
from Teacher
where Tname like '李%'

 

6. Information inquiry learned "John Doe" of teachers to teach students

This is interesting, on behalf of the jump from one table to another table to find information

The first idea is of course join, the plurality of tables are connected on a one

select Student.*
from
(select tid from Teacher where Tname='张三') as t1
inner join Course on t1.tid=Course.TId
inner join SC on Course.CId=SC.CId
inner join Student on SC.SId=Student.SId

But there is another way to write

select study.dbo.Student.*
from teacher,study.dbo.Course  ,study.dbo.student,study.dbo.sc
where teacher.Tname='张三' and   teacher.TId=Course.TId and   Course.CId=sc.CId and   sc.SId=student.SId

Direct from multiple tables, the where to write =

I checked, in fact, in this way is to use the implicit inner join, little difference in efficiency

 

 

 


7. Information inquiry did not learn the whole course of all students

I found not studied the whole course all students sid very simple query in Table 4. 1 student information inquiry implement contingency tables with inner join.

SELECT *
FROM study.dbo.Student as t1
inner join
(select Student.SId from Student
left join
study.dbo.SC
on Student.SId=SC.SId
group by Student.SId
having COUNT(SC.CId)!=(select count(*) from study.dbo.Course)) as t2
on t1.SId=t2.SId

 

Guess you like

Origin www.cnblogs.com/take-it-easy/p/11775991.html