📖 Scenario: You are working with two small tables in a store database: one lists products and the other lists colors. You want to see every possible combination of product and color to plan your inventory.
🎯 Goal: Build a query using CROSS JOIN to generate all combinations of products and colors.
📋 What You'll Learn
Create a table called products with columns id (integer) and name (text) and insert exactly these rows: (1, 'Shirt'), (2, 'Pants')
Create a table called colors with columns id (integer) and color (text) and insert exactly these rows: (1, 'Red'), (2, 'Blue')
Write a SELECT query that uses CROSS JOIN between products and colors
Select products.name and colors.color in the result
💡 Why This Matters
🌍 Real World
Retailers often need to see all possible combinations of product attributes to plan inventory and marketing.
💼 Career
Understanding <code>CROSS JOIN</code> helps in data analysis and reporting tasks where combinations of data sets are required.
Progress0 / 4 steps
1
Create the products table and insert data
Create a table called products with columns id as integer and name as text. Insert these exact rows: (1, 'Shirt') and (2, 'Pants').
PostgreSQL
Hint
Use CREATE TABLE to define the table and INSERT INTO with multiple rows.
2
Create the colors table and insert data
Create a table called colors with columns id as integer and color as text. Insert these exact rows: (1, 'Red') and (2, 'Blue').
PostgreSQL
Hint
Similar to step 1, use CREATE TABLE and INSERT INTO for the colors table.
3
Write a CROSS JOIN query to combine products and colors
Write a SELECT query that selects products.name and colors.color from productsCROSS JOINcolors.
PostgreSQL
Hint
Use SELECT with CROSS JOIN to get all combinations.
4
Complete the query with an alias for clarity
Modify the CROSS JOIN query to add table aliases p for products and c for colors. Select p.name and c.color.
PostgreSQL
Hint
Use AS or just put the alias after the table name for clarity.
Practice
(1/5)
1. What does a CROSS JOIN do in PostgreSQL?
easy
A. Pairs every row from the first table with every row from the second table.
B. Returns only matching rows based on a condition.
C. Joins tables based on a common column with duplicates removed.
D. Filters rows from the first table that exist in the second table.
Solution
Step 1: Understand CROSS JOIN concept
A CROSS JOIN creates combinations of all rows from both tables without any condition.
Step 2: Compare with other JOIN types
Unlike INNER JOIN or LEFT JOIN, CROSS JOIN does not filter or match rows but pairs all possible combinations.
Final Answer:
Pairs every row from the first table with every row from the second table. -> Option A
Quick Check:
CROSS JOIN = all combinations [OK]
Hint: Remember: CROSS JOIN multiplies rows from both tables [OK]
Common Mistakes:
Confusing CROSS JOIN with INNER JOIN
Thinking CROSS JOIN filters rows
Assuming CROSS JOIN requires a join condition
2. Which of the following is the correct syntax for a CROSS JOIN in PostgreSQL?
easy
A. SELECT * FROM table1 CROSS JOIN table2;
B. SELECT * FROM table1 JOIN table2 ON table1.id = table2.id;
C. SELECT * FROM table1 INNER CROSS JOIN table2;
D. SELECT * FROM table1 CROSS JOIN ON table2;
Solution
Step 1: Recall CROSS JOIN syntax
The correct syntax is SELECT * FROM table1 CROSS JOIN table2; without any ON clause.
Step 2: Identify incorrect options
SELECT * FROM table1 JOIN table2 ON table1.id = table2.id; uses INNER JOIN with ON clause, C has invalid keyword INNER CROSS JOIN, A has misplaced ON keyword.
Final Answer:
SELECT * FROM table1 CROSS JOIN table2; -> Option A
Quick Check:
Syntax for CROSS JOIN = SELECT * FROM table1 CROSS JOIN table2; [OK]
Hint: CROSS JOIN syntax has no ON clause [OK]
Common Mistakes:
Adding ON clause with CROSS JOIN
Using INNER CROSS JOIN which is invalid
Misplacing keywords in the query
3. You have two tables: Colors: color_id | color_name 1 | Red 2 | Blue