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. ANo two rows have the same non-NULL value in that column
  2. BThe column automatically becomes the primary key of the table
  3. CThe column values are sorted
  4. DThe column cannot be changed after insert
2.Easy

Which operator is used for simple text pattern matching in a WHERE clause?

  1. ALIKE
  2. BSIMILAR
  3. CMATCH
  4. DCONTAINS
3.Easy

What does COMMIT do in a transaction?

  1. ALocks the whole database
  2. BUndoes all changes made since the transaction began
  3. CMakes all changes of the transaction permanent
  4. DStarts a new table
4.Medium

A LEFT JOIN returns:

  1. AAll rows from the left table and matching rows from the right, with NULLs where there is no match
  2. BAll rows from the right table only
  3. COnly matching rows from both tables
  4. DRows that match in neither table
5.Medium

An employees table has a column manager_id that refers to another employee. How do you list each employee next to their manager's name?

  1. AUse GROUP BY manager_id only
  2. BCombine the table with itself using UNION ALL and a WHERE filter
  3. CJoin the employees table with itself using two aliases
  4. DCreate a second copy of the table on disk
6.Medium

The orders table has 5 rows, each referring to an existing customer. The customers table has 3 rows. How many rows does an INNER JOIN on customer_id return?

  1. A3
  2. B5
  3. C15
  4. D8
7.Hard

A table has a million rows and a Boolean column is_active that is TRUE for 95% of them. Why might the optimiser ignore an index on is_active for WHERE is_active = TRUE?

  1. AIndexes cannot be created on Boolean columns
  2. BThe optimiser never uses indexes in WHERE clauses
  3. CTRUE is treated as NULL by the index
  4. DMost rows match, so a full table scan is cheaper than many index lookups
8.Hard

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

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

Answer key

  1. 1. A · No two rows have the same non-NULL value in that column
  2. 2. A · LIKE
  3. 3. C · Makes all changes of the transaction permanent
  4. 4. A · All rows from the left table and matching rows from the right, with NULLs where there is no match
  5. 5. C · Join the employees table with itself using two aliases
  6. 6. B · 5
  7. 7. D · Most rows match, so a full table scan is cheaper than many index lookups
  8. 8. A · 1, 1, 3

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