当前位置:网站首页>SQL Server 2016 learning records - connection query
SQL Server 2016 learning records - connection query
2022-07-28 10:23:00 【Hard work Zhang Zhang】
Learning goals :
SELECT — Link query
Link query : Queries involving multiple tables at the same time
The condition used to join two tables becomes a join condition or join predicate
sql The main types of join queries in :
- (1) Cross connect ( The generalized Cartesian product )
- (2) Equivalent connection
- (3) Self connect
- (4) Composite conditional connection
- (5) Internal connection
- (6) External connection
Learning content :
sql The main types of join queries in :
(1) Cross connect ( The generalized Cartesian product )
Connection without predicate , Rarely used
SELECT student.*,sc.* FROM student,sc
SELECT student.*,sc.* FROM student cross join sc

Many are meaningless , So I seldom use
(2) Equivalent connection
The connection operator is = The connection of
-- Inquire about each student and their elective courses
select student.*,sc.* from student,sc where student.sno=sc.sno;

-- Check the student number of computer major students , full name , major , Elective course number , Course name , And achievements
select sc.sno,sname,sdept,sc.cno,cname,grade
from student,sc,course
where student.sno=sc.sno and course.cno=sc.cno
and sdept='CS';

(3) Self connect
Representation :
- You need to alias the table to show the difference
- Because all attribute names have the same name , So you have to use the alias prefix
-- Query the course name of the direct prerequisite course of each course
select a1.cname,a2.cname from course a1,course a2 where a1.cpno=a2.cno;

-- Query the course number of the indirect prerequisite course of each course ( That is, the prerequisite course of the pre course )
select a1.cno,a2.cno from course a1,course a2 where a1.cpno=a2.cpno;

-- Query at the same time 2 Course No. and 3 The student number of course No
select a1.sno from sc a1,sc a2 where a1.sno=a2.sno and a1.cno=2 and a2.cno=3;

- (4) Composite conditional connection
where Clause contains multiple join conditions , It is called qualified connection
classification :
The two tables are connected by multiple attributes
Connect itself by multiple attributes
Multiple table joins
-- Check options 1 No. and the result is 90 Names of students with scores above , Gender and department
select sname,ssex,sdept from student,sc
where student.Sno=sc.Sno /* Join predicate */
and cno=1
and grade>90;

-- Check the student number of each student 、 full name 、 The name of the elective course is the grade
select student.sno,sname,cname,grade from student,sc,course
where student.sno=sc.sno
and sc.cno=course.cno;

- (5) Internal connection
inner join perhaps join
Inner connection is actually equivalent connection , He USES “=、>、<、<>” The equal operator matches the row query statements in the two tables according to the values of the columns common to each table
-- Query all student information and elective courses
select student.*,sc.* from student join sc
on student.sno=sc.sno;

- (6) External connection
Left connection (left join) Or left outer connection (left outer join)
The right connection (right join) Or right outer connection (right outer join)
Full connection (full join) Or all external connections (full outer join)
-- All information of all students and their course selection records
select student.*,sc.* from student left join sc
on student.sno=sc.sno;

-- Course selection records and corresponding course information of all students
select sc.*,course.* from sc right join course
on sc.cno=course.cno;

select sc.*,course.* from sc full join course
on sc.cno=course.cno;
// Returns all rows in the left and right tables , When a row has no matching row in another table , Return null value

边栏推荐
猜你喜欢

初识SuperMap iDesktop

Aqua Data Studio 18.5.0 export insert statement

15. Judge whether the target value exists in the two-dimensional array

14、双指针——盛最多水的容器

语音聊天app——如何规范开发流程?

Get to know SuperMap idesktop for the first time

7. Dichotomy -- find a set of repeated or ordered but rotating arrays

Record a parent-child project in idea, modify the name of project and module, and test it personally!

利用正则表达式从文件路径中匹配文件名

Typora tutorial
随机推荐
B2B e-commerce website scheme for building materials industry: enable the transformation and upgrading of building materials enterprises to achieve cost reduction and efficiency improvement
Pl/sql server syntax explanation
SQL Server 2016 学习记录 ---视图
Talk about the problem of preventing others from debugging websites through console based on JS implementation
Go json.Decoder Considered Harmful
配置树莓派,过程和遇到问题
Digital transformation scheme of real estate: all-round digital intelligence system operation, helping real estate enterprises improve the effectiveness of management and control
uni-app进阶之创建组件/原生渲染
Summary of key points of bank entry examination
阿里云镜像地址
[wechat applet] project practice - lottery application
吴雄昂遭Arm罢免内幕:建私人投资公司,损害了股东利益?
初识SuperMap iDesktop
Performance test of API gateway APIs IX in Google cloud T2a and T2D
gcc: error trying to exec 'as': execvp: No such file or directory
中兴通讯总裁徐子阳:5nm芯片将在2021年推出
MySQL的SQL TRACE一例
Database mysql Foundation
Context values traps and how to avoid or mitigate these traps in go
【云驻共创】华为云:MetaStudio数字内容生产线,让虚拟世界与现实世界无缝融合