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 the relational model, what is a row of a table called?

  1. AA tuple
  2. BA schema
  3. CAn attribute
  4. DA domain
2.Easy

Which keyword gives a column or table a temporary alternative name in a query?

  1. ARENAME
  2. BAS
  3. CALIAS
  4. DNAME
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 cannot be changed after insert
  3. CThe column automatically becomes the primary key of the table
  4. DThe column values are sorted
4.Medium

Which query returns the number of orders per customer?

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

Which statement removes all rows of a table, keeps its structure and cannot take a WHERE clause?

  1. ADROP TABLE
  2. BDELETE FROM with a WHERE clause
  3. CTRUNCATE TABLE
  4. DALTER TABLE
6.Medium

Which query returns the highest salary in each department?

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

Given the functional dependencies A → B, B → C and C → D on R(A, B, C, D), what is the closure A+?

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

What does a FULL OUTER JOIN return?

  1. AOnly matching rows
  2. BThe Cartesian product, pairing each row of one table with every row of the other
  3. CAll rows from both tables, matched where possible and NULL-filled otherwise
  4. DOnly unmatched rows

Answer key

  1. 1. A · A tuple
  2. 2. B · AS
  3. 3. A · No two rows have the same non-NULL value in that column
  4. 4. D · SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id
  5. 5. C · TRUNCATE TABLE
  6. 6. A · SELECT dept, MAX(salary) FROM emp GROUP BY dept
  7. 7. C · {A, B, C, D}
  8. 8. C · All rows from both tables, matched where possible and NULL-filled otherwise

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