SQL JOIN connection details and simple use instances, sqljoin

Source: Internet
Author: User

SQL JOIN connection details and simple use instances, sqljoin

SQL JOIN connection

The SQL JOIN clause is used to combine rows from two or more tables based on the common fields between these tables.

The most common JOIN type: SQL INNER JOIN (simple JOIN ). SQL INNER JOIN returns all rows that meet the JOIN conditions from multiple tables.

Let's take a look at the data selected from the "Orders" table:

OrderID CustomerID OrderDate
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20

Then, check the data selected from the "MERs" table:

CustomerID CustomerName ContactName Country
1 Alfreds Futterkiste Maria Anders Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Mexico
3 Antonio Moreno Taquer ía Antonio Moreno Mexico

Note that the "mermerid" column in the "Orders" table points to the Customers in the "Customers" table. The above two tables are linked through the "mermerid" column.

Then, if we run the following SQL statement (including INNER JOIN ):

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDateFROM OrdersINNER JOIN CustomersON Orders.CustomerID=Customers.CustomerID;

The running result is as follows:

OrderID CustomerName OrderDate
10308 Ana Trujillo Emparedados y helados 9/18/1996
10365 Antonio Moreno Taquer ía 11/27/1996
10383 Around the Horn 12/16/1996
10355 Around the Horn 11/15/1996
10278 Berglunds snabbk öp 8/12/1996

Different SQL JOIN

Before continuing to explain the examples, we will first list the different SQL JOIN types you can use:

  • INNER JOIN: If the table has at least one match, the row is returned.
  • Left join:Returns all rows from the left table even if no match exists in the right table.
  • Right join:Returns all rows from the right table even if no match exists in the left table.
  • Full join:If one of the tables matches, the row is returned.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

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.