SQLite Forum

Duplicated rows (and missing rows) when SELECTing with ORDER BY
Login
Hi,

I have a SQLite database (created with Python 3.7's `sqlite3` module) with a table called `files` that has several columns, many of them having a `unique` constraint. I think I found a terrible SQLite bug, and it has nothing to do with Python: I can reproduce the bug with the `sqlite3.exe` obtained from the [official download page](https://www.sqlite.org/2021/sqlite-tools-win32-x86-3360000.zip).

Steps to reproduce:

- When I export the `files` table content as CSV file (as described [here](https://www.sqlitetutorial.net/sqlite-tutorial/sqlite-export-csv/)), using a simple `SELECT * FROM files` query, I get a CSV file with 18623 rows (+1 header row), where the values of the rows are unique (following the constraints). Everything looks as expected.
- When I export the `files` table as CSV again, this time using `SELECT * FROM files ORDER BY path ASC` (where `path` is a `TEXT` column with a `unique` constraint), not only does the CSV file suddenly use "pipe" as delimiter (instead of "comma"), but the CSV now only contains 18622 rows (1 row is missing), and actually several entries are complete duplicates. For instance, the 1285'th and 1242'th row in the CSV file have the exact same content (including the primary key ID column).

I'm willing to share the database file, but I won't upload it to the WWW for *everyone* to see, as it contains sensitive information. Feel free to contact me directly, or let me know other ways how to send you the file (in case you need it at all).

Best regards,
Marius