Count If in MySQL

Learn via video courses
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Count with If In MySQL

Count If MySQL is a useful and powerful feature that allows users to count records based on specific conditions. By using the IF function within the COUNT function, it becomes possible to perform conditional counting and obtain valuable insights from the data. This feature enables users to analyze subsets of data that meet certain criteria and make more informed decisions. Understanding how to use this feature and its syntax can greatly enhance data manipulation, segmentation, and decision-making capabilities in MySQL. Suppose we want to count the number of students in the grade 7 or 8 who are male from the students table. We can use the COUNT function with IF to achieve this.

Syntax

In MySQL, the "Count with If" query has the following syntax:

The IF function, which assesses a condition and returns a value depending on whether the condition is true or false, is the crucial component in this situation. It returns 1 if the condition is true and NULL otherwise. The COUNT function then counts the non-null values, essentially counting the records that meet the provided requirement.

Sharpen Your Fundamentals with Free Learning

Examples

Consider the "students" sample table, which contains data about students at a school. Columns in the table include "student_id," "student_name," "grade," and "gender."

Sample Table: students

student_idstudent_namegradegender
1Shivam Singla7M
2Saloni8F
3Gautam9M
4Avi9F
5Nitish Garg7M

Example 1:

Counting the number of male students in grade 7.

SQL Query

Output

male_students_count
2

Explanation In this example, the IF function is being used to determine whether the gender is "M" and the grade is 7. If a record's condition is true, the IF function returns 1, indicating a male with grade 7. The IF function returns NULL if the condition is false. The COUNT function then calculates the number of male students in grade 7 by counting the non-null variables. Given that there are two males with grader 7 (Shivam Singla and Nitish Garg) in the sample table, the output is 2.

Example 2:

Counting the number of female students in all grades.

SQL Query

Output

female_students_count
2

Explanation The IF function in this case determines whether the gender is 'F' (female). The IF function returns 1, indicating a female student, if the condition is true. The IF function returns NULL if the condition is false. The count of female students across all grades is then determined by adding up the non-null values using the COUNT function. Since there are two female students (Saloni and Avi) in the sample table the output is 2.

Example 3:

Counting the number of students in grade 7 or grade 8.

SQL Query

Output

grade_7_8_students_count
3

Explanation The IF function in this illustration determines whether the grade is 7 or 8. The IF function returns 1, indicating a student in grade 7 or grade 8, if the condition is true. The IF function returns NULL if the condition is false. We can then use the COUNT function to count the non-null values to determine how many students are in grades 7 or 8. In the sample table, there are three students (Shivam Singla, Saloni, and Nitish Garg), resulting in an output of 3.

Example 4:

Counting the number of students with names starting with 'S'.

SQL Query

Output

s_starting_names_count
2

Explanation The IF function in this illustration determines whether the student_name begins with the letter "S." The IF function returns 1, which denotes a student with a name that starts with "S," if the condition is true. The IF function returns NULL if the condition is false. The number of students with names that begin with "S" is then determined by counting the non-null values using the COUNT function. Two students (Shivam Singla and Saloni) in the sample table have names that begin with "S," hence the output is 2.

Example 5:

Counting the number of students with names containing the letter 'l'.

SQL Query

Output

students_with_l_count
2

Explanation The IF function in the example above determines whether the student_name contains the letter "l." The IF function returns 1, indicating a student whose name contains the letter "l," if the condition is true. The IF function returns NULL if the condition is false. The COUNT function then calculates the number of students whose names contain the letter "l" by counting the non-null values. Two students (Shivam Singla and Saloni) in the sample table have names that contain the letter "l," producing an output of two.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Conclusion

  • An effective tool for conditional counting is the "Count If MySQL" feature, which enables users to count entries in accordance with specified conditions.
  • When a condition is tested with the IF function, it returns 1 if it is true or NULL if it is false.
  • Users can examine subsets of data that satisfy particular criteria using the "Count If MySQL" feature, which offers insightful information about the data.