SQL & Databases·Database skills from the start

Debugging queries

Systematically narrow down and diagnose queries that return the wrong rows or none at all.

Beginner 10 min read Lesson 6 of 480 Challenge included
What you will be able to do
  • Narrow results with a WHERE clause
  • Read EXPLAIN output to understand query plans
  • Find duplicate rows with GROUP BY and HAVING
  • Use IS NULL correctly and limit rows while exploring

In plain English

When a query returns the wrong results, the first step is to narrow it down by adding a WHERE clause so you only look at the rows you care about. Use EXPLAIN to see how the database plans to run your query, which helps you spot slow full-table scans. To find duplicate rows, group by the columns that should be unique and keep only the groups that appear more than once. Remember that NULL is not equal to anything, so checking for missing values means using IS NULL rather than = NULL. While exploring, add a LIMIT so you only fetch a small sample until you are sure the query is right.

Step by step

  1. 1Add a WHERE clause to shrink the result down to the rows you care about.
  2. 2Run EXPLAIN to see whether the database is scanning a whole table.
  3. 3Use GROUP BY with HAVING COUNT(*) > 1 to surface duplicate rows.
  4. 4Test for missing values with IS NULL and IS NOT NULL.
  5. 5Add LIMIT while exploring, then remove it for the final query.
debug.sqlSQL
CREATE TABLE orders (
  id SERIAL PRIMARY KEY,
  customer TEXT NOT NULL,
  total NUMERIC(10, 2) NOT NULL,
  status TEXT
);

SELECT id, customer, total
FROM orders
WHERE total > 100
LIMIT 20;

EXPLAIN SELECT * FROM orders WHERE customer = 'Ada';

SELECT customer, COUNT(*)
FROM orders
GROUP BY customer
HAVING COUNT(*) > 1;

SELECT id, customer, status
FROM orders
WHERE status IS NULL;

Common mistakes

  • Comparing a value to NULL with = instead of IS NULL, which returns no rows.
  • Scanning a huge result set while exploring instead of limiting it first.
  • Forgetting to add a WHERE clause and editing far more rows than intended.
  • Searching for duplicate rows without knowing which columns should be unique.
Your turn

Find orders missing a status

Easy

Return the id and customer of every order that has no status, ordered by id. The orders table has id, customer, and status columns.

SQL · starter
SELECT id, customer
FROM orders
Check your understanding before moving on.
Knowledge checkQuestion 1

Which clause should you add first to narrow down a misbehaving query?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Joins
Lesson 6 of 480 · 0% of course