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 is a composite key?

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

What is a surrogate key?

  1. AA key made from real-world data such as an email address
  2. BA key that is only used in views
  3. CAn artificial identifier with no business meaning, such as an auto-increment id
  4. DA key that refers to another table
3.Easy

What does WHERE country IN ('NL', 'BE', 'DE') do?

  1. AKeeps rows whose country is not in the list
  2. BSorts rows in the order of the list
  3. CKeeps rows whose country value contains all three codes at once
  4. DKeeps rows whose country equals any value in the list
4.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. ACreate a second copy of the table on disk
  2. BJoin the employees table with itself using two aliases
  3. CCombine the table with itself using UNION ALL and a WHERE filter
  4. DUse GROUP BY manager_id only
5.Medium

Which query returns the number of different cities in the customers table?

  1. ASELECT SUM(DISTINCT city) FROM customers
  2. BSELECT COUNT(DISTINCT city) FROM customers
  3. CSELECT DISTINCT COUNT(city) FROM customers
  4. DSELECT COUNT(city) FROM customers GROUP BY city
6.Medium

Second normal form (2NF) removes:

  1. AAll NULL values
  2. BPartial dependencies of non-key columns on part of a composite key
  3. CForeign keys
  4. DTransitive dependencies between non-key columns via another non-key column
7.Hard

What does the following return: SELECT name FROM students WHERE grade > (SELECT AVG(grade) FROM students)?

  1. AStudents with a grade above the average grade
  2. BStudents with the highest grade only
  3. CThe average grade of all students
  4. DAn error because subqueries are not allowed in WHERE
8.Hard

Which SQL feature is best suited to finding every employee below a manager in an organisation tree of unknown depth?

  1. AA UNIQUE constraint on manager_id
  2. BA single self-join of the employees table on manager_id
  3. CA recursive common table expression (WITH RECURSIVE)
  4. DA GROUP BY on manager_id

Answer key

  1. 1. C · A key made up of two or more columns
  2. 2. C · An artificial identifier with no business meaning, such as an auto-increment id
  3. 3. D · Keeps rows whose country equals any value in the list
  4. 4. B · Join the employees table with itself using two aliases
  5. 5. B · SELECT COUNT(DISTINCT city) FROM customers
  6. 6. B · Partial dependencies of non-key columns on part of a composite key
  7. 7. A · Students with a grade above the average grade
  8. 8. C · A recursive common table expression (WITH RECURSIVE)

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