Challenge - 5 Problems
Master of CROSS JOIN
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of CROSS JOIN with two small tables
Given two tables Colors and Shapes, what is the output of the following query?
Tables:
Colors: {color: 'Red'}, {color: 'Blue'}
Shapes: {shape: 'Circle'}, {shape: 'Square'}
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;
Attempts:
2 left
💡 Hint
Remember, CROSS JOIN pairs every row from the first table with every row from the second table.
✗ Incorrect
CROSS JOIN produces all combinations of rows from both tables. With 2 colors and 2 shapes, total rows = 2 * 2 = 4.
🧠 Conceptual
intermediate1: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?Attempts:
2 left
💡 Hint
Think about how many pairs you can make combining rows from both tables.
✗ Incorrect
CROSS JOIN returns every combination of rows from both tables, so total rows = 5 * 3 = 15.
📝 Syntax
advanced2:00remaining
Identify the correct CROSS JOIN syntax
Which of the following SQL queries correctly performs a CROSS JOIN between tables
Employees and Departments?Attempts:
2 left
💡 Hint
CROSS JOIN does not require an ON clause.
✗ Incorrect
Option A uses the correct CROSS JOIN syntax. Options B and D use INNER JOIN or implicit join with conditions, which are not CROSS JOIN. Option A is incomplete syntax.
🔧 Debug
advanced2:30remaining
Why does this CROSS JOIN query produce an error?
Consider the query:
It produces an error: "ambiguous column name 'name'". Why?
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?
Attempts:
2 left
💡 Hint
Check the column names and how they are referenced in SELECT.
✗ Incorrect
Both Authors and Books tables have a 'name' column. The query selects 'a.name' and 'b.name' which is correct, but if the query was written as SELECT name, name it would be ambiguous. The error suggests the query might have SELECT name, name without aliases.
❓ optimization
expert3:00remaining
Optimizing a CROSS JOIN query with large tables
You have two large tables,
Which approach can improve performance?
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?
Attempts:
2 left
💡 Hint
Think about reducing the number of rows before joining.
✗ Incorrect
Filtering rows before CROSS JOIN reduces the number of combinations and speeds up the query. Indexes don't help much for CROSS JOIN because it produces all combinations. INNER JOIN without ON is invalid. UNION ALL combines rows vertically, not horizontally.