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 COMMIT do in a transaction?

  1. AUndoes all changes made since the transaction began
  2. BStarts a new table
  3. CMakes all changes of the transaction permanent
  4. DLocks the whole database
2.Easy

How does UNION ALL differ from UNION?

  1. AUNION ALL keeps duplicate rows
  2. BUNION ALL only works on one table
  3. CUNION ALL removes duplicate rows
  4. DUNION ALL sorts the result
3.Easy

Which aggregate function returns the average of a numeric column?

  1. AMEAN
  2. BSUM
  3. CMEDIAN
  4. DAVG
4.Medium

Why does SELECT price * 2 AS double_price FROM t WHERE double_price > 10 fail in most databases?

  1. AArithmetic is not allowed in the SELECT list
  2. BWHERE is evaluated before the SELECT list, so the alias does not exist yet
  3. CWHERE can only compare text columns
  4. DAliases may not contain an underscore
5.Medium

Students can take many courses and courses have many students. How is this modelled in tables?

  1. ABy merging students and courses into one table
  2. BWith a junction table holding foreign keys to both students and courses
  3. CWith a foreign key column course_id in the students table pointing to courses
  4. DWith a list of course ids stored in one column
6.Medium

What is a database trigger?

  1. AA type of index that speeds up searches
  2. BCode that runs automatically when a specified event such as INSERT, UPDATE or DELETE occurs on a table
  3. CA query that a user runs by hand
  4. DA scheduled backup of the database
7.Hard

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

  1. A1, 2, 3
  2. B1, 1, 3
  3. C2, 2, 3
  4. D1, 1, 2
8.Hard

What is the result of SELECT COUNT(col) FROM t if all values in col are NULL and the table has 10 rows?

  1. AAn error
  2. B0
  3. C10
  4. DNULL

Answer key

  1. 1. C · Makes all changes of the transaction permanent
  2. 2. A · UNION ALL keeps duplicate rows
  3. 3. D · AVG
  4. 4. B · WHERE is evaluated before the SELECT list, so the alias does not exist yet
  5. 5. B · With a junction table holding foreign keys to both students and courses
  6. 6. B · Code that runs automatically when a specified event such as INSERT, UPDATE or DELETE occurs on a table
  7. 7. B · 1, 1, 3
  8. 8. B · 0

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