SQL · Python · Test Automation
SQL vs Python for Automation Testers: A Practical Comparison
What each language is for
SQL is a query language for relational databases. A tester uses it to look at the data an application stored: did the order get saved, with the right amount, status and customer? It is also used to create or reset test data.
Python is a general-purpose programming language. In automation it runs the test suite (usually with pytest), drives the browser (Selenium or Playwright), calls APIs (requests), reads files, and reports results. It can also open a database connection and run SQL.
| Task in a test project | SQL | Python |
|---|---|---|
| Check a row was written after a UI or API action | Writes the query | Runs the query and asserts on the result |
| Find bad data (duplicates, orphans, nulls) | Main tool | Schedules and reports |
| Seed or reset test data | INSERT / DELETE scripts | Fixtures that run them before each test |
| Click through a web page | Not possible | Selenium, Playwright |
| Call a REST API and check the response | Not possible | requests, httpx |
| Loops, conditions, retries, file handling | Very limited | Core language features |
| Run in CI and produce a report | Not on its own | pytest with JUnit XML or HTML reports |
SQL on its own: checking the data
Suppose a test environment has these two tables:
CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
amount REAL,
status TEXT
);
INSERT INTO customers (id, name) VALUES (1, 'asha'), (2, 'ravi');
INSERT INTO orders (customer_id, amount, status) VALUES
(1, 499.0, 'PAID'),
(2, 1299.0, 'PAID'),
(1, 250.0, 'FAILED'),
(9, 100.0, 'PAID');A summary query a tester might run after a batch job:
SELECT status, COUNT(*) AS n, SUM(amount) AS total
FROM orders
GROUP BY status
ORDER BY status;| status | n | total |
|---|---|---|
| FAILED | 1 | 250.0 |
| PAID | 3 | 1898.0 |
And a data-integrity check that finds orders pointing at a customer that does not exist:
SELECT o.id
FROM orders o
LEFT JOIN customers c ON c.id = o.customer_id
WHERE c.id IS NULL;This returns order 4, whose customer_id is 9. Joins, GROUP BY and NULL handling cover most of what testers need; common SQL interview queries is a good set to practise.
Python running the same checks as tests
Run by hand, a query is a one-off check. Wrapped in a pytest test, it runs on every build. This example uses Python's built-in sqlite3 so it runs anywhere; with MySQL or PostgreSQL you would swap in that database's driver.
# test_orders.py (pip install pytest)
import sqlite3
import pytest
SCHEMA = """ ...the CREATE TABLE and INSERT statements above... """
@pytest.fixture
def db():
conn = sqlite3.connect(":memory:")
conn.executescript(SCHEMA)
yield conn
conn.close()
def test_paid_total(db):
total = db.execute(
"SELECT SUM(amount) FROM orders WHERE status = ?", ("PAID",)
).fetchone()[0]
assert total == 1898.0
def test_no_orphan_orders(db):
orphans = db.execute("""
SELECT o.id FROM orders o
LEFT JOIN customers c ON c.id = o.customer_id
WHERE c.id IS NULL
""").fetchall()
assert orphans == [], f"orders without a customer: {orphans}"Running pytest -q ends with:
FAILED test_orders.py::test_no_orphan_orders - AssertionError: orders without a customer: [(4,)]
1 failed, 1 passed(pytest also prints the run time.) The failing test is the point: it caught the bad row. Note the ? placeholder in the first test. Always pass values as parameters rather than building SQL strings with f-strings, even in test code.
The common real-world pattern: UI or API action, then a database check
- Python calls the API or drives the browser to place an order.
- Python runs a SQL query to confirm the order row exists with the right values.
- A fixture cleans up the test data afterwards.
import requests
def test_create_order_is_saved(db_conn, base_url):
r = requests.post(f"{base_url}/api/orders",
json={"customer_id": 1, "amount": 499.0})
assert r.status_code == 201
order_id = r.json()["id"]
row = db_conn.execute(
"SELECT amount, status FROM orders WHERE id = ?", (order_id,)
).fetchone()
assert row == (499.0, "PAID")Here db_conn and base_url are fixtures you define for your environment. The API side of this is covered in API testing questions and sample test cases, and the browser side in getting started with Playwright.
Which to learn first
Most testers benefit from basic SQL early, because even manual testers verify data. It is a small language and a few weeks of practice covers SELECT, joins, GROUP BY and subqueries. Python comes next once you move to automation: variables, functions, loops, lists and dictionaries, then pytest and one browser or API library. Many job descriptions for automation roles list both. If your team's framework is in Java instead, the same split applies with JDBC in place of sqlite3; Java vs Python for automation testing compares the two languages.
Limits to keep in mind
- Database checks need read access to the test database. In some companies only APIs are exposed to testers.
- Do not hard-code IDs; query for the record your test created.
- Never run DELETE or UPDATE scripts against shared or production databases.
- Asserting on the database can couple tests to the schema. Prefer API assertions when the API exposes the same information, and use SQL for what it does not show.
A broader view of the skills an automation tester builds is in the automation tester roadmap.