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

In a LIKE pattern, what does the _ (underscore) wildcard stand for?

  1. AA space character
  2. BExactly one character
  3. CAny letter but no digits
  4. DZero or more characters
2.Easy

Which statement deletes a whole table, including its structure?

  1. ADELETE FROM
  2. BDROP TABLE
  3. CREMOVE TABLE
  4. DTRUNCATE ROWS
3.Easy

What is a composite key?

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

What does the functional dependency A → B mean?

  1. AA is a foreign key referencing B
  2. BA and B are both primary keys
  3. CEach value of B is associated with exactly one value of A
  4. DEach value of A is associated with exactly one value of B
5.Medium

Which query returns the number of orders per customer?

  1. ASELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id
  2. BSELECT DISTINCT COUNT(*) FROM orders
  3. CSELECT customer_id, COUNT(*) FROM orders ORDER BY customer_id
  4. DSELECT customer_id FROM orders WHERE COUNT(*) > 0
6.Medium

What does EXCEPT (called MINUS in Oracle) return?

  1. ARows of the second query that do not appear in the first one
  2. BRows of the first query that do not appear in the second
  3. CAll rows from both queries without duplicates
  4. DRows that appear in both queries
7.Hard

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

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

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

  1. AAn error because subqueries are not allowed in WHERE
  2. BStudents with the highest grade only
  3. CStudents with a grade above the average grade
  4. DThe average grade of all students

Answer key

  1. 1. B · Exactly one character
  2. 2. B · DROP TABLE
  3. 3. C · A key made up of two or more columns
  4. 4. D · Each value of A is associated with exactly one value of B
  5. 5. A · SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id
  6. 6. B · Rows of the first query that do not appear in the second
  7. 7. C · The execution plan chosen by the optimiser, such as scans and join methods
  8. 8. C · Students with a grade above the average grade

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