DELETE TOP x rows with an ORDER BY – SQL Undercover
Using a CTE
The second solution involves using a CTE to select all the rows that we want to delete and then deleting everything from the CTE. Let’s have a look at what that code looks like…
WITH ToDelete
AS
(SELECT TOP 10 id, first_name, last_name, email, gender, DOB
FROM people
WHERE gender = 'Female'
ORDER BY DOB ASC)
DELETE FROM ToDelete
September 19, 2022 at 8:59:58 AM EDT
*
FILLER