Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

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

Tag: SQL Best Practices

SQL

SQL: Use CTEs for Readable Complex Queries

- 30.03.26 - ErcanOPAK comment on SQL: Use CTEs for Readable Complex Queries

📝 Named Subqueries Nested subqueries everywhere? Can’t understand your own query? CTEs (WITH clause) create temporary named result sets. Makes complex queries readable. The Subquery Nightmare — ❌ Nested subqueries – Unreadable! SELECT customer_name, total_orders, avg_order_value FROM ( SELECT c.name AS customer_name, COUNT(o.id) AS total_orders, AVG(o.total) AS avg_order_value FROM customers c JOIN ( SELECT * […]

Read More
SQL

SQL: Use OFFSET FETCH for Pagination

- 19.03.26 | 19.03.26 - ErcanOPAK comment on SQL: Use OFFSET FETCH for Pagination

Building pagination? OFFSET FETCH is standard SQL way. — Page 1 (rows 1-10) SELECT * FROM Products ORDER BY ProductId OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY; — Page 2 (rows 11-20) SELECT * FROM Products ORDER BY ProductId OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY; — Dynamic pagination DECLARE @PageNumber INT = […]

Read More
SQL

SQL: Use COALESCE to Handle NULL Values

- 19.03.26 | 19.03.26 - ErcanOPAK comment on SQL: Use COALESCE to Handle NULL Values

NULL values breaking calculations? Use COALESCE for fallback. — Returns first non-NULL value SELECT COALESCE(Phone, Email, ‘No contact’) AS Contact FROM Users; — Default values in calculations SELECT Name, Price * COALESCE(Quantity, 0) AS Total FROM Orders; — Multiple fallbacks SELECT COALESCE(PreferredName, FirstName, ‘Unknown’) AS DisplayName FROM Users; vs ISNULL: COALESCE is ANSI standard (works […]

Read More
SQL

SQL: Use CTEs for Readable Complex Queries

- 19.03.26 | 19.03.26 - ErcanOPAK comment on SQL: Use CTEs for Readable Complex Queries

📖 Readable SQL Nested subqueries hurt readability. CTEs (Common Table Expressions) make complex queries clear. — Clear, readable, step-by-step WITH ActiveUsers AS ( SELECT * FROM Users WHERE IsActive = 1 ), RecentOrders AS ( SELECT * FROM Orders WHERE CreatedAt > DATEADD(month, -1, GETDATE()) ) SELECT u.Name, COUNT(o.OrderId) AS OrderCount FROM ActiveUsers u LEFT […]

Read More
SQL

SQL Query Optimization: The 5 Tricks That Saved Our Database from Collapse

- 23.02.26 | 23.02.26 - ErcanOPAK comment on SQL Query Optimization: The 5 Tricks That Saved Our Database from Collapse

💥 The Database Meltdown 9 AM Monday. Database CPU: 98%. Queries timing out. Users can’t log in. Boss is screaming. You have 1 hour to fix it before the CEO finds out. The 5 Performance Killers (And How to Fix Them) 💣 Killer #1: SELECT * ❌ SLOW (transfers 50 columns, 10MB) SELECT * FROM […]

Read More
SQL

SQL: Use EXISTS Instead of IN for Better Performance

- 22.02.26 | 22.02.26 - ErcanOPAK comment on SQL: Use EXISTS Instead of IN for Better Performance

IN processes entire subquery. EXISTS stops at first match (faster). Slow (IN): SELECT * FROM Customers WHERE CustomerId IN ( SELECT CustomerId FROM Orders WHERE Total > 1000 ); — Processes ALL matching orders Fast (EXISTS): SELECT * FROM Customers c WHERE EXISTS ( SELECT 1 FROM Orders o WHERE o.CustomerId = c.CustomerId AND o.Total […]

Read More
SQL

SQL: Use UNION ALL Instead of UNION When Duplicates Don’t Matter

- 22.02.26 | 22.02.26 - ErcanOPAK comment on SQL: Use UNION ALL Instead of UNION When Duplicates Don’t Matter

UNION removes duplicates (expensive operation). UNION ALL keeps them (fast). UNION (Slow): SELECT Name FROM ActiveUsers UNION SELECT Name FROM InactiveUsers; — Removes duplicates: Sorts, scans, filters — Slow on large datasets! UNION ALL (Fast): SELECT Name FROM ActiveUsers UNION ALL SELECT Name FROM InactiveUsers; — Keeps duplicates: No sorting or filtering — Much faster! […]

Read More
SQL

SQL: Use CTEs to Make Complex Queries Readable

- 15.02.26 - ErcanOPAK comment on SQL: Use CTEs to Make Complex Queries Readable

Nested subqueries are impossible to read. CTEs (Common Table Expressions) break them into named, readable chunks. Messy Nested Query: SELECT * FROM Orders o WHERE o.CustomerId IN ( SELECT CustomerId FROM Customers WHERE Country IN ( SELECT Country FROM TopCountries WHERE Revenue > 1000000 ) ); Clean CTE Version: WITH TopCountries AS ( SELECT Country […]

Read More
SQL

SQL: Use COALESCE to Replace NULL Values Inline

- 13.02.26 - ErcanOPAK comment on SQL: Use COALESCE to Replace NULL Values Inline

Don’t use CASE WHEN for simple NULL checks. COALESCE is cleaner. Verbose Way: SELECT CASE WHEN Name IS NULL THEN ‘Unknown’ ELSE Name END AS Name FROM Users; Clean Way: SELECT COALESCE(Name, ‘Unknown’) AS Name FROM Users; Multiple Fallbacks: SELECT COALESCE(MobilePhone, WorkPhone, HomePhone, ‘No phone’) AS Phone FROM Users; Returns first non-NULL value.

Read More
SQL

SQL: Use MERGE to Upsert (Insert or Update) in One Statement

- 03.02.26 - ErcanOPAK comment on SQL: Use MERGE to Upsert (Insert or Update) in One Statement

Inserting if row doesn’t exist, updating if it does? MERGE statement does both in one atomic operation without race conditions. The Problem – Separate Insert/Update: — Check if exists IF EXISTS (SELECT 1 FROM Products WHERE ProductId = @Id) BEGIN — Update UPDATE Products SET ProductName = @Name, Price = @Price WHERE ProductId = @Id; […]

Read More
SQL

SQL: When CTEs Kill Performance and You Should Use Temp Tables Instead

- 01.02.26 - ErcanOPAK comment on SQL: When CTEs Kill Performance and You Should Use Temp Tables Instead

Common Table Expressions (CTEs) make queries readable but can cause performance disasters when SQL Server evaluates them multiple times. The CTE Performance Trap: — ❌ BAD: CTE evaluated 3 times! WITH RecentOrders AS ( SELECT CustomerId, OrderDate, Total FROM Orders WHERE OrderDate >= DATEADD(month, -1, GETDATE()) — Complex calculation that takes 5 seconds ) SELECT […]

Read More
SQL

SQL Indexing: Why Your Query Ignores Indexes and Scans 10 Million Rows Instead

- 01.02.26 - ErcanOPAK comment on SQL Indexing: Why Your Query Ignores Indexes and Scans 10 Million Rows Instead

Created an index but your query still takes 30 seconds? SQL Server might be ignoring it due to implicit conversions, functions, or wildcard positioning. Index Killer #1 – Functions on Indexed Columns: — Index exists on CreatedDate CREATE INDEX IX_Orders_CreatedDate ON Orders(CreatedDate); — ❌ BAD: Index ignored, full table scan SELECT * FROM Orders WHERE […]

Read More
November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

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

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

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