SQL Left, Right Join, and other operators.
1. query all the data in tableA and tableB and the data in tableA in tableB.
Select * from tableA A left join tableB B on A. key = B. key
2. query and exclude the data in tableB and the remaining data in tableA.
Select * from tableA A left join tableB B on A. key = B. key where B. key is null
3. query all data in tableA and tableB
Select * from tableA A full outer join tableB B on A. key = B. key
4. query data out of table A and Table B
Select * from tableA A full outer join tableB B on A. key = B. key where A. key is null or B. key is null
5. query the data in Table A and table B.
Select * from tableA A inner join tableB B on A. key = B. key
Briefly describes the functions of inner join, left join, and right join in SQL.
In fact, you are not allowed to directly write an example to show it to you. Maybe you can understand it faster.
Create two tables A and B. Table A contains A field Aid. Table B contains two fields Aid and Bid.
Create table A (aid int)
Create table B (aid int, bid int)
Insert data separately
Insert 1 to the aid field of table A and 2 to the aid field of Table.
Insert 1 to the aid field of Table B, and 2 to the bid Field
Insert 3 to the aid field of Table B, and 4 to the bid Field
Insert into a (aid) values (1)
Insert into a (aid) values (2)
Insert into B (aid, bid) values (1, 2)
Insert into c (aid, bid) values (3, 4)
Use inner join, left jion, and right join to join each other and you will see their functions and differences.
Select a. aid, B. aid, B. bid from a inner join B on a. aid = B. aid
Result 1, 2 is obtained.
Select a. aid, B. aid, B. bid from a left join B on a. aid = B. aid
Result
1, 1, 2
2 ,,
Select a. aid, B. aid, B. bid from a right join B on a. aid = B. aid
Result
1, 1, 2
, 3, 4
That is to say,
The inner join operator obtains the matching values of the equal signs of the correlated FIELD on a. aid = B. aid.
Left join is the value that matches the left of the associated field on a. aid = B. aid. If there is a value on the left that does not match the right, fill it with null.
Right join is the value that matches the right of the associated field on a. aid = B. aid. Similarly, if there is a value on the right and there is no matching on the left, it is also filled with null values.
What are the differences between join, left join, and right join in SQL statements?
Left join: returns all records in the left table and records with the same connection fields in the right table.
Right join: right join. All records in the right table and records with the same connection fields in the left table are returned.
Inner join: An inner join, also called an equivalent join, returns only rows with the same connection fields in two tables.
Full join: Outer join. the rows in the two tables are returned: left join + right join.
Cross join: The result is a Cartesian product, that is, the number of rows in the first table multiplied by the number of rows in the second table.
Declare @ a table (a int, B int)
Declare @ B table (a int, B int)
Insert @ a values (1, 1)
Insert @ a values (2, 2)
Insert @ B values (1, 1)
Insert @ B values (3, 3)
Select * from @
Select * from @ B
-- Left:
Select * from @ a Aa left join @ B Bb on Aa. a = Bb.
-- Right:
Select * from @ a Aa right join @ B Bb on Aa. a = Bb.
-- Intranet
Select * from @ a Aa inner join @ B Bb on Aa. a = Bb.
-- Outer:
Select * from @ a Aa full join @ B Bb on Aa. a = Bb.
-- Cross join
Select * from @ a cross join @ B