Skip to main content

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).

pip install sqlanswer

Show a query as a table

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

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

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 has SQL challenges you can solve right in your browser.

License

MIT

Metadata

Release files for sqlanswer 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlanswer 0.1.0
File Size Uploaded Provenance Actions
sqlanswer-0.1.0.tar.gz 7.7 kB Without provenance Details for sqlanswer-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sqlanswer 0.1.0
File Interpreter ABI Platform Actions
sqlanswer-0.1.0-py3-none-any.whl Python 3 none any Details for sqlanswer-0.1.0-py3-none-any.whl

Total release size: 14.8 kB

Release files / sqlanswer-0.1.0.tar.gz

Download URL sqlanswer-0.1.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
175cafa9dcb4c22f2cc3be706c48cc8a550f7d16d03943ca4cfa512c9f3b3b71
BLAKE2b-256 checksum
How to use checksums
bbe40aeb5d49ff5ac15e0e9280e40df48427821d283bac626dd6367f01614a33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Release files / sqlanswer-0.1.0-py3-none-any.whl

Download URL sqlanswer-0.1.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0bd965d6b37275f5f70339381e65bad71370888d89beed054a8647fe02fd15a5
BLAKE2b-256 checksum
How to use checksums
9ac23257a7d623ea28f431ef3f8f1598ddb16c5e829970c22e0ff07133974c99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Security for sqlanswer 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page