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 a UNIQUE constraint on a column guarantee?

  1. AThe column values are sorted
  2. BThe column cannot be changed after insert
  3. CNo two rows have the same non-NULL value in that column
  4. DThe column automatically becomes the primary key of the table
2.Easy

GRANT and REVOKE belong to which part of SQL?

  1. AData Definition Language (DDL)
  2. BData Manipulation Language (DML)
  3. CData Control Language (DCL)
  4. DData Query Language (DQL)
3.Easy

Which keyword gives a column or table a temporary alternative name in a query?

  1. AAS
  2. BRENAME
  3. CNAME
  4. DALIAS
4.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
5.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 NULL
  2. BSELECT c.name FROM customers c INNER 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 LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NOT NULL
6.Medium

When is WHERE EXISTS (subquery) true for a row?

  1. AWhen the subquery returns at least one row
  2. BWhen the subquery returns exactly one row
  3. CWhen the subquery returns a number greater than zero
  4. DWhen the subquery returns no NULL values
7.Hard

An app runs UPDATE item SET qty = 4, version = 8 WHERE id = 1 AND version = 7, and reports a conflict if no row was updated. What technique is this?

  1. AStrict two-phase locking with row locks
  2. BDatabase sharding
  3. COptimistic concurrency control
  4. DWrite-ahead logging
8.Hard

Two doctors on call each check that the other is still on call, then both sign off at the same time. Which anomaly can snapshot isolation still allow here?

  1. ADirty write
  2. BWrite skew
  3. CNon-repeatable read within one transaction
  4. DDirty read

Answer key

  1. 1. C · No two rows have the same non-NULL value in that column
  2. 2. C · Data Control Language (DCL)
  3. 3. A · AS
  4. 4. B · The child rows that reference it are deleted automatically
  5. 5. A · SELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL
  6. 6. A · When the subquery returns at least one row
  7. 7. C · Optimistic concurrency control
  8. 8. B · Write skew

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