Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9100847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:58:11+00:00 2026-06-17T00:58:11+00:00

I have some doubt with the SUM() function in mysql. When i am using

  • 0

I have some doubt with the SUM() function in mysql. When i am using SUM() function without condition is will work fine. But if i will use SUM() function with condition it will return number of records that match the condition. Explain me this.

I was created one table Teacher with four column as below.

mysql> desc Teacher;


+--------------+-------------+------+-----+-------------------+
| Field        | Type        | Null | Key | Default           |
+--------------+-------------+------+-----+-------------------+
| TeacherId    | varchar(36) | NO   | PRI | NULL              |
| FirstName    | varchar(50) | NO   |     | NULL              |
| LastName     | varchar(50) | NO   |     | NULL              |
| SyncStatus   | int(11)     | NO   |     | NULL              |
+--------------+-------------+------+-----+-------------------+

It was contained following records.

mysql> select * from Teacher;

+-----------+-----------+-----------+------------+
| TeacherId | FirstName | LastName  | SyncStatus |
+-----------+-----------+-----------+------------+
| 001       | Sagar     | Kapadia   |          1 |
| 002       | Vishal    | Chaudhari |          1 |
| 003       | Milan     | Panchal   |          1 |
| 004       | Jugal     | Desai     |          1 |
| 005       | Baburao   | Apte      |          1 |
| 006       | Jaya      | Bacchan   |          0 |
| 007       | Jaya      | Bacchan   |          2 |
| 008       | Jaya      | Bacchan   |          2 |
+-----------+-----------+-----------+------------+
8 rows in set (0.00 sec)




mysql> select TeacherId,FirstName,LastName,Concat(FirstName,' ',LastName) as FullName,SUM(SyncStatus) from Teacher having FullName like '_____ %';
+-----------+-----------+----------+---------------+-----------------+
| TeacherId | FIrstName | LastName | FullName      | sum(SyncStatus) |
+-----------+-----------+----------+---------------+-----------------+
| 001       | Sagar     | Kapadia  | Sagar Kapadia |               9 |
+-----------+-----------+----------+---------------+-----------------+
1 row in set (0.00 sec)

So it will return me the perfect sum on sync status. But if i will use the SUM() with condition then returen the number of records.

mysql> select TeacherId,FIrstName,LastName,Concat(FirstName,' ',LastName) as FullName,sum(SyncStatus = 0) from Teacher having FullName like '_____ %';
+-----------+-----------+----------+---------------+---------------------+
| TeacherId | FIrstName | LastName | FullName      | sum(SyncStatus = 0) |
+-----------+-----------+----------+---------------+---------------------+
| 001       | Sagar     | Kapadia  | Sagar Kapadia |                   1 |
+-----------+-----------+----------+---------------+---------------------+
1 row in set (0.00 sec)



mysql> select TeacherId,FIrstName,LastName,Concat(FirstName,' ',LastName) as FullName,sum(SyncStatus = 1) from Teacher having FullName like '_____ %';
+-----------+-----------+----------+---------------+---------------------+
| TeacherId | FIrstName | LastName | FullName      | sum(SyncStatus = 1) |
+-----------+-----------+----------+---------------+---------------------+
| 001       | Sagar     | Kapadia  | Sagar Kapadia |                   5 |
+-----------+-----------+----------+---------------+---------------------+
1 row in set (0.00 sec)



mysql> select TeacherId,FIrstName,LastName,Concat(FirstName,' ',LastName) as FullName,sum(SyncStatus = 2) from Teacher having FullName like '_____ %';
+-----------+-----------+----------+---------------+---------------------+
| TeacherId | FIrstName | LastName | FullName      | sum(SyncStatus = 2) |
+-----------+-----------+----------+---------------+---------------------+
| 001       | Sagar     | Kapadia  | Sagar Kapadia |                   2 |
+-----------+-----------+----------+---------------+---------------------+
1 row in set (0.00 sec)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:58:12+00:00Added an answer on June 17, 2026 at 12:58 am

    You are right. This function sum(SyncStatus = 2) will return the number of rows, because that is how it is executed

    In each row SyncStatus = 2 will test if SyncStatus equals 2 and returns 1(true) or 0(false), so you are basically getting the sum of the truth values(1)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some doubt about the title, but I couldn't come up with anything
I have a doubt regarding MySQL. I have a table where it has some
I am trying to fix this problem but I have some doubt. How to
It may be a noob question but I have some doubt. I googled a
I am using oracle db. I have some basic doubt, I have created a
I get doubt that are there some schemas which have a valid schema but
I have some doubt on below code #include<stdio.h> int i=6; int main() { int
I have some doubt on wget command. Here is the thing I want to
I am beginner so have some doubt about Adobe AIR. How do I install
I'm studying javascript for the first time and I've some doubt: I have two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.