x

SQL – DELETE

Prev     Next

SQL – DELETE statement:

The DELETE statement in SQL is used to delete existing records in a table. Either we can delete all existing records in a table or can delete only particular records in a table using DELETE statement in SQL. Syntax for SQL DELETE statement is given below.


SQL Syntax for DELETE statement:

Syntax to DELETE only selected records DELETE FROM table_name
WHERE column_name=value;
Syntax to DELETE all records DELETE FROM table_name;
or
DELETE * FROM table_name;

Example for how to use SQL DELETE statement:

Please consider the following table with few records as given below.

Table name (for example): student
Column names in this table: Student_ID, Student_name, City and Age
Available records: 4 rows

sql-table1

  • To delete a record from this table, please execute below query.

SQL query:

DELETE FROM student
WHERE Student_ID = 3;

SQL query Output:

After executing above DELETE SQL query, source table will have only below 3 records. Please run following SQL statement to know currently existing records in the source table.


SQL query: SELECT * FROM student;

sql-table9

  • To delete all records from the source table, please execute below query.

SQL query:

DELETE FROM student;
or
DELETE * FROM student;

SQL query Output:

After executing above DELETE SQL query, source table will have no records. Please run following SQL statement to know currently existing records in the source table.

SQL query: SELECT * FROM student;

sql-table10

Do you know in SQL?

  • It’s a best practice to run select query with where clause to know how many records will get deleted by delete query. After that, we can execute our delete query.
  • Mistakenly deleted many records or almost all records? Don’t worry. There is a command called ROLLBACK; Please go through this SQL tutorial on SQL – ROLLBACK topic to know more.

Prev     Next



Like it? Please Spread the word!