SQL Interview Questions

41 Questions
SQL

SQL

BackendWeb DevelopmentData Science

Question 41

What is the difference between a CROSS JOIN and a FULL JOIN?

Answer:

CROSS JOIN and FULL JOIN are both types of joins in SQL, but they serve very different purposes and produce different types of results. Here is an explanation of each, along with their differences:

CROSS JOIN

Purpose: The CROSS JOIN produces a Cartesian product of the two tables involved. This means it returns all possible combinations of rows from the two tables.

Characteristics

  • No Join Condition: The CROSS JOIN does not require a join condition (ON clause).
  • Result Size: The number of rows in the result set is the product of the number of rows in the first table and the number of rows in the second table.
  • Use Case: It is useful when you need to combine every row of one table with every row of another table.

Syntax

SELECT *
FROM table1
CROSS JOIN table2;

Example

Suppose you have two tables:

Products Table

ProductID ProductName
1 Apple
2 Banana

Suppliers Table

SupplierID SupplierName
A Supplier1
B Supplier2

CROSS JOIN Result

SELECT *
FROM Products
CROSS JOIN Suppliers;
ProductID ProductName SupplierID SupplierName
1 Apple A Supplier1
1 Apple B Supplier2
2 Banana A Supplier1
2 Banana B Supplier2

FULL JOIN

Purpose: The FULL JOIN (or FULL OUTER JOIN) combines the results of both LEFT JOIN and RIGHT JOIN. It returns all rows when there is a match in one of the tables. This means it returns all rows from both tables, with NULL values in place where there is no match.

Characteristics

  • Join Condition: The FULL JOIN requires a join condition (ON clause) to specify how the rows between the tables should be matched.
  • Result Size: The result set includes all rows from both tables, with NULL in columns where there is no match.
  • Use Case: It is useful when you need to retrieve all rows from both tables and identify matching and non-matching rows.

Syntax

SELECT *
FROM table1
FULL JOIN table2
ON table1.column_name = table2.column_name;

Example

Suppose you have two tables:

Customers Table

CustomerID CustomerName
1 Alice
2 Bob

Orders Table

OrderID CustomerID OrderDate
101 1 2023-07-01
102 3 2023-07-02

FULL JOIN Result

SELECT *
FROM Customers
FULL JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;
CustomerID CustomerName OrderID OrderDate
1 Alice 101 2023-07-01
2 Bob NULL NULL
NULL NULL 102 2023-07-02

Summary of Differences

Feature CROSS JOIN FULL JOIN
Purpose Produces Cartesian product of two tables Combines results of both LEFT JOIN and RIGHT JOIN
Join Condition No join condition required Requires a join condition (ON clause)
Result Size Product of rows in both tables All rows from both tables, with NULLs for non-matches
Use Case Combine every row of one table with every row of another table Retrieve all rows from both tables, with matching and non-matching rows
Example Combine Products and Suppliers to see all combinations Combine Customers and Orders to see all customers and orders

Conclusion

CROSS JOIN and FULL JOIN serve different purposes in SQL:

  • CROSS JOIN is used to produce a Cartesian product, combining every row of one table with every row of another table, resulting in all possible combinations.
  • FULL JOIN is used to combine the results of LEFT JOIN and RIGHT JOIN, returning all rows from both tables with NULL values where there is no match.

Understanding the differences between these joins allows you to use them appropriately based on the requirements of your data retrieval and manipulation tasks.

Recent job openings