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 candidate key?

  1. AA column that is waiting to become a foreign key
  2. BA set of columns shared between two tables to link their rows
  3. CAny column that contains NULL values
  4. DA minimal set of columns that uniquely identifies each row
2.Easy

What does ROLLBACK do in a transaction?

  1. ADeletes the table
  2. BUndoes the changes made since the transaction began
  3. CSaves all changes of the transaction permanently to disk
  4. DRepeats the last statement
3.Easy

Which statement removes rows from a table?

  1. ATRUNCATE COLUMN
  2. BDROP
  3. CREMOVE
  4. DDELETE
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 parent row is copied to an archive table
  3. CThe child rows that reference it are deleted automatically
  4. DThe delete is always rejected
5.Medium

Which statement about HAVING is correct?

  1. AIt can use aggregate functions to filter groups
  2. BIt must come before WHERE in the SELECT statement
  3. CIt cannot be used with GROUP BY
  4. DIt sorts the results
6.Medium

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

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

What does SELECT name, dept, AVG(salary) OVER (PARTITION BY dept) FROM emp return?

  1. AOne row per department with its average salary
  2. BOnly employees who earn more than their department average
  3. CEvery employee row together with the average salary of their department
  4. DAn error because name is not in a GROUP BY
8.Hard

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

  1. AStudents with the highest grade only
  2. BThe average grade of all students
  3. CAn error because subqueries are not allowed in WHERE
  4. DStudents with a grade above the average grade

Answer key

  1. 1. D · A minimal set of columns that uniquely identifies each row
  2. 2. B · Undoes the changes made since the transaction began
  3. 3. D · DELETE
  4. 4. C · The child rows that reference it are deleted automatically
  5. 5. A · It can use aggregate functions to filter groups
  6. 6. C · With a junction table holding foreign keys to both students and courses
  7. 7. C · Every employee row together with the average salary of their department
  8. 8. D · Students with a grade above the average grade

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