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 GROUP BY do in a query?

  1. APuts rows with the same values into groups so aggregates are computed per group
  2. BRemoves rows with NULL values
  3. CJoins two tables on their common columns
  4. DSorts the rows of the result alphabetically
2.Easy

What is a candidate key?

  1. AA minimal set of columns that uniquely identifies each row
  2. BA set of columns shared between two tables to link their rows
  3. CAny column that contains NULL values
  4. DA column that is waiting to become a foreign key
3.Easy

What does a UNIQUE constraint on a column guarantee?

  1. ANo two rows have the same non-NULL value in that column
  2. BThe column values are sorted
  3. CThe column automatically becomes the primary key of the table
  4. DThe column cannot be changed after insert
4.Medium

Which query returns the number of different cities in the customers table?

  1. ASELECT COUNT(city) FROM customers GROUP BY city
  2. BSELECT SUM(DISTINCT city) FROM customers
  3. CSELECT DISTINCT COUNT(city) FROM customers
  4. DSELECT COUNT(DISTINCT city) FROM customers
5.Medium

Why can a table have only one clustered index?

  1. AThe SQL standard forbids more than one index on a table
  2. BThe rows can be stored physically in only one order
  3. CClustered indexes can only be on the primary key
  4. DClustered indexes use too much memory
6.Medium

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

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

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

  1. AStudents with the highest grade only
  2. BThe average grade of all students
  3. CStudents with a grade above the average grade
  4. DAn error because subqueries are not allowed in WHERE
8.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. AMost rows match, so a full table scan is cheaper than many index lookups
  2. BIndexes cannot be created on Boolean columns
  3. CTRUE is treated as NULL by the index
  4. DThe optimiser never uses indexes in WHERE clauses

Answer key

  1. 1. A · Puts rows with the same values into groups so aggregates are computed per group
  2. 2. A · A minimal set of columns that uniquely identifies each row
  3. 3. A · No two rows have the same non-NULL value in that column
  4. 4. D · SELECT COUNT(DISTINCT city) FROM customers
  5. 5. B · The rows can be stored physically in only one order
  6. 6. B · No rows, because the comparison is unknown
  7. 7. C · Students with a grade above the average grade
  8. 8. A · Most rows match, so a full table scan is cheaper than many index lookups

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