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 WHERE country IN ('NL', 'BE', 'DE') do?
- AKeeps rows whose country equals any value in the list
- BSorts rows in the order of the list
- CKeeps rows whose country is not in the list
- DKeeps rows whose country value contains all three codes at once
2.Easy
Which keyword gives a column or table a temporary alternative name in a query?
- ARENAME
- BAS
- CALIAS
- DNAME
3.Easy
What does COMMIT do in a transaction?
- AMakes all changes of the transaction permanent
- BLocks the whole database
- CUndoes all changes made since the transaction began
- DStarts a new table
4.Medium
A foreign key is declared with ON DELETE CASCADE. What happens when a parent row is deleted?
- AThe foreign key in the child rows is set to NULL automatically
- BThe delete is always rejected
- CThe child rows that reference it are deleted automatically
- DThe parent row is copied to an archive table
5.Medium
Students can take many courses and courses have many students. How is this modelled in tables?
- ABy merging students and courses into one table
- BWith a foreign key column course_id in the students table pointing to courses
- CWith a list of course ids stored in one column
- DWith a junction table holding foreign keys to both students and courses
6.Medium
Which statement removes all rows of a table, keeps its structure and cannot take a WHERE clause?
- ATRUNCATE TABLE
- BDROP TABLE
- CDELETE FROM with a WHERE clause
- DALTER TABLE
7.Hard
What does SELECT day, SUM(amount) OVER (ORDER BY day) FROM sales compute?
- AThe total of all amounts on every row
- BThe average amount per day
- COne row per day with that day's total only
- DA running total of amount ordered by day
8.Hard
Which window function assigns 1, 2, 3, ... to rows within each partition without ties?
- ARANK()
- BDENSE_RANK()
- CCOUNT()
- DROW_NUMBER()