Trappola
COUNT(*) vs COUNT(colonna)
COUNT(column) salta i NULL; COUNT(*) conta le righe.Per contare le righe, usa COUNT(*). Usa COUNT(column) solo quando intendi "le righe in cui questa colonna è
valorizzata", e COUNT(DISTINCT column) per i valori distinti.
SELECT COUNT(*) AS all_rows,
COUNT(city) AS with_city,
COUNT(DISTINCT city) AS cities
FROM customers;
| all_rows | with_city | cities |
|---|---|---|
| 400 | 382 | 27 |
Esercitati:COUNT(*) or COUNT(city)?