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 UNION do with the results of two SELECT queries?

  1. AJoins them side by side on a key
  2. BKeeps only rows that appear in both
  3. CCombines them into one result and removes duplicate rows
  4. DKeeps only rows that appear in the first query but not the second
2.Easy

What does ROLLBACK do in a transaction?

  1. AUndoes the changes made since the transaction began
  2. BRepeats the last statement
  3. CSaves all changes of the transaction permanently to disk
  4. DDeletes the table
3.Easy

Which statement adds a new column to an existing table?

  1. ACREATE COLUMN
  2. BINSERT COLUMN
  3. CALTER TABLE
  4. DUPDATE TABLE
4.Medium

In relational algebra, the selection operator (σ) corresponds to which SQL clause?

  1. AWHERE
  2. BSELECT list
  3. CGROUP BY
  4. DORDER BY
5.Medium

Which query lists customers who have never placed an order?

  1. ASELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NOT NULL
  2. BSELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL
  3. CSELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id = NULL
  4. DSELECT c.name FROM customers c INNER JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL
6.Medium

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

  1. AAn error in every database
  2. BNo rows, because the comparison is unknown
  3. CAll rows of the table
  4. DOnly the rows where col is NULL, as with IS NULL
7.Hard

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

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

What is a known drawback of decomposing tables into BCNF compared with 3NF?

  1. ABCNF tables cannot have primary keys
  2. BSome functional dependencies may no longer be checkable within a single table
  3. CBCNF allows more redundancy than 3NF
  4. DThe decomposition always loses data when joined back

Answer key

  1. 1. C · Combines them into one result and removes duplicate rows
  2. 2. A · Undoes the changes made since the transaction began
  3. 3. C · ALTER TABLE
  4. 4. A · WHERE
  5. 5. B · SELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL
  6. 6. B · No rows, because the comparison is unknown
  7. 7. A · Every employee row together with the average salary of their department
  8. 8. B · Some functional dependencies may no longer be checkable within a single table

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