DEV Community

Cover image for Learn SQL: Microsoft SQL Server - Episode 15: Deleting records from a table
Goran Kortjie
Goran Kortjie

Posted on

Learn SQL: Microsoft SQL Server - Episode 15: Deleting records from a table

Hello-Goran

Time for the scariest segment of SQL query statements. As in the previous entry, we need to make sure that we are working with the correct data.

Continuing with our previous episode, we will be working in the HumanResourcesDepartment table. This is where we will be performing the delete operation, we will delete the row with the DepartmentID = 17.

To construct a delete statement we need to use the following syntax.

delete from [table-name]
where [column-name] = value
Enter fullscreen mode Exit fullscreen mode

Notice how this allows us to wipe out the entire HumanResourcesDepartment table if we were to omit the Where clause.

Delete-a-single-record

We can use any Column-Name in the table to specify which row we want to delete. Generally using a key would be safest for deleting a single record in a table.

As a side-note...We can also delete multiple rows simultaneously by specifying a column that has a value present in more than one record or row. For example in the GroupName with the value 'Executive General and Administration'. If we pass in this value in our Where clause it will remove each row containing 'Executive General and Administration' in it's GroupName column.

Be safe

To make this perfectly clear, we need to ensure the row or rows we want to delete are the values specified in the Where clause.

I personally would double-check the row or rows I need to delete by using a Select statement.

Select * from [Table-Name]
Where [column-name] = [value]
Enter fullscreen mode Exit fullscreen mode

By using this statement before we perform our delete operation we can confirm that we are working with the correct data.

select-statement-check-before-delete

Always make sure you are working with the correct data and never perform a Delete operation without a Where clause.

Thats all from me, I hope you enjoyed this episode and look forward to the next and final entry on Learn SQL.

relax-man

Top comments (0)