SQL DELETE Statement

In this step by step SQL Tutorial I will show you how to remove rows from a table . For the purpose of this tutorial I will use SQL Server 2012.

Other Versions of SQL Server:

  • SQL Server 2012
    • This blog post has been written using SQL Server 2012
  • SQL Server 2008 / R2
    • The examples we used should work in SQL Server 2008 and R2
  • SQL Server 2005
    • We suspect that this will work in SQL Server 2005 as well

Who is it for?

  • People who would like to learn basics of using SQL DELETE 
  • Those who are preparing for Microsoft certifications and need to cover this topic.

Content:

DELETE Introduction

Delete allows to remove rows from a table and you can write delete query with filters so you delete only the rows you want.

To better understand DELETE let's create a task that is close to real-life. In our story imagine you are a developer
that has been ask to clean your table before loading new data from spreadsheet. In the  following example I will show you how you could solve your task.

DELETE using SSMS 2012

For this example I will use AdventureWorksDW2012 database, dbo.DatabaseLog table. 

 

I retrieve all data (*) From dbo.DatabaseLog and check how many rows I have got now. 

 

I use DELETE statement to remove all records from dbo.DatabaseLog. You can see that in the Messages window under my script is message saying that 112 rows was deleted.

 

Now I just check if my table is empty and you can see that all columns contain not data.

 

DELETE with WHERE Statement

In the Object Explorer I go to AdventureWorksDW2012, dbo.DimCusomer.

 

I open the table and find CustomerKey for row Katie Glownia.

 

I check rows number for DimCusomer table 

 

I type script to delete customer key 29485 and run my query 

 

I get Message saing that 1 row was deleted sucessfully.

 

I can see that in the DimCustomer last row containg data about Katie Glownia is not existing.

I have 18484 rows in DimCustemer.

 

I hope that will help you 

Katie 

Did you find this page useful? +1  |  -0
(1 Votes)

Want to add a comment


no comments
Donations: $145 (30 days)
Next Target: $250
Achieved: 72%

30 days stats
Thank you to your donation!

21st Aug - John Chamblee ($50)
20th Aug - Eamon Heenan (£10)
20th Aug - Pawel Olejniczak ($20)
19th Aug - Robert French ($10)
19th Aug - Robert Kelly ($10)
16th Aug - Lee Mezzulo ($5)
8th Aug - Betrehail Keteam ($2)
7th Aug - Pawel Bednarski (£10)
27th Jul - Bernhard Lauber (€20)
----
15th Jul - Pawel Olejniczak (£20)
13th July - Thomas Tabert ($10)
30 days stats
#1: Server $82/month
#2: Video hosting $25/month
#3: Katie & Emil $0.00/h
Caring is sharing :)