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. CRows that do not match
  4. DAll rows from both tables
2.Easy

Which statement removes rows from a table?

  1. AREMOVE
  2. BTRUNCATE COLUMN
  3. CDROP
  4. DDELETE
3.Easy

Which rows does WHERE price BETWEEN 10 AND 20 select?

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

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

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

Which query returns the number of orders per customer?

  1. ASELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id
  2. BSELECT DISTINCT COUNT(*) FROM orders
  3. CSELECT customer_id, COUNT(*) FROM orders ORDER BY customer_id
  4. DSELECT customer_id FROM orders WHERE COUNT(*) > 0
6.Medium

Why can a table have only one clustered index?

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

What does SELECT day, SUM(amount) OVER (ORDER BY day) FROM sales compute?

  1. AOne row per day with that day's total only
  2. BThe total of all amounts on every row
  3. CA running total of amount ordered by day
  4. DThe average amount per day
8.Hard

What is a known drawback of decomposing tables into BCNF compared with 3NF?

  1. ASome functional dependencies may no longer be checkable within a single table
  2. BThe decomposition always loses data when joined back
  3. CBCNF allows more redundancy than 3NF
  4. DBCNF tables cannot have primary keys

Answer key

  1. 1. A · Only rows with matching values in both tables
  2. 2. D · DELETE
  3. 3. B · Rows with price from 10 to 20, including 10 and 20
  4. 4. A · SELECT COUNT(DISTINCT city) FROM customers
  5. 5. A · SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id
  6. 6. A · The rows can be stored physically in only one order
  7. 7. C · A running total of amount ordered by day
  8. 8. A · Some functional dependencies may no longer be checkable within a single table

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