COUNT(*) with and without GROUP BY, no matching rows

Viewed 1901

Consider a relation table (Balance,Customer) with the following records:

screenshot of database table

Now I tried these two queries here:

-- Query 1:
select A.Customer, count(B.Customer)
from account A, account B
where A.balance < B.balance
group by A.Customer;

-- Query 2:
select A.Customer, count(B.Customer)
from account A, account B
where A.balance < B.balance;

The first query gives me no output. With the second query, I am getting an output with count = 0.

In both cases there are no rows satisfying the criteria in the where clause, and hence no rows are returned. Then why is the count function giving an output only in the second case?

2 Answers

An aggregation query that has no group by always returns one row (if it is syntactically correct). The count in such a row would be 0.

An aggregation query with a group by returns one row per group. If there are no groups then there are no rows.

the SQL returns no data because you have no data that satisfy the condition where A.balance

Related