I know about joins in SQL Server.

For example. There are two tables Table1, Table2.

Their table structures are the following.

create table Table1 (id int, Name varchar (10))

create table Table2 (id int, Name varchar (10))

Table1 data as follows:

    Id     Name     
    -------------
    1      A        
    2      B    

Table2 data as follows:

    Id     Name     
    -------------
    1      A        
    2      B 
    3      C

If I execute both below mentioned SQL statements, both outputs will be the same

select *
from Table1
  left join Table2 on Table1.id = Table2.id

select *
from Table2
  right join Table1 on Table1.id = Table2.id

Please explain the difference between left and right join in the above SQL statements.

推荐答案

Select * from Table1 left join Table2 ...

and

Select * from Table2 right join Table1 ...

are indeed completely interchangeable. Try however Table2 left join Table1 (or its identical pair, Table1 right join Table2) to see a difference. This query should give you more rows, since Table2 contains a row with an id which is not present in Table1.

Sql相关问答推荐

为什么在postgres中,横向连接比相关子查询快?

如何优化我的功能以减少花费的时间?

重新组合已排序的日期范围

我可以在SQL的IN子句中使用比子查询包含的值更少的值吗?

导出部分条形码字符串GS1-128

返回UPSERT中的旧行值

冲突查询的UPDATE时违反非空约束

在MS Access Modern图表的X轴上显示时间值时遇到问题

Oracle 23c ROUND,数据类型为DATE

Select 列组(按同一表格中的另一列分组)Laravel 10

在SELECT中将日期格式转换为双周时段

显示十进制列,但尽可能显示为整数

使用 Oracle SQL Developer 将不同的列值转换为列会导致错误 ORA-01489

获取多个开始-结束时间戳集之间经过的时间

SQL ORACLE - 查找连续天数

达到特定值时,从0开始累加求和

Postgresql 需要一个查询,为我提供所有没有具有特定状态值的子元素的父母

查找具有相同连接列数据的所有记录

PostgreSQL 如何在一组项目及其数量上找到完全相同的订单?

SQL:有没有办法根据另一列的数据细节过滤和形成另一列?