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

In PostgreSQL and MySQL, which clause returns only the first 10 rows of a result?

  1. AMAX 10
  2. BFIRST 10 ONLY
  3. CLIMIT 10
  4. DTOP ROWS 10
2.Easy

Which SQL statement changes values in rows that already exist?

  1. AREPLACE COLUMN
  2. BMODIFY
  3. CUPDATE
  4. DINSERT
3.Easy

Which SQL keyword removes duplicate rows from a result?

  1. ASINGLE
  2. BUNIQUE
  3. CDISTINCT
  4. DDIFFERENT
4.Medium

The orders table has 5 rows, each referring to an existing customer. The customers table has 3 rows. How many rows does an INNER JOIN on customer_id return?

  1. A5
  2. B3
  3. C8
  4. D15
5.Medium

Which query returns the highest salary in each department?

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

When is WHERE EXISTS (subquery) true for a row?

  1. AWhen the subquery returns a number greater than zero
  2. BWhen the subquery returns at least one row
  3. CWhen the subquery returns no NULL values
  4. DWhen the subquery returns exactly one row
7.Hard

Table enrolment(student_id, course_id, student_name, grade) has the key (student_id, course_id). Which normal form does it violate?

  1. AThird normal form only, because grade depends on student_name
  2. BFirst normal form, because the grade column does not hold atomic values
  3. CNone, it is in BCNF
  4. DSecond normal form, because student_name depends on student_id only
8.Hard

Two transactions both read stock = 10, each subtracts 1 and writes the result, giving 9 instead of 8. Which anomaly is this?

  1. ADeletion anomaly
  2. BDirty read
  3. CLost update
  4. DPhantom read

Answer key

  1. 1. C · LIMIT 10
  2. 2. C · UPDATE
  3. 3. C · DISTINCT
  4. 4. A · 5
  5. 5. C · SELECT dept, MAX(salary) FROM emp GROUP BY dept
  6. 6. B · When the subquery returns at least one row
  7. 7. D · Second normal form, because student_name depends on student_id only
  8. 8. C · Lost update

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