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

Average of all values in a column that are not zero in SQL

- 11.06.19 | 24.03.26 - ErcanOPAK

The Challenge: You need to calculate the average price, rating, or score in a column. However, the column contains 0 (zero) values that represent “missing data” or “not yet rated.” If you use a standard AVG(), these zeros will pull your average down significantly, giving you an inaccurate result.

The Secret: In SQL, the AVG() function is designed to automatically ignore NULL values, but it always counts Zeros. To get an accurate average of only the non-zero values, we just need to temporarily turn those zeros into NULL.


🚀 The “Elegant” Solution: Using NULLIF

Instead of writing long CASE statements, the NULLIF function is the most concise way to solve this in a single line:

-- The cleanest way to average non-zero values
SELECT AVG(NULLIF(ColumnName, 0)) AS PureAverage
FROM YourTable;

How it works: NULLIF(ColumnName, 0) checks each value. If it’s a 0, it returns NULL. If it’s anything else, it returns the original value. Since AVG() ignores nulls, your zeros no longer affect the calculation!


🛠️ The “Explicit” Way: Using CASE

If you prefer a more readable or standard SQL approach (or if you need more complex logic), the CASE statement is your best friend:

SELECT 
    AVG(CASE WHEN Score <> 0 THEN Score ELSE NULL END) AS CleanAverage
FROM ExamResults;

📊 Why not just use a WHERE clause?

Using WHERE Value <> 0 filters the entire row out of your result set. If you are trying to calculate multiple different averages or counts in the same query, a WHERE clause will break your other calculations.

Pro Tip: Using the NULLIF method inside the SELECT allows you to calculate both the “Total Average” and the “Non-Zero Average” in the same row!

Summary

In SQL, 0 is data, but NULL is the absence of data. By mastering NULLIF, you can control exactly how your aggregate functions behave, leading to more accurate reporting and cleaner code.

Related posts:

SQL Deletes Lock Tables

How to list all tables referencing a table by Foreign Key in MS SQL

SQL Server: Querying JSON Columns Like a Pro

Post Views: 531

Post navigation

Confirm before process with ASPxButton in Devexpress
Using View Model to pass Multiple Models in Single View in MVC

Leave a Reply Cancel reply

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

April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

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

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

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

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

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