Database multi-table query where & INNER join__ database

Source: Internet
Author: User

In a multiple-table query, some SQL developers prefer to use where to join, such as:

SELECT a.id, B.name, b.date from Customers A, Sales b WHERE a.id = b.id;

Disadvantage: In the above statement, the Cartesian product of two tables is actually created, and all possible combinations are created. In the Cartesian connection, in the above example, if there were 1000 customers and 1000 sales records, the query would produce 1 million results and then filter out 1000 records with the correct ID. This is an inefficient use of database resources, the database to do 100 times times more work. In a large database, the Cartesian connection is a big problem, and the Cartesian product of two large tables creates 1 billion or trillions of records.

to avoid creating Cartesian product, you should use the inner JOIN:

SELECT a.id, B.name, b.date from Customers a INNER JOIN Sales b on a.id = b.ID;

Advantages: such as the above statement, using the inner join allows the database to produce only 1000 target results that are equal to the ID. Increased query efficiency.

Some database systems recognize where connections and automatically convert to INNER JOIN. In these database systems, there is no performance difference between the WHERE connection and the inner join. However, the INNER JOIN is recognized by all databases, so DBAs recommend using it in your environment.

INNER join (inner JOIN, or equivalent connection): Gets a record of the field matching relationships in the two tables.
LEFT join: Gets all the records in the left table, even if the right table does not have a matching record.
right join: In contrast to a left join, to get all the records in the right table, even if there is no matching record for the left-hand table.

Attention:
1, INNER join is equal to join;
2, schematic:

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.