TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst · 76.7K subscribers
Post #2772 1.3K
⚠️ 23. Common Subquery Mistakes

Mistake 1 — Returning multiple rows with =

Use IN instead of = when multiple rows are expected.

Mistake 2 — Forgetting NULL behavior with NOT IN

Mistake 3 — Ignoring duplicates

Mistake 4 — Making the query unnecessarily complicated

🎤 SQL Interview Questions

Q1. What is a subquery?

A query nested inside another query.

Q2. What is a scalar subquery?

A subquery that returns a single value.

Q3. When should you use IN?

When the subquery returns a set of values.

Q4. What does EXISTS do?

It checks whether at least one matching row exists.

Q5. What is a correlated subquery?

A subquery that references a column from the outer query.

Q6. What is a derived table?

A subquery in the FROM clause treated as a temporary result set.

Q7. Can a subquery be used in SELECT?

Yes.

Q8. What is the difference between IN and EXISTS?

IN compares against a set, while EXISTS checks for existence.

Q9. Why is NOT IN dangerous with NULL?

Three-valued logic can cause unexpected results.

Q10. Can every subquery be replaced with a JOIN?

Many can, but the best approach depends on the logic.

📝 Practice Questions

Practice 1 — Employees earning more than average:

SELECT employee_name, salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);


Practice 2 — Customers with at least one order:

SELECT customer_id, customer_name
FROM customers
WHERE customer_id IN (
SELECT customer_id
FROM orders
);


Practice 3 — Customers with more than 5 orders:

SELECT customer_id, customer_name
FROM customers
WHERE customer_id IN (
SELECT customer_id
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 5
);


Practice 4 — Products higher than average price:

SELECT product_name, price
FROM products
WHERE price > (
SELECT AVG(price)
FROM products
);


Practice 5 — Customers who never placed an order:

SELECT c.customer_id, c.customer_name
FROM customers c
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);


🧪 Mini SQL Challenge

Find customers whose total spending is greater than the average customer spending.

SELECT customer_id, total_spending
FROM (
SELECT
customer_id,
SUM(amount) AS total_spending
FROM orders
GROUP BY customer_id
) AS customer_totals
WHERE total_spending > (
SELECT AVG(total_spending)
FROM (
SELECT
customer_id,
SUM(amount) AS total_spending
FROM orders
GROUP BY customer_id
) AS totals
);


💡 Double Tap ❤️ For More
  • ❤ 8
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 →