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 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
2.Easy

Which statement adds a new column to an existing table?

  1. AINSERT COLUMN
  2. BALTER TABLE
  3. CUPDATE TABLE
  4. DCREATE COLUMN
3.Easy

Which aggregate function returns the average of a numeric column?

  1. AAVG
  2. BSUM
  3. CMEAN
  4. DMEDIAN
4.Medium

An employees table has a column manager_id that refers to another employee. How do you list each employee next to their manager's name?

  1. AUse GROUP BY manager_id only
  2. BCombine the table with itself using UNION ALL and a WHERE filter
  3. CJoin the employees table with itself using two aliases
  4. DCreate a second copy of the table on disk
5.Medium

Which query returns the highest salary in each department?

  1. ASELECT dept, MAX(salary) FROM emp ORDER BY dept
  2. BSELECT MAX(dept), salary FROM emp GROUP BY salary
  3. CSELECT dept, MAX(salary) FROM emp GROUP BY dept
  4. DSELECT dept, salary FROM emp WHERE salary = MAX(salary)
6.Medium

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

  1. ASELECT list
  2. BORDER BY
  3. CWHERE
  4. DGROUP BY
7.Hard

For an equality join between two large tables with no useful indexes, which join algorithm does an optimiser often choose?

  1. ANested loop join with index lookups
  2. BBitmap join on the primary key
  3. CCross join
  4. DHash join
8.Hard

What does a FULL OUTER JOIN return?

  1. AAll rows from both tables, matched where possible and NULL-filled otherwise
  2. BOnly matching rows
  3. CThe Cartesian product, pairing each row of one table with every row of the other
  4. DOnly unmatched rows

Answer key

  1. 1. A · Undoes the changes made since the transaction began
  2. 2. B · ALTER TABLE
  3. 3. A · AVG
  4. 4. C · Join the employees table with itself using two aliases
  5. 5. C · SELECT dept, MAX(salary) FROM emp GROUP BY dept
  6. 6. C · WHERE
  7. 7. D · Hash join
  8. 8. A · All rows from both tables, matched where possible and NULL-filled otherwise

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