SQL Server-parallel query, cross query, difference query

table of Contents

 

And query

Hand in query

Bad query


        student table  

                             sc table     

 

 

And query

                                                             

What is and query?

Combine the results of the two select statements

As shown in the figure, combine the queried A result set and B result set together

 

Code:

select a.CId,a.score ,b.Sname           --找出指定学生
from sc a, student b
where a.SId =b.SId and CId = 01

union

select ' ',sum(score),'合计'               --成绩总数
from sc a, student b
where a.SId =b.SId and CId = 01

Results of the:

                  

 


Hand in query

                                                          

What is an inquiry?

Combine the result sets of the two select statements and find the parts they have in common

As shown in the figure, get result set A and result set B, and ask for the result set C that they have together

Code:

select a.CId ,a.score ,b.Sname  from sc a ,student b            --查询课程为801,考试为01的学生
where a.SId =b.SId and a.CId ='01' and a.课程编号 ='801'

intersect

select top 10 a.CId ,a.score ,b.Sname  from  sc a, student b    --查询课程为801,成绩前10的学生
where a.SId =b.SId  and a.课程编号 ='801'  order by a.score desc

Results of the:

                    

 


Bad query

What part does the difference query look for?

Remove the place where the two result sets do not want to intersect

Code:

select a.CId ,a.score ,b.Sname  from sc a ,student b            --查询考试为01的学生
where a.SId =b.SId and a.CId ='01' 

except

select top 15 a.CId ,a.score ,b.Sname  from  sc a, student b    --查询课程为801,成绩前10的学生
where a.SId =b.SId  and a.课程编号 ='801'  order by a.score desc

Results of the:

                     

 

 

 

Guess you like

Origin blog.csdn.net/weixin_43319713/article/details/106084592