| SQL Server Basics
Delete Query in SQL Server
learn sql - delete query in ms sql server 2008, ms sql server 2012, ms sql server 2014, ms sql server 2016
Description
The SQL SERVER DELETE query is used to delete a single record or multiple records from the table in SQL Server.
Syntax
Below is the sql code for delete statement in sql server
DELETE FROM table [WHERE conditions];
However, the full syntax for the DELETE statement in SQL Server sql code
DELETE [ TOP (top_value) [ PERCENT] ] FROM table [WHERE conditions];
Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | SQL Server Basics
SELECT [ ALL | DISTINCT ] [ TOP (top_value) [ PERCENT ] [ WITH TIES ] ]
expressions FROM tables [WHERE conditions] [GROUP BY expressions] [HAVING condition] [ORDER BY expression [ ASC | DESC ]];
Parameters or Arguments
table The table from where we are trying to delete the records. WHERE conditions Optional. Where Condition to filter the data through our delete statement. TOP (top_value) Optional. Delete top records with condition or with out condition. For example, I got some 10 records and am giving delete top(5). This will delete only top 5 records.
Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | SQL Server Basics
PERCENT Optional. Percentage of records to delete. 50% in 10 records indicates 5 records to be deleted.
Example-Using one condition
Delete statement will one or more condition to filter the data before deleting is really important or else it will delete the entire records in the table.
Syntax
DELETE FROM table_name WHERE [condition1]
Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | SQL Server Basics
Example-Using Multiple condition
Delete statement will be used in multiple condition to filter the data. Before deleting it’s really an important factor to check it out or else it will delete the entire records in the table.
Syntax
DELETE FROM table_name WHERE [condition1] and [condition2]
Example-Using TOP Keywords
Top keyword associated with sql server DELETE statement will delete the specified number of records from the table.
Syntax
DELETE TOP(value) FROM table_name [WHERE condition]
Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | SQL Server Basics
Example-Using TOP percent Keywords
Top percent keyword in sql server DELETE statement will delete the specified percentage of records in the table.
Syntax
DELETE TOP(value) FROM table_name [WHERE condition]
Sample sql Code
Prerequisite query:
create table dbo.cutomers(customerid int, city varchar(100)) go
The above queries will create a table and the data will be inserted into it. Already if you got customers table which we created in our previous excercises. Do not worry, pass only delete statement to it. Here is the query,
Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | SQL Server Basics
Delete from dbo.customers -- This query will delete all records and pass the insert statement.
Now execute your insert statements.
insert into dbo.Customers values (1,'New York') insert into dbo.Customers values (2,'washington')
insert into dbo.Customers values (3,'Los Angeles') insert into dbo.Customers values (4,'Chicago') insert into dbo.Customers values (5,'Houston') insert into dbo.Customers values (6,'Boston') insert into dbo.Customers values (7,'Philadelphia') insert into dbo.Customers values (8,'San Diego') insert into dbo.Customers values (9,'Phoenix')
insert into dbo.Customers values (10,'Denver')
Now execute your delete query
delete from dbo.Customers where customerid=4 -- delete the record with customerid=4. 1 record deleted from the total records of 10 select * from customers -- retrieving the records in the table 9 records. delete Top (2) from dbo.Customers -- remove top 2 records from the table. Now the number of records is 7 delete Top (50) percent from dbo.Customers -- Among 7 records 50% of records indicates it’s 4 records. So 4 records will be delete here. select * from customers --- final remaining 3 records available in the table will be displayed.
Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | SQL Server Basics
Code Explanation
Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | SQL Server Basics
Delete the customer id =4 from the table customers. Fetch all the data from the table. We will receive data without the customer id =4.
Delete top 2 customers from the table.
Delete 50% of records from the table. Considering, If we got 7 records 50% is more than 3 right. So, 4 records will be deleted.
Fetch the remaining records from the table.
Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | SQL Server Basics
Delete the customer id =4 from the table customers.
Fetch all the data from the table. We will receive data without the customer id =4. Delete top 2 customers from the table.
Delete 50% of records from the table. Considering, If we got 7 records 50% is more than 3 right. So, 4 records will be deleted.
Fetch the remaining records from the table.
Fetch the remaining 9 data after 1 data removed through the
previous statement.
Fetch the remaining data available in the table. In this case, it’s 3 records.
Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved.