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

Which statement adds a new column to an existing table?

  1. AUPDATE TABLE
  2. BCREATE COLUMN
  3. CALTER TABLE
  4. DINSERT COLUMN
2.Easy

Which operator is used for simple text pattern matching in a WHERE clause?

  1. ALIKE
  2. BSIMILAR
  3. CCONTAINS
  4. DMATCH
3.Easy

Which keyword sorts the result of a query?

  1. AORDER BY
  2. BSORT
  3. CGROUP BY
  4. DARRANGE
4.Medium

What does INTERSECT return when used between two SELECT queries?

  1. ARows that appear in both results
  2. BThe Cartesian product of the results
  3. CRows in the first result but not the second
  4. DAll rows from both results
5.Medium

A view in SQL is:

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

Students can take many courses and courses have many students. How is this modelled in tables?

  1. AWith a foreign key column course_id in the students table pointing to courses
  2. BWith a junction table holding foreign keys to both students and courses
  3. CBy merging students and courses into one table
  4. DWith a list of course ids stored in one column
7.Hard

A table has a million rows and a Boolean column is_active that is TRUE for 95% of them. Why might the optimiser ignore an index on is_active for WHERE is_active = TRUE?

  1. AThe optimiser never uses indexes in WHERE clauses
  2. BTRUE is treated as NULL by the index
  3. CIndexes cannot be created on Boolean columns
  4. DMost rows match, so a full table scan is cheaper than many index lookups
8.Hard

R(A, B, C, D) has the functional dependencies A → B and C → D. What is the candidate key?

  1. A{A}
  2. B{B, D}
  3. C{C}
  4. D{A, C}

Answer key

  1. 1. C · ALTER TABLE
  2. 2. A · LIKE
  3. 3. A · ORDER BY
  4. 4. A · Rows that appear in both results
  5. 5. C · A stored query that behaves like a virtual table
  6. 6. B · With a junction table holding foreign keys to both students and courses
  7. 7. D · Most rows match, so a full table scan is cheaper than many index lookups
  8. 8. D · {A, C}

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