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

An INNER JOIN returns:

  1. AOnly rows with matching values in both tables
  2. BAll rows from both tables
  3. CRows that do not match
  4. DAll rows from the left table, with NULLs where there is no match
2.Easy

How does UNION ALL differ from UNION?

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

Which aggregate function returns the average of a numeric column?

  1. AMEDIAN
  2. BSUM
  3. CMEAN
  4. DAVG
4.Medium

A view in SQL is:

  1. AA stored query that behaves like a virtual table
  2. BA copy of a table on disk
  3. CA screen in the database client for browsing tables
  4. DAn index type
5.Medium

A table has rows where col is NULL. Which rows does WHERE col = NULL return?

  1. AOnly the rows where col is NULL, as with IS NULL
  2. BAll rows of the table
  3. CNo rows, because the comparison is unknown
  4. DAn error in every database
6.Medium

What is a database trigger?

  1. AA query that a user runs by hand
  2. BA scheduled backup of the database
  3. CCode that runs automatically when a specified event such as INSERT, UPDATE or DELETE occurs on a table
  4. DA type of index that speeds up searches
7.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. BStudents with a grade above the average grade
  3. CAn error because subqueries are not allowed in WHERE
  4. DThe average grade of all students
8.Hard

What does EXPLAIN (or EXPLAIN ANALYZE in PostgreSQL) show for a query?

  1. AThe list of users who ran the query
  2. BA rewrite of the query in relational algebra notation only
  3. CThe execution plan chosen by the optimiser, such as scans and join methods
  4. DThe meaning of each SQL keyword in the query

Answer key

  1. 1. A · Only rows with matching values in both tables
  2. 2. C · UNION ALL keeps duplicate rows
  3. 3. D · AVG
  4. 4. A · A stored query that behaves like a virtual table
  5. 5. C · No rows, because the comparison is unknown
  6. 6. C · Code that runs automatically when a specified event such as INSERT, UPDATE or DELETE occurs on a table
  7. 7. B · Students with a grade above the average grade
  8. 8. C · The execution plan chosen by the optimiser, such as scans and join methods

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