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. AJoins two tables on their common columns
  2. BRemoves rows with NULL values
  3. CPuts rows with the same values into groups so aggregates are computed per group
  4. DSorts the rows of the result alphabetically
2.Easy

An INNER JOIN returns:

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

What does a CHECK constraint do?

  1. ARejects rows whose values do not satisfy a condition
  2. BChecks whether a table exists before a query is run on it
  3. CCounts the rows in a table
  4. DVerifies the user's password
4.Medium

A LEFT JOIN returns:

  1. AAll rows from the left table and matching rows from the right, with NULLs where there is no match
  2. BRows that match in neither table
  3. COnly matching rows from both tables
  4. DAll rows from the right table only
5.Medium

What does GRANT SELECT ON employees TO alice do?

  1. AMakes alice the owner of the database
  2. BAllows the user alice to read the employees table
  3. CCopies the employees table to alice
  4. DAllows alice to update and delete rows in the employees table
6.Medium

Which query returns the highest salary in each department?

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

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

  1. AThe average grade of all students
  2. BStudents with a grade above the average grade
  3. CAn error because subqueries are not allowed in WHERE
  4. DStudents with the highest grade only
8.Hard

Why might a query with WHERE YEAR(order_date) = 2025 not use an index on order_date?

  1. AAn index on order_date is only used for ORDER BY clauses, never for WHERE
  2. BYEAR is not valid SQL
  3. CIndexes never work with dates
  4. DApplying a function to the column prevents a simple index range lookup

Answer key

  1. 1. C · Puts rows with the same values into groups so aggregates are computed per group
  2. 2. C · Only rows with matching values in both tables
  3. 3. A · Rejects rows whose values do not satisfy a condition
  4. 4. A · All rows from the left table and matching rows from the right, with NULLs where there is no match
  5. 5. B · Allows the user alice to read the employees table
  6. 6. C · SELECT dept, MAX(salary) FROM emp GROUP BY dept
  7. 7. B · Students with a grade above the average grade
  8. 8. D · Applying a function to the column prevents a simple index range lookup

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