Languages
[Edit]
EN

MS SQL Server - count if

0 points
Created by:
Roseanne-Read
1365

In this article, we would like to show you how to count rows when the expression is true in MS SQL Server.

Quick solution:

Note:

Go to this article to read more about CASE statement.

Practical example

Edit

To show you how IF() function works, we will use the following users table:

MS SQL Server - example data used to explain IF() function
MS SQL Server - example data.

Note:

At the end of this article you can find database preparation SQL queries.

Example

Edit

In this example, we will count the number of users whose salary is high, average or low.

Query

Result:

MS SQL Server - COUNT IF example - result
Results - HeidiSQL

Database preparation

Edit

create_tables.sql file:

insert_data.sql file:

Alternative titles

  1. MS SQL Server - COUNT IF equivalent
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

MS SQL Server - functions

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join