COUNT() aggregate function in SQL - sql

What is meant by putting * in COUNT() aggregate function as in:
COUNT(*) > 1
Does it counts all values of all the columns and see if they are greater than 1, which will be of-course be greater than 1 and quite understood.
But what is the point here?

COUNT() counts the number of rows. You can find the difference if you have NULL value.
SELECT (*) -- will include null value
SELECT COUNT(colName) -- will only count NON NULL
consider this,
colA, colB
1 , A
2 , B
3 , NULL
SELECT COUNT(*) => returns 3
SELECT COUNT(colB) => returns 2
SQLFiddle Demo

* is simply a placeholder. If you put Count(1) it will act just the same. COUNT() is useful for counting distinct values: Count(distinct col)

Related

Oracle SQL how to find count less than avg

my code is like :
SELECT
number,
name,
count(*) as "the number of correct answer"
FROM
table1 NATURAL JOIN table2
WHERE
answer = 'T'
GROUP BY
number,
name
HAVING
count(*) < avg(count(*))
ORDER BY
count(*);
Here I want to find the group with count less than the average number of count for each group, but here I failed to use HAVING or WHERE, could anyone help me?
How can I only select the 1 name1 2 since avg of count is (2+6+7)/3 = 5 and only 2 is less than avg.
number name count
1 name1 2
2 name2 6
3 name3 7
I would advise you to never use natural joins. They obfuscate the query and make the query a maintenance nightmore.
You can use window functions:
SELECT t.*
FROM (SELECT number, name,
COUNT(*) as num_correct,
AVG(COUNT(*)) OVER () as avg_num_correct
FROM table1 JOIN
table2
USING (?). -- be explicit about the column name
WHERE answer = 'T'
GROUP BY number, name
) t
WHERE num_correct < avg_num_correct;
As with your version of the query, this filters out all groups that have no correct answers.
I would place your current query logic into a CTE, and then tag on the average count in the process:
WITH cte AS (
SELECT number, name, COUNT(*) AS cnt,
AVG(COUNT(*)) OVER () AS avg_cnt
FROM table1
NATURAL JOIN table2
WHERE answer = 'T'
GROUP BY number, name
)
SELECT number, name, cnt AS count
FROM cte
WHERE cnt < avg_cnt;
Here we are using the AVG() function as an analytic function, with the window being the entire aggregated table. This means it will find the average of the counts per group, across all groups (after aggregation). Window functions (almost) always evaluate last.

How to take count of distinct rows which have a specific column with NULL values is all rows

I have a table CodeResult as follows:
Here we can notice that Code 123 alone has a Code2, that has a value in Result. I want to take a count of distinct Codes that has no values at all in Result. Which means, in this example, I should get 2.
I do not want to use group by clause because it will slow down the query.
Below code gives wrong result:
Select count(distinct code) from CodeResult where Result is Null
One method is two levels of aggregation:
select count(*)
from (select code
from t
group by code
having max(result) is null
) c;
A more clever method doesn't use a subquery. It counts the number of distinct codes and then removes the ones that have a result:
select ( count(distinct code) -
count(distinct case when result is not null then code end )
)
from t;
You simply can't avoid a GROUP BY: In all DBMSs I know, the query plan you get from a:
SELECT DISTINCT a,b,c FROM tab; ,
is the same as the one for:
SELECT a,b,c FROM tab GROUP BY a,b,c;
The following query will return each of the Code values for which there are no corresponding non-NULL values in CodeResult:
select distinct Code
from CodeResult as CR
where not exists
( select 42 from CodeResult as iCR where iCR.Code = CR.Code and iCR.CodeResult is not NULL );
Counting the rows is left as an exercise for the reader.

What is the difference between count (*) and count(attribute_name)?

Is there any difference between COUNT(*) and COUNT(attribute_name)?
I used count(attribute_name) as I thought that it would be specific hence the searching process would be easier. Is that true?
It would be great to see any example with sql code with my issue to help me understand better
Imagine this table:
select Count(TelephoneNumber) from Calls -- returns 3
select Count(*) from Calls -- returns 4
count(column_name) also counts duplicate values. Consider:
select Count(TelephoneNumber) from Calls -- returns 4
COUNT(*) counts all the records in the group.
COUNT(column_name) only counts non-null values.
There is also another typical expression, COUNT(DISTINCT column_name), that counts non-null distinct values.
Since you asked for it, here is a demo on DB Fiddlde:
with t as (
select 1 x from dual
union all select 1 from dual
union all select null from dual
)
select count(*), count(x), count(distinct x) from t
COUNT(*) | COUNT(X) | COUNT(DISTINCTX)
-------: | -------: | ---------------:
3 | 2 | 1
COUNT(*) will count all the rows.
COUNT(column) will count non-NULLs only.
Your can use of COUNT(*) or COUNT(column) which should be based on the desired output only.
Consider below Example of employee table
ID Name Description
1 Raji Smart
2 Rahi Positive
3
4 Falle Smart
select count(*) from employee;
Count(*)
4
select count(name) from employee;
Count(Name)
3
count() only counts non-null values. * references the complete row and as such never excludes any rows. count(attribute_name) only counts rows where that column is no null.
So this:
select count(attribute_name)
from the_table
is equivalent to:
select count(*)
from the_table
where attribute_name is not null
The difference is simple: COUNT(*) counts the number of rows produced by the query, whereas COUNT(1) counts the number of 1 values. Note that when you include a literal such as a number or a string in a query, this literal is "appended" or attached to every row that is produced by the FROM clause.
For more detail this link would help you understand.

Calculate difference between two dates and count the number of occurrences

I am trying to calculate the difference between two dates and in the same query trying to count the frequency of occurence.
SELECT DATEDIFF(day,[Date1],[Date2]) AS 'Mydate'
, count ([Mydate])
FROM Table1
group
by 'Mydate'
I am expecting the query to calculate and count but getting an error msg "Each GROUP BY expression must contain at least one column that is not an outer reference."
what I am expecting is, something like this:
Mydate Count
0 5
1 4
2 5
Can someone help me please? Thanks
First of all this doesn't look like MySQL rather SQL Server. Secondly you can't use the alias in Count() function like that. Rather say count(*)
SELECT DATEDIFF(day,[Date1],[Date2]) AS 'Mydate',
Count (*)
FROM Table1
GROUP BY DATEDIFF(day,[Date1],[Date2])
You cannot group by an alias:
SELECT 'Mydate', Cnt
FROM (SELECT DATEDIFF(day, [Date1], [Date2]) AS 'Mydate',
Count(*) Cnt
FROM Table1) T1
GROUP BY 'Mydate'

COUNT() doesn't work with GROUP BY?

SELECT COUNT(*) FROM table GROUP BY column
I get the total number of rows from table, not the number of rows after GROUP BY. Why?
Because that is how group by works. It returns one row for each identified group of rows in the source data. In this case, it will give the count for each of those groups.
To get what you want:
select count(distinct column)
from table;
EDIT:
As a slight note, if column can be NULL, then the real equivalent is:
select (count(distinct column) +
max(case when column is null then 1 else 0 end)
)
from table;
Try this:
SELECT COUNT(*), column
FROM table
GROUP BY column