TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst · 76.7K subscribers
Post #2755 1.18K
SELECT c.customer_id, c.customer_name, SUM(o.amount) AS total_spending 
FROM customers c
INNER JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name;


💰 11. Include Customers with Zero Spending

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


🔢 12. JOIN + COUNT()

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;


Why COUNT(o.order_id) instead of COUNT(*)?

Because COUNT(*) would count the LEFT JOIN row even when the customer has no matching order.

⚠️ 13. A Very Common JOIN Mistake

SELECT ... WHERE o.amount > 500; -- This removes NULLs and behaves like INNER JOIN


Correct:

LEFT JOIN orders o ON c.customer_id = o.customer_id AND o.amount > 500;


Important concept: With an OUTER JOIN, the location of a filter can change the result.

🔗 14. Joining More Than Two Tables

SELECT c.customer_name, o.order_id, p.product_name, 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;


🏢 15. Real-World Business Example

SELECT p.category, SUM(o.amount) AS total_revenue 
FROM orders o
JOIN products p ON o.product_id = p.product_id
GROUP BY p.category
ORDER BY total_revenue DESC;


This is a typical Data Analyst query.

📈 16. JOIN + WHERE + GROUP BY + HAVING

Question:

«Find customers who spent more than ₹50,000.»

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
HAVING SUM(o.amount) > 50000
ORDER BY total_spending DESC;


Logical flow: JOIN → GROUP BY → HAVING → ORDER BY

🪞 17. SELF JOIN

A table can also be joined to itself.

SELECT e.employee_name AS employee, m.employee_name AS manager 
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;


🔢 18. CROSS JOIN

"CROSS JOIN" produces every possible combination of rows.

5 products x 4 regions = 20 rows

🚨 19. The Biggest JOIN Problem: Duplicate Rows

One customer has five orders → customer appears five times. This is the natural result of a one-to-many relationship.

If you want unique customers:

SELECT COUNT(DISTINCT c.customer_id)
  • ❤ 4
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 →