sqlsql-serversql-server-2008t-sqlcross-join

CROSS JOIN vs INNER JOIN in SQL


What is the difference between CROSS JOIN and INNER JOIN?

CROSS JOIN:

SELECT 
    Movies.CustomerID, Movies.Movie, Customers.Age, 
    Customers.Gender, Customers.[Education Level], 
    Customers.[Internet Connection], Customers.[Marital Status], 
FROM   
    Customers 
CROSS JOIN 
    Movies

INNER JOIN:

SELECT 
    Movies.CustomerID, Movies.Movie, Customers.Age, 
    Customers.Gender, Customers.[Education Level], 
    Customers.[Internet Connection], Customers.[Marital Status]
FROM   
    Customers 
INNER JOIN 
    Movies ON Customers.CustomerID = Movies.CustomerID

Which one is better and why would I use either one?


Solution

  • 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