I have a table with 2 fields
x y
---- ----
1 null
2 5
3 5
4 null
5 null
6 10
7 5
And my SQLite query is
select y,count(y)
from mytable
group by y
And the result is
null 0
5 3
10 1
It is expected to see null 3.
But the output is null 0.
what does it mean?
ORDER BY and GROUP BY with NULL SQL considers the NULL values as the UNKNOWN values. Therefore, if we use ORDER By and GROUP by clause with NULL value columns, it treats them equally and sorts, group them. For example, in our customer table, we have NULLs in the MilddleName column.
Group functions ignore the NULL values in the column. To enforce the group functions ti include the NULL value, use NVL function.
If a grouping column contains null values, all null values are considered equal, and they are put into a single group.
Answer: C. NVL is a general function used to provide alternate value to the NULL values. The functions MAX, MIN and AVG can be used as GROUP BY functions.
From Aggregate Functions in SQLite
The count(X) function returns a count of the number of times that X is not NULL in a group. The count(*) function (with no arguments) returns the total number of rows in the group.
So, the COUNT
function does not count NULL
so use COUNT(*)
instead of COUNT(y)
.
SELECT y, COUNT(*) AS COUNT
FROM mytable
GROUP BY y
Or you can also use COUNT(x)
like this one.
SELECT y, COUNT(x) AS COUNT
FROM mytable
GROUP BY y
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With