I am referring to the Chinook database, which i am using to learn SQLite. This query retrieves the number of invoices for each CustomerId, as I wanted:
select i.customerid, count(i.invoiceid)
from invoices as i
group by i.customerid
returns:
+------------+--------------------+
| CustomerId | count(i.invoiceid) |
+------------+--------------------+
| 1 | 7 |
| 2 | 7 |
| 3 | 7 |
...
But as i was building a more complex query i observed something that i cannot explain:
select i.customerid, count(i.invoiceid)
from invoices as i, customers as c
group by i.customerid
returns:
+------------+--------------------+
| CustomerId | count(i.invoiceid) |
+------------+--------------------+
| 1 | 413 |
| 2 | 413 |
| 3 | 413 |
...
Turns out 413 = 7 * 59, and 59 is the number of distinct CustomerID's. There must be some fundamental SQL behavior that i am misunderstanding here, because I would expect no difference by adding "customers as c" in the "from" clause, since I am not using it yet. Can anyone enlighten me here on what is happening?