Notes
Why SQL
Testers use SQL for four jobs. To verify that an action stored the right data, like stock going down after an order. To set up a test, by finding data that fits it, like a product with low stock. To investigate whether a bug lives in the page, the API or the data. And to attach evidence to a bug report. Almost all of it is reading with SELECT. In ShopRung, the console at slash S Q L only allows reading, so you cannot break anything.
- verify: an action stored the right data (ORD-2 stock)
- set up: find test data (a product with low stock)
- investigate: is the bug in the UI, the API or the data?
- evidence: a query result in the bug report
- mostly SELECT, read-only
The tables
First, know the tables. ShopRung has users, products, coupons, carts and cart items, orders and order items. Money is stored in whole cents, so fifty dollars is five thousand. The links are the id columns: an order has a user id that points to a user, and each order item has an order id and a product id. Read the schema before you write queries. In a real job, ask for the diagram or run the database's describe command.
users id, email, name, role
products id, sku, name, category,
price_cents, stock
coupons code, kind, amount,
min_subtotal_cents
cart_items user_id, product_id, qty
orders id, user_id, status,
subtotal..total_cents
order_items order_id, product_id,
name, price_cents, qty SELECT
The basic query has four parts. SELECT the columns you want, FROM a table, WHERE a condition, ORDER BY a column. Here: the sku, name and stock of every product with fewer than ten in stock, lowest first. ShopRung returns four rows: the red mug with zero, the Bug Hunter's Diary with three, the headphones with five and the keyboard with eight. That is exactly the test data you need for the out of stock and stock limit cases.
SELECT sku, name, stock FROM products WHERE stock < 10 ORDER BY stock; MG-202 Red Build Mug 0 BK-103 The Bug Hunter's .. 3 HD-403 Noise-Cancelling .. 5 KB-401 Quiet Mechanical .. 8
Filters
Filters combine with AND and OR. BETWEEN gives a range, including both ends. IN matches a list, like orders that are placed or paid. LIKE matches a pattern, where the percent sign means any text, so percent mug percent finds both mugs. And to find empty values, write IS NULL, never equals NULL, which matches nothing. Strings go in single quotes. On a real database with millions of rows, add LIMIT so a query returns quickly.
WHERE category = 'Books'
AND price_cents BETWEEN 1000
AND 2000
WHERE status IN ('PLACED','PAID')
WHERE name LIKE '%Mug%'
WHERE coupon_code IS NULL
-- strings in single quotes
-- NULL needs IS, not =
-- LIMIT 10 for big tables Counting
Aggregate functions summarise rows: COUNT, SUM, MIN, MAX and AVG. With GROUP BY they work per group. Here we count products and find the cheapest price in each category. Electronics and books have three products each, stickers, home and clothing two each, and the cheapest item in the shop is a sticker pack at four ninety nine. Counting is a quick sanity check: the page says three books, and so does the database.
SELECT category, COUNT(*) AS n,
MIN(price_cents) AS cheapest
FROM products
GROUP BY category
ORDER BY n DESC;
Electronics 3 2550
Books 3 1250
Stickers 2 499
Home 2 1500 · Clothing 2 2000 JOIN
Real questions need more than one table. A JOIN combines rows where the ids match. Here we join orders to users on the user id, so each order shows the customer's email. The letters o and u are short names, called aliases. On a fresh ShopRung, both seed orders belong to Alice: order one is delivered with a total of thirty four ninety nine, and order two is shipped with a total of seventy five dollars.
SELECT o.id, u.email, o.status,
o.total_cents
FROM orders o
JOIN users u ON u.id = o.user_id
ORDER BY o.id;
1 alice@shoprung.test
DELIVERED 3499
2 alice@shoprung.test
SHIPPED 7500 LEFT JOIN
A plain join only keeps rows that match on both sides. A LEFT JOIN keeps every row from the left table, and fills the right side with NULL where nothing matches. That makes it the tester's favourite for finding what is missing. Which products were never ordered? Left join products to order items, and keep rows where the order id is NULL. Ten products come back, everything except the green mug and the keyboard, which Alice ordered.
SELECT p.sku FROM products p LEFT JOIN order_items i ON i.product_id = p.id WHERE i.order_id IS NULL; 10 rows: every product except MG-201 and KB-401 INNER JOIN: only matches LEFT JOIN: all left + matches
Data checks
The most powerful queries are checks that should return zero rows. Orders whose total does not equal subtotal minus discount plus shipping, which is price ten. Emails that appear more than once, which account four forbids; HAVING filters groups after counting. Products with negative stock. On a healthy ShopRung, all three return nothing. Run them after a test session, and any row they return is a bug, with the evidence already attached.
-- totals add up (PRICE-10) SELECT id FROM orders WHERE total_cents <> subtotal_cents - discount_cents + shipping_cents; -- duplicate emails (ACC-4) SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1; -- negative stock SELECT sku FROM products WHERE stock<0;
Before and after
The everyday pattern is before and after. Query the stock of the Bug Hunter's Diary: three. Buy one in the shop. Query again: two, as rule order two requires. Now switch on the stock bug and repeat: the order is placed, but the query still says three. You have found the bug and your evidence in one move. This is exactly what you will do in the bug report lab.
SELECT stock FROM products WHERE sku = 'BK-103'; -- 3 -- in the shop: buy 1 Diary SELECT stock FROM products WHERE sku = 'BK-103'; -- 2 ok bug stock-not-reduced on: still 3 → evidence for B-7
Safety
On real systems, some rules. Use a read only account when you can. Never update or delete rows in a shared test database without telling the team, because other people's tests depend on that data. Never touch production data without written permission. Add LIMIT to queries on big tables. Treat personal data with care and look only at what the test needs. And save your useful queries in Git, so the whole team can reuse them.
- use a read-only account
- never UPDATE or DELETE on a shared test DB without telling the team
- never touch production data without written permission
- add LIMIT to big queries
- personal data: look only at what the test needs
- save useful queries in Git
Common mistakes
These mistakes give wrong answers without any error. Writing equals NULL instead of IS NULL. Using an inner join when you needed a left join, so rows silently vanish. Forgetting the join condition, so rows multiply. Comparing cents with dollars. Filtering a count with WHERE instead of HAVING. And trusting a query without checking the number of rows makes sense. If the shop has twelve products and your query says forty, something is wrong.
- = NULL instead of IS NULL
- JOIN when you needed a LEFT JOIN (rows vanish)
- forgetting a join condition (rows multiply)
- comparing cents to dollars
- WHERE on a COUNT (use HAVING)
- trusting one query without checking the row count
Say it like this
Interviewers ask how you use SQL, then give a small task. Say: I use SELECT queries to find test data, to verify that actions stored the right values, and to add evidence to bug reports, and I write checks that should return zero rows, like totals that do not add up or duplicate emails. Be ready for classic tasks: inner versus left join, GROUP BY with HAVING, duplicates, and the second highest salary.
- "I use SELECT queries to set
- up data, verify that actions
- stored the right values, and add evidence to bug reports.
- I write checks that should
- return zero rows, like totals
- that do not add up or duplicate emails. Common asks: JOIN vs LEFT JOIN, GROUP BY with HAVING, the 2nd highest."