CROSS JOIN vs INNER JOIN in SQL

前端 未结 12 1149
醉酒成梦
醉酒成梦 2020-11-22 03:16

What is the difference between CROSS JOIN and INNER JOIN?

CROSS JOIN:

SELECT 
    Movies.CustomerID, Movie         


        
12条回答
  •  猫巷女王i
    2020-11-22 03:36

    CROSS JOIN

    As I explained in this article, the CROSS JOIN is meant to generate a Cartesian Product.

    A Cartesian Product takes two sets A and B and generates all possible permutations of pair records from two given sets of data.

    For instance, assuming you have the following ranks and suits database tables:

    And the ranks has the following rows:

    | name  | symbol | rank_value |
    |-------|--------|------------|
    | Ace   | A      | 14         |
    | King  | K      | 13         |
    | Queen | Q      | 12         |
    | Jack  | J      | 11         |
    | Ten   | 10     | 10         |
    | Nine  | 9      |  9         |
    

    While the suits table contains the following records:

    | name    | symbol |
    |---------|--------|
    | Club    | ♣      |
    | Diamond | ♦      |
    | Heart   | ♥      |
    | Spade   | ♠      |
    

    As CROSS JOIN query like the following one:

    SELECT
       r.symbol AS card_rank,
       s.symbol AS card_suit
    FROM
       ranks r
    CROSS JOIN
       suits s
    

    will generate all possible permutations of ranks and suites pairs:

    | card_rank | card_suit |
    |-----------|-----------|
    | A         | ♣         |
    | A         | ♦         |
    | A         | ♥         |
    | A         | ♠         |
    | K         | ♣         |
    | K         | ♦         |
    | K         | ♥         |
    | K         | ♠         |
    | Q         | ♣         |
    | Q         | ♦         |
    | Q         | ♥         |
    | Q         | ♠         |
    | J         | ♣         |
    | J         | ♦         |
    | J         | ♥         |
    | J         | ♠         |
    | 10        | ♣         |
    | 10        | ♦         |
    | 10        | ♥         |
    | 10        | ♠         |
    | 9         | ♣         |
    | 9         | ♦         |
    | 9         | ♥         |
    | 9         | ♠         |
    

    INNER JOIN

    On the other hand, INNER JOIN does not return the Cartesian Product of the two joining data sets.

    Instead, the INNER JOIN takes all elements from the left-side table and matches them against the records on the right-side table so that:

    • if no record is matched on the right-side table, the left-side row is filtered out from the result set
    • for any matching record on the right-side table, the left-side row is repeated as if there was a Cartesian Product between that record and all its associated child records on the right-side table.

    For instance, assuming we have a one-to-many table relationship between a parent post and a child post_comment tables that look as follows:

    Now, if the post table has the following records:

    | id | title     |
    |----|-----------|
    | 1  | Java      |
    | 2  | Hibernate |
    | 3  | JPA       |
    

    and the post_comments table has these rows:

    | id | review    | post_id |
    |----|-----------|---------|
    | 1  | Good      | 1       |
    | 2  | Excellent | 1       |
    | 3  | Awesome   | 2       |
    

    An INNER JOIN query like the following one:

    SELECT
       p.id AS post_id,
       p.title AS post_title,
       pc.review  AS review
    FROM post p
    INNER JOIN post_comment pc ON pc.post_id = p.id
    

    is going to include all post records along with all their associated post_comments:

    | post_id | post_title | review    |
    |---------|------------|-----------|
    | 1       | Java       | Good      |
    | 1       | Java       | Excellent |
    | 2       | Hibernate  | Awesome   |
    

    Basically, you can think of the INNER JOIN as a filtered CROSS JOIN where only the matching records are kept in the final result set.

    For more details about how INNER JOIN works, check out this article as well.

提交回复
热议问题