The difference between left join and right join in SQL and Inner join vs. Full join

Source: Internet
Author: User

Let's start by looking at the results of the left join and the right join and the Inner join and the full join working on the table.

    • Create a new two table in the database and insert the data you want to test.

New table:

[SQL]View PlainCopy

Use [Test]

GO

/****** object: Table [dbo].[ EMP] Script Date: 06/22/2012 15:37:28 ******/

SET ansi_nulls on

GO

SET quoted_identifier on

GO

CREATE TABLE [dbo].[ EMP] (

[Ename] [nchar] (Ten) COLLATE chinese_prc_ci_as not NULL,

[City] [nchar] (Ten) COLLATE chinese_prc_ci_as NULL

) on [PRIMARY]


[SQL]View PlainCopyUse [Test]

GO

/****** object: Table [dbo].[ SAL] Script Date: 06/22/2012 15:38:04 ******/

SET ansi_nulls on

GO

SET quoted_identifier on

GO

CREATE TABLE [dbo].[ SAL] (

[Ename] [nchar] (Ten) COLLATE chinese_prc_ci_as not NULL,

[SALARY] [Money] NULL

) on [PRIMARY]


Table to insert data from:

EMP Table:

Sal table:

    • Left JOIN connect [SQL] view plain copy
      1. SELECT * FROM EMP left join SAL on EMP. ename = SAL.  ename;

      Left JOIN, the table EMP is the primary table, so the query results show all the information of the EMP (primary table) and the SAL (schedule) information related to EMP.
    • Right connect [SQL] view plain copy
      1. Select * from EMP right join SAL on EMP. ename = SAL.  ename;

      Right connection, table Sal is the primary table, so the query results show all the information of the salt (the primary table) and the EMP (schedule) and the information that Sal wants to close.
    • Internal connection [SQL] view plain copy
      1. SELECT * FROM EMP inner join SAL on EMP. ename = SAL.  ename;

      Link, which displays information related to a table.
    • Full connection [SQL] view plain copy
      1. SELECT * from EMP full join SAL on EMP. ename = SAL.  ename;

      Fully connected, displaying all two tables of information.

The difference between left join and right join in SQL and Inner join vs. Full join

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.