SQL Query is not returning NULL values despite using IFNULL (MySQL)

mysql ifnull
coalesce
mysql ifnull in where clause
bigquery ifnull
mysql ifnull not working
mysql ifnull else
sql if null then 0
mysql ifnull vs coalesce

I have a test table set up as such: Table Rows setup

My objective is to try and get the count of departments that were established before the current department. My SQL Is:

SELECT A.Department, IFNULL(COUNT(*), 0) 
FROM Departments A
INNER JOIN Departments B ON B.YearOfEstablishment < A.YearOfEstablishment
GROUP BY Department
ORDER BY COUNT(*);

However I've tried both LEFT JOIN and INNER JOIN, the last department that was found first never is returned because I can assume it is null. Despite having IFNULL, the department is not shown.

What am I doing wrong here?

I think that this is the query you need :

SELECT A.Department, COUNT(B.Department)
FROM Departments A
LEFT JOIN Departments B ON B.YearOfEstablishment < A.YearOfEstablishment
GROUP BY A.Department
ORDER BY 2;

See this db fiddle demo.

| Department        | cnt |
| ----------------- | --- |
| Office Management | 0   |
| Business          | 1   |
| Sales Management  | 2   |
| ComputerScience   | 3   |
| Liberal Arts      | 4   |
| Farming           | 4   |
| Communications    | 6   |
| Digital Science   | 7   |

NB : as commented by @fifonik, IFNULL is not needed since COUNT already returns 0 when no records are available.

MySQL IFNULL Function And Practical Example - Querychat, Your SQL questions, answered In common parlance, IFNULL is used to return the value of an expression. Here, the resulting expression from the function will be exp1 if it is not null. In case Therefore, IFNULL could be used in the list of columns in select clause, in the where clause or even in the order by clauses. The NOT logical operate is used to test for Boolean values and evaluates to true if the Boolean value is false and false if the Boolean value is true. The NOT NULL clause is used to eliminate NULL values from a result set. Performing arithmetic operations on NULL values always returns NULL results.

In MySQL 8+, you can better do this using rank() or row_number():

SELECT d.Department,
       ROW_NUMBER() OVER (ORDER BY YearOfEstablishment) - 1 as seqnum
FROM Departments d
ORDER BY seqnum;

With no ties, this would be the same as your query. It might be better to do:

SELECT d.Department,
       RANK() OVER (ORDER BY YearOfEstablishment) - 1 as seqnum
FROM Departments d
ORDER BY seqnum;

This should be the count you are looking for.

MySQL IFNULL, Summary: in this tutorial, you will learn about the MySQL IFNULL function, which is a very handy control flow function to handle NULL values. The following illustrates the syntax of the IFNULL function: IFNULL(expression_1,expression_2​);. The IFNULL function returns expression_1 if expression_1 is not NULL ; otherwise  The IFNULL function returns expression_1 if expression_1 is not NULL; otherwise, it returns expression_2. The. The IFNULL function returns a string or a numeric based on the context where it is used. If you want to return a value based on TRUE or FALSE condition other than NULL, you should use the IF function. MySQL IFNULL function examples

You can also do something like this:

SELECT
      A.Department
    , A.YearOfEstablishment
    , COUNT(*) - 1
FROM
    Departments A
    INNER JOIN Departments B ON (
           A.id = B.id
        OR B.YearOfEstablishment < A.YearOfEstablishment
    )
GROUP BY
    Department
ORDER BY
    COUNT(*);

The '= NULL' Mistake and other SQL NULL Heresies, Phil Factor explains how to avoid this, and other SQL NULL-related in an expression requires use of the COALESCE() , ISNULL() or NULLIF() If you're not used to the concept of 'null', and you treat it like a If there is a value in the Title column, you add a space to it and return it. NET · Oracle · MySQL  MySQL. The MySQL IFNULL () function lets you return an alternative value if an expression is NULL: SELECT ProductName, UnitPrice * (UnitsInStock + IFNULL (UnitsOnOrder, 0)) FROM Products; or we can use the COALESCE () function, like this: SELECT ProductName, UnitPrice * (UnitsInStock + COALESCE(UnitsOnOrder, 0)) FROM Products;

MySQL IFNULL() Function, with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. The IFNULL() function returns a specified value if the expression is NULL. If the expression is NOT NULL, this function returns the expression. Syntax. IFNULL(expression, alt_value) Works in: From MySQL 4.0​  The IFNULL() function returns a specified value if the expression is NULL. If the expression is NOT NULL, this function returns the expression. Syntax

NULL, Null values just propagate through expressions without aborting execution. concept used internally in the SQL standard to define the behavior of group by , select MySQL and MariaDB act in the spirit of the SQL standard and return null . That means that there are two cases in which the result is not null , although one  SQL Query to Select All If Parameter is Empty or NULL Examples. The following list of example will show you various ways to get the result. SQL Query to Select All If Parameter is NULL. In this example, we are using IIF Function along with IS NULL to check whether the parameter value is NULL or not. If true, Occupation = Occupation otherwise

How to Handle Null Values in MYSQL Server, Even though it is permitted to use Null as the value of an expression that must where the tutorial_count column is or is not NULL, the queries should be for use with NULL values which include ISNULL() and IFNULL(). For example : Any NULL argument passed to CONCAT() causes it to return NULL:. In SQL, NULL is a special marker used to indicate that a data value does not exist in the database. For more details, check out Wikipedia's explanation of NULL in SQL . We will use the following employee table to illustrate how the GROUP BY clause works with NULL values.

Comments
  • You do not need to use IFNULL here as COUNT always return number.
  • @fifonik : correct, thanks for reminding me of that... I updated my answer accordingly.