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

A customer can place many orders, but each order belongs to exactly one customer. What type of relationship is this?

  1. AMany-to-many
  2. BOne-to-many
  3. CZero-to-zero
  4. DOne-to-one
2.Easy

Which rows does WHERE price BETWEEN 10 AND 20 select?

  1. ARows with price equal to 10 or 20 only
  2. BRows with price from 10 to 20, including 10 and 20
  3. CRows with price above 10 and below 20, excluding 10 and 20
  4. DRows with price outside the range 10 to 20
3.Easy

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

  1. AZero or more characters
  2. BA space character
  3. CAny letter but no digits
  4. DExactly one character
4.Medium

A foreign key is declared with ON DELETE CASCADE. What happens when a parent row is deleted?

  1. AThe child rows that reference it are deleted automatically
  2. BThe parent row is copied to an archive table
  3. CThe delete is always rejected
  4. DThe foreign key in the child rows is set to NULL automatically
5.Medium

Which statement removes all rows of a table, keeps its structure and cannot take a WHERE clause?

  1. ATRUNCATE TABLE
  2. BALTER TABLE
  3. CDROP TABLE
  4. DDELETE FROM with a WHERE clause
6.Medium

A LEFT JOIN returns:

  1. AOnly matching rows from both tables
  2. BAll rows from the left table and matching rows from the right, with NULLs where there is no match
  3. CAll rows from the right table only
  4. DRows that match in neither table
7.Hard

What does a FULL OUTER JOIN return?

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

Which SQL feature is best suited to finding every employee below a manager in an organisation tree of unknown depth?

  1. AA single self-join of the employees table on manager_id
  2. BA recursive common table expression (WITH RECURSIVE)
  3. CA GROUP BY on manager_id
  4. DA UNIQUE constraint on manager_id

Answer key

  1. 1. B · One-to-many
  2. 2. B · Rows with price from 10 to 20, including 10 and 20
  3. 3. D · Exactly one character
  4. 4. A · The child rows that reference it are deleted automatically
  5. 5. A · TRUNCATE TABLE
  6. 6. B · All rows from the left table and matching rows from the right, with NULLs where there is no match
  7. 7. D · All rows from both tables, matched where possible and NULL-filled otherwise
  8. 8. B · A recursive common table expression (WITH RECURSIVE)

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