Metadata-Version: 2.4
Name: sqlanswer
Version: 0.1.0
Summary: Print SQL results as clean tables and check if two queries return the same rows. Handy for SQL practice.
Author: SQLCrab
License-Expression: MIT
Project-URL: Homepage, https://sqlcrab.com
Project-URL: SQL exercises, https://sqlcrab.com
Keywords: sql,sqlite,table,compare,practice,learning,education
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: SQL
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Education
Classifier: Topic :: Database
Classifier: Topic :: Education
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# sqlanswer

Print SQL query results as clean tables, and check whether two queries return the same rows.

Handy when you're practicing SQL from Python: run your query, see the result clearly, then check it against a reference answer. No dependencies. Works with `sqlite3` out of the box and with any DB-API connection (psycopg, mysql-connector, duckdb and others).

```bash
pip install sqlanswer
```

## Show a query as a table

```python
import sqlite3
from sqlanswer import show

conn = sqlite3.connect("shop.db")
show(conn, "SELECT name, price, category FROM products")
```

```
+-------+-------+-----------+
| name  | price | category  |
+-------+-------+-----------+
| Pen   |   1.5 | office    |
| Desk  |   120 | furniture |
| Chair | 75.25 | furniture |
| Lamp  |    30 | NULL      |
+-------+-------+-----------+
4 rows
```

Numbers are right-aligned, `NULL` is shown as `NULL`, long text is cut off, and large results show the first 50 rows (`limit=None` shows everything). You can also pass a file path instead of a connection: `show("shop.db", "SELECT ...")`.

## Check your answer against another query

```python
from sqlanswer import compare

compare(
    conn,
    "SELECT name FROM products WHERE price > 10",   # your query
    "SELECT name FROM products WHERE price > 50",   # expected answer
)
```

```
DIFFERENT: Rows differ: 0 missing, 1 extra
Extra rows (returned but not expected):
+------+
| name |
+------+
| Lamp |
+------+
```

By default `compare` works the way most SQL exercises are graded:

- **Row order doesn't matter.** Use `ordered=True` when the task asks for a specific `ORDER BY`.
- **Duplicate rows do matter.** Forgetting `DISTINCT` counts as wrong.
- **Column names don't matter.** Use `check_column_names=True` when aliases are part of the task.
- **Floats are rounded** to 6 digits, so `0.1 + 0.2` matches `0.3`.

`compare` returns a result that is truthy when the queries match, with `.missing`, `.extra` and `.reason` for details.

## From the command line

```bash
python -m sqlanswer shop.db "SELECT * FROM products"
python -m sqlanswer shop.db "SELECT name FROM products WHERE price > 10" \
    --expected "SELECT name FROM products WHERE price > 50"
```

The compare command exits with code 1 when the results differ, so you can use it in scripts.

## Want SQL exercises to practice on?

[SQLCrab](https://sqlcrab.com) has SQL challenges you can solve right in your browser.

## License

MIT
