Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
SQL

SQL: Use Subqueries for Complex Data

- 11.07.26 - ErcanOPAK

📊 Subqueries = Complex Data

Single queries are limited. Subqueries nest queries. Complex conditions, derived data.

📝 Subquery Examples

-- Users with orders over $100
SELECT name, email
FROM users
WHERE id IN (
    SELECT user_id
    FROM orders
    WHERE total > 100
);

-- Users with no orders
SELECT name, email
FROM users
WHERE id NOT IN (
    SELECT DISTINCT user_id
    FROM orders
);

-- Products with average order above 100
SELECT name
FROM products
WHERE price > (
    SELECT AVG(price)
    FROM products
);

-- Customers with orders in last 30 days
SELECT name, email
FROM users
WHERE EXISTS (
    SELECT 1
    FROM orders
    WHERE user_id = users.id
    AND order_date > CURRENT_DATE - INTERVAL 30 DAY
);

🎯 Advanced Subqueries

-- Subquery in SELECT
SELECT 
    name,
    (SELECT COUNT(*) FROM orders WHERE user_id = users.id) as order_count,
    (SELECT COALESCE(SUM(total), 0) FROM orders WHERE user_id = users.id) as total_spent
FROM users;

-- Subquery with multiple columns
SELECT name, email
FROM users u
WHERE (SELECT COUNT(*) FROM orders WHERE user_id = u.id) > 5
AND (SELECT AVG(total) FROM orders WHERE user_id = u.id) > 100;

-- Correlated subquery
SELECT name, salary
FROM employees e
WHERE salary > (
    SELECT AVG(salary)
    FROM employees
    WHERE department_id = e.department_id
);

-- Subquery in FROM (derived table)
SELECT 
    department_id,
    avg_salary
FROM (
    SELECT 
        department_id,
        AVG(salary) as avg_salary
    FROM employees
    GROUP BY department_id
) AS dept_stats
WHERE avg_salary > 50000;

💡 Subquery Tips

  • Use EXISTS for existence checks
  • Use IN for list of values
  • Use scalar subqueries in SELECT
  • Correlated subqueries can be slow
  • Consider JOIN as alternative

“Subqueries add power to SQL. Complex conditions, derived data. Essential for complex queries.”

— SQL Developer

Related posts:

Get the row number "x" from SQL

SQL: Use DELETE to Remove Data from Tables

SQL: Use SELECT to Query Data from Tables

Post Views: 1

Post navigation

.NET Core: Add Globalization for Localization
C#: Master Exception Handling with Try-Catch

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

July 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Jun    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (597)
  • Add Constraint to SQL Table to ensure email contains @ (582)
  • Average of all values in a column that are not zero in SQL (545)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (512)
  • Find numbers with more than two decimal places in SQL (460)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes
  • Ajax: Use Axios for HTTP Requests
  • JavaScript: Understand Hoisting
  • HTML: Use Web Storage for Client-Side Data
  • CSS: Use Filter Effects for Visual Magic
  • Windows 11: Unlock God Mode for All Settings

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com