0

I'm messing around in my school's database and I am trying to count the null values in this query:

SELECT Depart_ID, DepartName,
       COUNT(Name) AS employees, 
       SUM(Salary) AS 'Total salary'
FROM Ch4Employee AS E
INNER JOIN Ch4Department AS D 
   ON E.Depart_ID = D.ID
GROUP BY Depart_ID, DepartName

Any tips?

1
  • 2
    NULL values in which field? Commented Mar 4, 2022 at 1:15

2 Answers 2

1

If you want to count NULL salaries, do count(*) - count(salary), because count(*) counts all rows, and count(salary) counts the non-null values.

E.g.

SELECT Depart_ID, DepartName,
       COUNT(Name) AS employees, 
       SUM(Salary) AS 'Total salary',
       COUNT(*) - COUNT(Salary) AS null_salaries
FROM Ch4Employee AS E
INNER JOIN Ch4Department AS D 
   ON E.Depart_ID = D.ID
GROUP BY Depart_ID, DepartName
Sign up to request clarification or add additional context in comments.

Comments

0
select Depart_ID, DepartName, COUNT(Name) as employees, SUM(Salary) as 'Total salary' from Ch4Employee AS E 
inner join Ch4Department as D on E.Depart_ID = D.ID
group by Depart_ID, DepartName
HAVING COUNT(Name) = 0

If you want to check the values ​​after aggregation you have to use Having.

3 Comments

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
Actually, how does this answer the OP's question? Where is the 40 coming from?
Syntax error. One GROUP BY too many.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.