Skip to main content
Learning DUB

Deleting Records in a SQL Database Table

As the name suggests, the SQL DELETE statement is used to delete existing records from a database table when they are no longer needed. Using DELETE statement, you can delete single or multiple records from the database. The DELETE command is used more frequently with the WHERE clause when removing unnecessary records from the database.

Ways to Delete Data in SQL

SQL Server supports these ways to delete data in an existing table:
The DELETE statement.
The deletion of the current row in a result set or cursor.
The TRUNCATE TABLE statement.

For this exercise however we are going to focus on deleting data in SQL using the DELETE statement. The DELETE statement removes one or more rows in a table or view. A simplified form of the DELETE syntax is:

DELETE Syntax
DELETE table_or_view
FROM table_sources
WHERE search_condition;

table_or_view is the name the table or view from which the rows are to be deleted. All rows in table_or_view that meet the qualifications of the WHERE search condition are deleted.

If a WHERE clause is not specified, all the rows in table_or_view are deleted.

The FROM clause specifies additional tables or views and join conditions that can be used by the predicates in the WHERE clause search condition to qualify the rows to be deleted from table_or_view.

Rows are not deleted from the tables named in the FROM clause, only from the table named in table_or_view.

Any table that has all rows removed remains in the database. The DELETE statement deletes only rows from the table, and the table must be removed from the database by using the DROP TABLE statement.

DELETE Statement Examples

Below are a three scripts demonstrating the use of DELETE statements and showing a series of deletes of differing complexity. This script below shows the three DELETE statements needed to delete the rows associated with products supplied by the company named Lyngbysild in the Northwind database. This would not be a typical business operation because it involves deleting lines from existing orders, but it does show a series of deletes of differing complexity.

SQL
SQL

To delete limited records in the database, use the WHERE clause along with DELETE command. For example:

SQL

Another example:
USE Northwind GO DELETE FROM Employee WHERE Employee_Id IN(3,5); GO

Once we execute the above query will remove the employees with employee IDs 3 & 5 entries from the employee table

//////////////////////////////////

//////////////////////////////////////////
How to Delete Rows with the DELETE statement
///////////////////////////////////////////
How to Delete Rows in Result Sets
// The ADO, OLE DB, and ODBC application programming interfaces (APIs) support deleting the current row that an application is positioned on in a result set. The application executes a statement, and then fetches rows from the result set. After an application has fetched the row, it can use the following functions or methods to delete the row: ADO applications use the Delete method of the Recordset object. OLE DB applications use the DeleteRows method of the IRowsetChange interface. ODBC applications use the SQLSetPos function with the SQL_DELETE option. DB-library applications use dbcursor to perform a CRS_DELETE operation. Transact-SQL scripts, stored procedures, and triggers can use the WHERE CURRENT OF clause on a DELETE statement to delete the cursor row on which they are currently positioned, for example:

DECLARE abc CURSOR FOR
SELECT * FROM MyTable
OPEN abc
FETCH NEXT FROM abc
DELETE MyTable WHERE CURRENT OF abc
CLOSE abc
DEALLOCATE abc

//////////////////////////////////////////
How to Delete All Rows Using TRUNCATE TABLE
// The TRUNCATE TABLE statement is a fast, nonlogged method of deleting all rows in a table. It is almost always faster than a DELETE statement with no conditions because DELETE logs each row deletion, and TRUNCATE TABLE logs only the deallocation of whole data pages. TRUNCATE TABLE immediately frees all the space occupied by that table’s data and indexes. The distribution pages for all indexes are also freed. As with DELETE, the definition of a table emptied using TRUNCATE TABLE remains in the database, along with its indexes and other associated objects. The DROP TABLE statement must be used to drop the definition of the table.

Related Content

Scroll to Top