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

An INNER JOIN returns:

  1. AOnly rows with matching values in both tables
  2. BAll rows from the left table, with NULLs where there is no match
  3. CAll rows from both tables
  4. DRows that do not match
2.Easy

How do you sort a result from the highest to the lowest salary?

  1. ASORT salary DOWN
  2. BORDER BY salary ASC
  3. CORDER BY salary DESC
  4. DGROUP BY salary DESC
3.Easy

How does UNION ALL differ from UNION?

  1. AUNION ALL keeps duplicate rows
  2. BUNION ALL only works on one table
  3. CUNION ALL sorts the result
  4. DUNION ALL removes duplicate rows
4.Medium

Second normal form (2NF) removes:

  1. AAll NULL values
  2. BForeign keys
  3. CTransitive dependencies between non-key columns via another non-key column
  4. DPartial dependencies of non-key columns on part of a composite key
5.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 COUNT(DISTINCT city) FROM customers
  4. DSELECT DISTINCT COUNT(city) FROM customers
6.Medium

A column holds the values 10, 20 and NULL. What does AVG(col) return?

  1. ANULL
  2. B30
  3. C10
  4. D15
7.Hard

What is the result of SELECT COUNT(col) FROM t if all values in col are NULL and the table has 10 rows?

  1. ANULL
  2. BAn error
  3. C10
  4. D0
8.Hard

When is decomposing R into R1 and R2 guaranteed to be lossless?

  1. AWhen R1 and R2 have the same number of columns
  2. BWhen R1 and R2 have no attributes in common
  3. CWhen the common attributes of R1 and R2 form a superkey of R1 or of R2
  4. DWhen every attribute of R appears in both R1 and R2

Answer key

  1. 1. A · Only rows with matching values in both tables
  2. 2. C · ORDER BY salary DESC
  3. 3. A · UNION ALL keeps duplicate rows
  4. 4. D · Partial dependencies of non-key columns on part of a composite key
  5. 5. C · SELECT COUNT(DISTINCT city) FROM customers
  6. 6. D · 15
  7. 7. D · 0
  8. 8. C · When the common attributes of R1 and R2 form a superkey of R1 or of R2

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