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. ADEFAULT
  2. BCHECK NULL
  3. CUNIQUE
  4. DNOT NULL
2.Easy

Which SQL keyword removes duplicate rows from a result?

  1. AUNIQUE
  2. BDIFFERENT
  3. CDISTINCT
  4. DSINGLE
3.Easy

In a LIKE pattern, what does the _ (underscore) wildcard stand for?

  1. AAny letter but no digits
  2. BZero or more characters
  3. CExactly one character
  4. DA space character
4.Medium

What does EXCEPT (called MINUS in Oracle) return?

  1. ARows of the second query that do not appear in the first one
  2. BRows that appear in both queries
  3. CAll rows from both queries without duplicates
  4. DRows of the first query that do not appear in the second
5.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 delete is always rejected
  3. CThe parent row is copied to an archive table
  4. DThe child rows that reference it are deleted automatically
6.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. ACombine the table with itself using UNION ALL and a WHERE filter
  2. BUse GROUP BY manager_id only
  3. CCreate a second copy of the table on disk
  4. DJoin the employees table with itself using two aliases
7.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
8.Hard

Which window function assigns 1, 2, 3, ... to rows within each partition without ties?

  1. ACOUNT()
  2. BRANK()
  3. CROW_NUMBER()
  4. DDENSE_RANK()

Answer key

  1. 1. D · NOT NULL
  2. 2. C · DISTINCT
  3. 3. C · Exactly one character
  4. 4. D · Rows of the first query that do not appear in the second
  5. 5. D · The child rows that reference it are deleted automatically
  6. 6. D · Join the employees table with itself using two aliases
  7. 7. B · Write skew
  8. 8. C · ROW_NUMBER()

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