Databases & SQL worksheet with answers

Free printable Databases & SQL worksheet: 8 problems from easy to exam level, with an answer key. Print it or save it as a PDF.

For teachers: free to print, copy and use in class. Every new worksheet has new values and its own answer key. A link to ExamProblems on your course page is appreciated.

Name: ____________________   Date: ____________

1.Easy

An INNER JOIN returns:

  1. AAll rows from both tables
  2. BOnly rows with matching values in both tables
  3. CAll rows from the left table, with NULLs where there is no match
  4. DRows that do not match
2.Easy

In PostgreSQL and MySQL, which clause returns only the first 10 rows of a result?

  1. ATOP ROWS 10
  2. BFIRST 10 ONLY
  3. CLIMIT 10
  4. DMAX 10
3.Easy

What does WHERE country IN ('NL', 'BE', 'DE') do?

  1. AKeeps rows whose country is not in the list
  2. BSorts rows in the order of the list
  3. CKeeps rows whose country value contains all three codes at once
  4. DKeeps rows whose country equals any value in the list
4.Medium

Which query lists customers who have never placed an order?

  1. ASELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NOT NULL
  2. BSELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL
  3. CSELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id = NULL
  4. DSELECT c.name FROM customers c INNER JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL
5.Medium

Which query returns the number of different cities in the customers table?

  1. ASELECT COUNT(city) FROM customers GROUP BY city
  2. BSELECT DISTINCT COUNT(city) FROM customers
  3. CSELECT SUM(DISTINCT city) FROM customers
  4. DSELECT COUNT(DISTINCT city) FROM customers
6.Medium

What does SELECT name, CASE WHEN grade >= 5.5 THEN 'pass' ELSE 'fail' END FROM results return?

  1. AAn error because CASE is not allowed in SELECT
  2. BThe number of students who passed and failed
  3. CEach name with the label 'pass' or 'fail' depending on the grade
  4. DOnly the students who passed
7.Hard

Why might a query with WHERE YEAR(order_date) = 2025 not use an index on order_date?

  1. AApplying a function to the column prevents a simple index range lookup
  2. BYEAR is not valid SQL
  3. CIndexes never work with dates
  4. DAn index on order_date is only used for ORDER BY clauses, never for WHERE
8.Hard

There is a composite index on (last_name, first_name). Which condition can use it most effectively?

  1. AWHERE first_name = 'Eva'
  2. BWHERE UPPER(first_name) = 'EVA'
  3. CWHERE first_name LIKE '%a'
  4. DWHERE last_name = 'Jansen'

Answer key

  1. 1. B · Only rows with matching values in both tables
  2. 2. C · LIMIT 10
  3. 3. D · Keeps rows whose country equals any value in the list
  4. 4. B · SELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL
  5. 5. D · SELECT COUNT(DISTINCT city) FROM customers
  6. 6. C · Each name with the label 'pass' or 'fail' depending on the grade
  7. 7. A · Applying a function to the column prevents a simple index range lookup
  8. 8. D · WHERE last_name = 'Jansen'

Full step-by-step solutions: practise this topic on examproblems.com.