[SQLite] Why more records with/without GROUP BY?
by littlebigman from LinuxQuestions.org on (#5AY4H)
Hello,
This is an SQL(ite) newbie question.
I found data on how people commute in my area.
The database holds two tables: One where the actual data lives, and a second with the familiar, two-column ZIP=city name.
I'd like to know how many people in my hometown (zip 12345) drive to work (mode 3), and where.
For some reason, SQLite doesn't return the same number of rows depending on my (not) using GROUP BY:
Code:SELECT COUNT(commute.ZIP_WORK) FROM commute WHERE commute.ZIP_RESIDENCE=12345 AND commute.MODE=3;
=> 1559 records
SELECT COUNT(commute.ZIP_WORK) FROM commute,zip WHERE commute.ZIP_RESIDENCE=12345 AND commute.MODE=3 AND commute.ZIP_WORK=zip.ZIP;
=> 1559 records
SELECT COUNT(commute.ZIP_WORK) FROM commute,zip WHERE commute.ZIP_RESIDENCE=12345 AND commute.MODE=3 AND commute.ZIP_WORK=zip.ZIP GROUP BY commute.ZIP ORDER BY COUNT(commute.ZIP);
=> 1763 records
SELECT COUNT(commute.ZIP_WORK) FROM commute,zip FROM commute JOIN zip ON commute.ZIP_WORK=zip.ZIP WHERE commute.ZIP_RESIDENCE=12345 AND commute.MODE=3 GROUP BY commute.ZIP ORDER BY COUNT(commute.ZIP);
=> 1763 recordsAny idea why?
Thank you.
Attached Thumbnails


This is an SQL(ite) newbie question.
I found data on how people commute in my area.
The database holds two tables: One where the actual data lives, and a second with the familiar, two-column ZIP=city name.
I'd like to know how many people in my hometown (zip 12345) drive to work (mode 3), and where.
For some reason, SQLite doesn't return the same number of rows depending on my (not) using GROUP BY:
Code:SELECT COUNT(commute.ZIP_WORK) FROM commute WHERE commute.ZIP_RESIDENCE=12345 AND commute.MODE=3;
=> 1559 records
SELECT COUNT(commute.ZIP_WORK) FROM commute,zip WHERE commute.ZIP_RESIDENCE=12345 AND commute.MODE=3 AND commute.ZIP_WORK=zip.ZIP;
=> 1559 records
SELECT COUNT(commute.ZIP_WORK) FROM commute,zip WHERE commute.ZIP_RESIDENCE=12345 AND commute.MODE=3 AND commute.ZIP_WORK=zip.ZIP GROUP BY commute.ZIP ORDER BY COUNT(commute.ZIP);
=> 1763 records
SELECT COUNT(commute.ZIP_WORK) FROM commute,zip FROM commute JOIN zip ON commute.ZIP_WORK=zip.ZIP WHERE commute.ZIP_RESIDENCE=12345 AND commute.MODE=3 GROUP BY commute.ZIP ORDER BY COUNT(commute.ZIP);
=> 1763 recordsAny idea why?
Thank you.
Attached Thumbnails