TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst · 76.7K subscribers
Post #2756 1.44K
⚠️ 20. Double Counting in Multiple JOINs

If both "orders" and "payments" have multiple rows per customer, joining them directly can create a many-to-many multiplication.

Example: 2 orders × 3 payments = 6 joined rows. SUM() will overcount.

Understand the grain of each table before joining.

🧠 21. JOINs and Table Grain

Before writing a JOIN, identify:

Table 1 - One row = one customer,

Table 2 - One row = one order → One-to-Many relationship.

Understanding table grain helps prevent: duplicate counts, inflated revenue, incorrect averages, incorrect KPIs.

🎤 SQL Interview Questions

Q1. What is a JOIN?

Combines rows from multiple tables using a related condition.

Q2. What is the difference between INNER JOIN and LEFT JOIN?

INNER returns only matching, LEFT returns all from left + matching from right.

Q3. How do you find customers who never placed an order?

LEFT JOIN + WHERE o.customer_id IS NULL

Q4. What is a SELF JOIN?

Joins a table to itself, for hierarchical relationships.

Q5. What is a CROSS JOIN?

Creates every possible combination.

Q6. Why can JOINs create duplicate rows?

Because of one-to-many or many-to-many relationships.

Q7. Why should you understand table grain?

Because grain determines how rows multiply and whether aggregations become inaccurate.

Q8. What happens when there is no match in a LEFT JOIN?

Columns from right become NULL.

Q9. How do you count unique customers after a JOIN?

COUNT(DISTINCT customer_id)

Q10. Can a query contain multiple JOINs?

Yes.

📝 Practice Questions

Practice 1: Return customer names and their orders.

SELECT c.customer_name, o.order_id 
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id;


Practice 2: Find customers who have never ordered.

SELECT c.customer_id, c.customer_name 
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL;


Practice 3: Calculate total spending per customer.

SELECT c.customer_id, c.customer_name, SUM(o.amount) AS total_spending 
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name;


Practice 4: Return all customers and their order counts, including zero orders.

SELECT c.customer_id, c.customer_name, COUNT(o.order_id) AS order_count 
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name;


Practice 5: Find number of unique customers who placed orders.

SELECT COUNT(DISTINCT c.customer_id) AS unique_customers 
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id;


🧪 Mini SQL Challenge

Write a query that returns: Customer name, Product name, Category, Amount - Only orders > ₹1,000.

Solution:

SELECT c.customer_name, p.product_name, p.category, o.amount 
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
JOIN products p ON o.product_id = p.product_id
WHERE o.amount > 1000
ORDER BY o.amount DESC;


📌 JOINs are the bridge between database tables. But writing a JOIN is only half the skill. A strong Data Analyst also understands: What each table represents → How tables are related → How rows will multiply → How that affects the KPI.

Double Tap ❤️ For More
  • ❤ 9
More from @sqlanalyst
  1. Oct 7, 2026🚀𝗣𝗮𝘆 𝗔𝗳𝘁𝗲𝗿 𝗣𝗹𝗮𝗰𝗲𝗺𝗲𝗻𝘁 𝗧𝗿𝗮𝗶𝗻𝗶𝗻𝗴 | 𝗕𝗲𝗰𝗼𝗺𝗲 𝗮 𝗙𝘂𝗹𝗹𝘀𝘁𝗮𝗰…
  2. Oct 7, 2026SQL Interview Series — Part 4 📌 Question 4: Find the Highest Salary in Each Department Su…
  3. Oct 7, 2026𝗠𝗮𝘀𝘁𝗲𝗿 𝗣𝗼𝘄𝗲𝗿 𝗕𝗜 𝗳𝗼𝗿 𝗙𝗥𝗘𝗘! 🔥 Learn Power BI through these FREE learnin…
  4. Sep 29, 2026SQL Interview Series — Part 2 📌 Question 2: Find Duplicate Records Suppose you have an Em…
  5. Sep 29, 2026𝗙𝗥𝗘𝗘 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀 𝗧𝗼 𝗟𝗲𝗮𝗿𝗻 𝗔𝗜 𝗶𝗻 𝟮𝟬𝟮𝟲🚀 ​ Explore 6 free resource…
  6. Sep 29, 2026SQL Interview Series — Part 1 Hi guys, let's start a SQL interview series covering frequen…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →