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

Which keyword sorts the result of a query?

  1. ASORT
  2. BGROUP BY
  3. CARRANGE
  4. DORDER BY
2.Easy

Which SQL statement retrieves data from a table?

  1. ASELECT
  2. BINSERT
  3. CUPDATE
  4. DCREATE
3.Easy

In a LIKE pattern, what does the % wildcard stand for?

  1. AExactly one character in that position of the string
  2. BThe end of the string
  3. CAny sequence of zero or more characters
  4. DAny single digit
4.Medium

Which query returns the number of orders per customer?

  1. ASELECT customer_id FROM orders WHERE COUNT(*) > 0
  2. BSELECT DISTINCT COUNT(*) FROM orders
  3. CSELECT customer_id, COUNT(*) FROM orders ORDER BY customer_id
  4. DSELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id
5.Medium

What does a common table expression (WITH name AS (...)) provide?

  1. AA new database user
  2. BA named temporary result that can be used within one statement
  3. CAn index on a column
  4. DA permanent table that stays stored on disk until it is dropped
6.Medium

A foreign key is declared with ON DELETE CASCADE. What happens when a parent row is deleted?

  1. AThe foreign key in the child rows is set to NULL automatically
  2. BThe child rows that reference it are deleted automatically
  3. CThe delete is always rejected
  4. DThe parent row is copied to an archive table
7.Hard

Why is NATURAL JOIN considered risky in production queries?

  1. AIt always returns a Cartesian product
  2. BIt joins on every column with the same name, so a new column can silently change the result
  3. CIt ignores primary and foreign keys and keeps only NULL rows
  4. DIt is not supported by any database
8.Hard

Scores are 90, 90 and 80. Which values does RANK() OVER (ORDER BY score DESC) give?

  1. A1, 2, 3
  2. B1, 1, 2
  3. C1, 1, 3
  4. D2, 2, 3

Answer key

  1. 1. D · ORDER BY
  2. 2. A · SELECT
  3. 3. C · Any sequence of zero or more characters
  4. 4. D · SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id
  5. 5. B · A named temporary result that can be used within one statement
  6. 6. B · The child rows that reference it are deleted automatically
  7. 7. B · It joins on every column with the same name, so a new column can silently change the result
  8. 8. C · 1, 1, 3

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