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 WHERE country IN ('NL', 'BE', 'DE') do?

  1. AKeeps rows whose country equals any value in the list
  2. BSorts rows in the order of the list
  3. CKeeps rows whose country is not in the list
  4. DKeeps rows whose country value contains all three codes at once
2.Easy

Which keyword gives a column or table a temporary alternative name in a query?

  1. ARENAME
  2. BAS
  3. CALIAS
  4. DNAME
3.Easy

What does COMMIT do in a transaction?

  1. AMakes all changes of the transaction permanent
  2. BLocks the whole database
  3. CUndoes all changes made since the transaction began
  4. DStarts a new table
4.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 child rows that reference it are deleted automatically
  4. DThe parent row is copied to an archive table
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 foreign key column course_id in the students table pointing to courses
  3. CWith a list of course ids stored in one column
  4. DWith a junction table holding foreign keys to both students and courses
6.Medium

Which statement removes all rows of a table, keeps its structure and cannot take a WHERE clause?

  1. ATRUNCATE TABLE
  2. BDROP TABLE
  3. CDELETE FROM with a WHERE clause
  4. DALTER TABLE
7.Hard

What does SELECT day, SUM(amount) OVER (ORDER BY day) FROM sales compute?

  1. AThe total of all amounts on every row
  2. BThe average amount per day
  3. COne row per day with that day's total only
  4. DA running total of amount ordered by day
8.Hard

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

  1. ARANK()
  2. BDENSE_RANK()
  3. CCOUNT()
  4. DROW_NUMBER()

Answer key

  1. 1. A · Keeps rows whose country equals any value in the list
  2. 2. B · AS
  3. 3. A · Makes all changes of the transaction permanent
  4. 4. C · The child rows that reference it are deleted automatically
  5. 5. D · With a junction table holding foreign keys to both students and courses
  6. 6. A · TRUNCATE TABLE
  7. 7. D · A running total of amount ordered by day
  8. 8. D · ROW_NUMBER()

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