Database Changes SQL Topic exerciseEasyVerified answerSQLite + PostgreSQL live · 3 guided

Remove Expired Cancelled Orders

Delete cancelled orders placed before 2025-01-01.

  • DELETE
  • Predicate safety
  • Date filtering

Exercise brief

Understand the request

Data governance Cancelled orders before the 2025 retention boundary are eligible for deletion.

Data retention policy allows old cancelled orders to be removed from the active operations database.

Return

  • Remove only records matching both retention conditions.

Constraints

  • Keep all pending and completed orders.
  • Treat 2025-01-01 as an exclusive cutoff.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

orders

  • order_idINTEGER
  • statusVARCHAR(20)
  • ordered_atDATE

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

DELETE removes rows, so make the retention rule explicit in the WHERE clause.

Hint 2

Combine status = 'cancelled' with a date earlier than 2025-01-01.

Hint 3

DELETE FROM orders WHERE status = 'cancelled' AND ordered_at < '2025-01-01';

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
DELETE FROM orders
WHERE status = 'cancelled' AND ordered_at < '2025-01-01';

Why this works

Both status and time are required to translate the retention policy into a narrowly scoped deletion.

Success check

The expired cancelled order is gone and the three unrelated orders remain.

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.