CRUD Statements  «Prev  Next»

DELETE with an IN Subquery — Exercise

Create a DELETE statement using a subquery

Objective: Practice creating a DELETE statement using a subquery.

Exercise scoring


This is a 5-point self-assessment. Compare your SQL with the model answer and rubric after submitting. The website does not execute SQL or automatically grade your response.

Instructions

Practice environment: Write SQL for the sample tables in the dbo schema. Submitting displays your text and a model answer; it does not run database commands. If you test independently, use a disposable practice database.

Assume Tasks.TaskID is a primary key, Timesheets.TaskID references it, WeekEndingDate has the date type, and Hours is numeric. The five displayed timesheet rows form the complete sample. For the UPDATE exercise, the listed employee/task/date combination identifies one row.

To test your understanding of the relationship between subqueries and joins, use the same instructions as the prior exercise, but construct a subquery instead of selecting values from another table. To review the instructions, use the course project as an example. Remember the Timesheets table looks like this:

EmployeeID ClientID TaskID WeekEndingDate Hours
1171012026-06-0130
1171022026-06-0850
231012026-06-1540
3321032026-06-0145
3321032026-06-0835


The Tasks table looks like this:
TaskID Description Chargeable
101Vacation0
102General1
103Programming1

The task is to delete all timesheet rows for tasks that are not chargeable, as listed in the Tasks table. Construct a DELETE statement that uses a subquery from the Tasks table to retrieve the values to be deleted. Do not use a join.
Hint: Use the IN predicate in the WHERE clause.
Self-assessment rubric
PointsCriterion
2Select non-chargeable TaskID values from Tasks using Chargeable = 0.
2Use IN in the outer WHERE clause to match Timesheets.TaskID.
1Delete from Timesheets only, using a valid complete statement.

Submitting the exercise

Type or paste your Transact-SQL statement into the text box below, then click Submit to submit the answer and view a results page.