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 constraint makes sure a column can never be left empty (NULL)?

  1. ANOT NULL
  2. BUNIQUE
  3. CCHECK NULL
  4. DDEFAULT
2.Easy

What is a composite key?

  1. AA key that is generated by the database
  2. BA key made up of two or more columns
  3. CA key stored in a separate table
  4. DA key that allows duplicate values
3.Easy

What does a CROSS JOIN return?

  1. AOnly the rows that match on a shared key column
  2. BOnly rows that do not match
  3. CThe rows of the first table only
  4. DEvery combination of rows from the two tables
4.Medium

Which query returns the number of orders per customer?

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

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

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

What does the "A" in ACID stand for?

  1. AAtomicity
  2. BAccuracy
  3. CAvailability
  4. DAggregation
7.Hard

Transaction T1 locks row A and waits for row B, while T2 locks row B and waits for row A. What does a typical DBMS do?

  1. ADetects the deadlock and aborts one transaction as the victim
  2. BLets both transactions wait until one of them commits normally
  3. CCommits both transactions at once
  4. DMerges the two transactions into one
8.Hard

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

  1. A2, 2, 3
  2. B1, 2, 3
  3. C1, 1, 3
  4. D1, 1, 2

Answer key

  1. 1. A · NOT NULL
  2. 2. B · A key made up of two or more columns
  3. 3. D · Every combination of rows from the two tables
  4. 4. D · SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id
  5. 5. C · When the subquery returns at least one row
  6. 6. A · Atomicity
  7. 7. A · Detects the deadlock and aborts one transaction as the victim
  8. 8. C · 1, 1, 3

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