TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2771 1.01K
Means: Return customers for whom no matching order exists.

๐Ÿง  12. EXISTS vs IN

IN โ†’ Compare against a set of returned values

EXISTS โ†’ Check whether a matching row exists

๐Ÿ”„ 13. Correlated Subquery

Depends on the current row of the outer query.

SELECT
e.employee_name,
e.salary,
e.department_id
FROM employees e
WHERE e.salary > (
SELECT AVG(e2.salary)
FROM employees e2
WHERE e2.department_id = e.department_id
);


ยซIs this employee's salary higher than the average salary of their own department?ยป

๐Ÿข 14. Above-Department-Average Salary

This is a correlated subquery. Bob is compared against the Sales average, while David is compared against the IT average.

๐Ÿ“ฆ 15. Subquery in FROM

It can appear in the FROM clause.

SELECT
customer_id,
total_spending
FROM (
SELECT
customer_id,
SUM(amount) AS total_spending
FROM orders
GROUP BY customer_id
) AS customer_sales;


This is often called a derived table.

๐Ÿ“ˆ 16. Finding High-Value Customers

SELECT
customer_id,
total_spending
FROM (
SELECT
customer_id,
SUM(amount) AS total_spending
FROM orders
GROUP BY customer_id
) AS customer_sales
WHERE total_spending > 50000;


๐Ÿงฉ 17. Subquery in SELECT

SELECT
c.customer_name,
(
SELECT COUNT(*)
FROM orders o
WHERE o.customer_id = c.customer_id
) AS order_count
FROM customers c;


โš ๏ธ 18. But Be Careful with Correlated Subqueries

Instead of a correlated subquery in SELECT, you could use:

SELECT
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;


The better choice depends on the database optimizer, table size, indexes, and other factors.

๐Ÿงฑ 19. Nested Subqueries

SELECT *
FROM products
WHERE price > (
SELECT AVG(price)
FROM products
WHERE category_id IN (
SELECT category_id
FROM categories
WHERE category_name = 'Electronics'
)
);


Deeply nested queries can become difficult to read. Use CTEs instead.

๐Ÿง  20. Subqueries vs JOINs

Subquery:

SELECT *
FROM customers
WHERE customer_id IN (
SELECT customer_id
FROM orders
);


JOIN:

SELECT DISTINCT
c.*
FROM customers c
JOIN orders o
ON c.customer_id = o.customer_id;


Choose based on readability, business logic, and performance.

๐Ÿ† 21. Subqueries for Business Analysis

Find products above average:

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


Find customers with at least one order:

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


Find customers with more than 10 orders:

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


๐Ÿงฎ 22. Subquery for KPI Comparison

SELECT order_id, customer_id, amount
FROM orders
WHERE amount > (
SELECT AVG(amount)
FROM orders
);
  • โค 1
More from @sqlanalyst
  1. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  2. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  3. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  4. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  5. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
  6. Sep 28, 2026๐Ÿง  Real-World SQL Scenario-Based Questions & Answers 1. Get the 2nd highest salary from thโ€ฆ
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 โ†’