0
0
SQLquery~20 mins

CROSS JOIN cartesian product in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of CROSS JOIN
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of CROSS JOIN with two small tables
Given two tables Colors and Shapes, what is the output of the following query?
SELECT Colors.color, Shapes.shape FROM Colors CROSS JOIN Shapes;

Tables:
Colors: {color: 'Red'}, {color: 'Blue'}
Shapes: {shape: 'Circle'}, {shape: 'Square'}
SQL
SELECT Colors.color, Shapes.shape FROM Colors CROSS JOIN Shapes;
A[{'color': 'Red', 'shape': 'Circle'}, {'color': 'Blue', 'shape': 'Square'}]
B[{'color': 'Red', 'shape': 'Circle'}, {'color': 'Red', 'shape': 'Square'}, {'color': 'Blue', 'shape': 'Circle'}, {'color': 'Blue', 'shape': 'Square'}]
C[{'color': 'Red', 'shape': 'Circle'}, {'color': 'Blue', 'shape': 'Circle'}, {'color': 'Red', 'shape': 'Square'}, {'color': 'Blue', 'shape': 'Square'}]
D[{'color': 'Red', 'shape': 'Square'}, {'color': 'Blue', 'shape': 'Circle'}]
Attempts:
2 left
💡 Hint
Remember, CROSS JOIN pairs every row from the first table with every row from the second table.
🧠 Conceptual
intermediate
1:30remaining
Understanding the number of rows in a CROSS JOIN
If table A has 5 rows and table B has 3 rows, how many rows will the query SELECT * FROM A CROSS JOIN B; return?
A5
B2
C8
D15
Attempts:
2 left
💡 Hint
Think about how many pairs you can make combining rows from both tables.
📝 Syntax
advanced
2:00remaining
Identify the correct CROSS JOIN syntax
Which of the following SQL queries correctly performs a CROSS JOIN between tables Employees and Departments?
ASELECT * FROM Employees CROSS JOIN Departments;
BSELECT * FROM Employees JOIN Departments ON Employees.id = Departments.id;
CSELECT * FROM Employees INNER JOIN Departments;
DSELECT * FROM Employees, Departments WHERE Employees.id = Departments.id;
Attempts:
2 left
💡 Hint
CROSS JOIN does not require an ON clause.
🔧 Debug
advanced
2:30remaining
Why does this CROSS JOIN query produce an error?
Consider the query:
SELECT a.name, b.name FROM Authors a CROSS JOIN Books b WHERE a.id = b.author_id;

It produces an error: "ambiguous column name 'name'". Why?
AThe query is missing a GROUP BY clause.
BThe alias 'a' and 'b' are not allowed in CROSS JOIN queries.
CBoth tables have a column named 'name' and the query does not specify which one to use in SELECT.
DCROSS JOIN cannot be used with WHERE clauses.
Attempts:
2 left
💡 Hint
Check the column names and how they are referenced in SELECT.
optimization
expert
3:00remaining
Optimizing a CROSS JOIN query with large tables
You have two large tables, Products with 100,000 rows and Colors with 50 rows. You want to list all product-color combinations but the query is slow:
SELECT p.id, c.color FROM Products p CROSS JOIN Colors c;

Which approach can improve performance?
AFilter Products or Colors before the CROSS JOIN to reduce rows.
BUse INNER JOIN instead of CROSS JOIN without any ON condition.
CAdd an index on Products.id and Colors.color columns.
DReplace CROSS JOIN with UNION ALL of Products and Colors.
Attempts:
2 left
💡 Hint
Think about reducing the number of rows before joining.