T-SQL Tutorial

SQL DELETE


To delete rows from an existing table in SQL Server, you can use the DELETE statement.


DELETE Syntax:

The basic syntax of the DELETE statement is as follows:

DELETE FROM table_name WHERE {condition}

IF you don't put the {condition} then all the records from the table will be erased.

For example, if you have a table named "customers" and you want to delete all rows where the "city" column is "New York", you would use the following query:

DELETE FROM customers WHERE city = 'New York';

You can also use more complex conditions in the WHERE clause to specify which rows to delete. Additionally, you can use the TRUNCATE TABLE statement to delete all the rows in the table and reset the identity seed, it's faster than DELETE statement. Before any delete statement, you should check if the table exist, you can use the following query to check if the table exist:

IF OBJECT_ID('table_name', 'U') IS NOT NULL
PRINT 'Table exists.';
ELSE
PRINT 'Table does not exist.';

Please be very careful when using the DELETE statement, as it permanently removes data from your table. Make sure to always test your queries on a backup or test version of your data before running them on your production database.


Store table:

OBJECT_IDPRICENAME
1200A
2500B
3900C
4500D

Example:

DELETE FROM store WHERE price=500;

SELECT * FROM store;

OBJECT_IDPRICENAME
1200A
3900C