CROSS JOIN vs INNER JOIN in SQL

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

What is the difference between CROSS JOIN and INNER JOIN?

CROSS JOIN:

SELECT 
    Movies.CustomerID, Movie         


        
相关标签:
12条回答
  • 2020-11-22 03:28

    While writing queries using inner joins the records will fetches from both tables if the condition satisfied on both tables, i.e. exact match of the common column in both tables.

    While writing query using cross join the result is like cartesian product of the no of records in both tables. example if table1 contains 2 records and table2 contains 3 records then result of the query is 2*3 = 6 records.

    So dont go for cross join until you need that.

    0 讨论(0)
  • 2020-11-22 03:30

    It depends on the output you expect.

    A cross join matches all rows in one table to all rows in another table. An inner join matches on a field or fields. If you have one table with 10 rows and another with 10 rows then the two joins will behave differently.

    The cross join will have 100 rows returned and they won't be related, just what is called a Cartesian product. The inner join will match records to each other. Assuming one has a primary key and that is a foreign key in the other you would get 10 rows returned.

    A cross join has limited general utility, but exists for completeness and describes the result of joining tables with no relations added to the query. You might use a cross join to make lists of combinations of words or something similar. An inner join on the other hand is the most common join.

    0 讨论(0)
  • 2020-11-22 03:33

    A = {1,5,3,4,6,7,9,8} B = {2,8,5,4,3,6,9}

    cross join act as Cartesian product A ✖ B = {1,2}, {1,8}...,{5,2}, {5,8},{5,5}.....{3,3}...,{6,6}....{8,9} and returned this long result set..

    when processing inner join its done through Cartesian product and choose matching pairs.. if think this ordered pairs as two table's primary keys and on clause search for A = B then inner join choose {5,5}, {4,4}, {6,6}, {9,9} and returned asked column in select clause related to these id's.

    if cross join on a = b then it's result same result set as inner join. in that case also use inner join.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 03:38

    Please remember, if a WHERE clause is added, the cross join behaves as an inner join. For example, the following Transact-SQL queries produce the same result set. Please refer to http://technet.microsoft.com/en-us/library/ms190690(v=sql.105).aspx

    0 讨论(0)
  • 2020-11-22 03:39

    Cross join does not combine the rows, if you have 100 rows in each table with 1 to 1 match, you get 10.000 results, Innerjoin will only return 100 rows in the same situation.

    These 2 examples will return the same result:

    Cross join

    select * from table1 cross join table2 where table1.id = table2.fk_id
    

    Inner join

    select * from table1 join table2 on table1.id = table2.fk_id
    

    Use the last method

    0 讨论(0)
提交回复
热议问题