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

What does ROLLBACK do in a transaction?

  1. AUndoes the changes made since the transaction began
  2. BSaves all changes of the transaction permanently to disk
  3. CRepeats the last statement
  4. DDeletes the table
2.Easy

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

  1. AAny sequence of zero or more characters
  2. BExactly one character in that position of the string
  3. CThe end of the string
  4. DAny single digit
3.Easy

A customer can place many orders, but each order belongs to exactly one customer. What type of relationship is this?

  1. AZero-to-zero
  2. BMany-to-many
  3. COne-to-many
  4. DOne-to-one
4.Medium

Third normal form (3NF) removes:

  1. ATransitive dependencies between non-key attributes
  2. BComposite keys
  3. CAtomic values
  4. DRedundant indexes on columns that are not part of a key
5.Medium

Second normal form (2NF) removes:

  1. AForeign keys
  2. BPartial dependencies of non-key columns on part of a composite key
  3. CAll NULL values
  4. DTransitive dependencies between non-key columns via another non-key column
6.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 = NULL
  3. CSELECT c.name FROM customers c INNER JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL
  4. DSELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL
7.Hard

Why do many DBMSs use strict two-phase locking, holding exclusive locks until commit?

  1. AIt allows transactions to read dirty data safely
  2. BIt removes the need for a transaction log
  3. CIt makes deadlocks impossible
  4. DIt prevents cascading aborts, because nobody can read uncommitted writes
8.Hard

Two transactions both read stock = 10, each subtracts 1 and writes the result, giving 9 instead of 8. Which anomaly is this?

  1. ALost update
  2. BDirty read
  3. CDeletion anomaly
  4. DPhantom read

Answer key

  1. 1. A · Undoes the changes made since the transaction began
  2. 2. A · Any sequence of zero or more characters
  3. 3. C · One-to-many
  4. 4. A · Transitive dependencies between non-key attributes
  5. 5. B · Partial dependencies of non-key columns on part of a composite key
  6. 6. D · SELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL
  7. 7. D · It prevents cascading aborts, because nobody can read uncommitted writes
  8. 8. A · Lost update

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