Bytes

SQL DELETE

Last Updated: 22nd June, 2023

Overview

In SQL, a delete statement is utilized to remove one or more columns from a table. The syntax by and large, incorporates the keyword "Delete", the table title from which to delete rows, and a WHERE clause indicating the conditions under which to delete columns.

What is Delete Statement?

Structured Query Language (SQL) may be a capable apparatus for overseeing and manipulating data in relational databases. One of the foremost fundamental SQL commands is the Delete statement. This command lets you delete one or more records from a table based on indicated conditions. In this blog, we will investigate the Delete statement in SQL and its various aspects. Delete could be a SQL explanation used to erase database table records.

For illustration, a company specializing in online retail might utilize a Delete statement to evacuate outdated or damaged things from the stock table. This would free up space within the table and guarantee that clients are, as it was seeing items that are accessible for buying. Let's see its language structure and cases.

Syntax of DELETE statement:

The basic syntax of the DELETE statement in SQL is as follows:


DELETE FROM table_name WHERE condition;

In the above syntax, table_name is the table name from which you want to delete the records. The condition specifies the criteria that must be met for the records to be deleted. If the condition is not specified, all the records in the table will be deleted.

Delete single record

Let's consider a sample table called "students" with the following columns: id, name, age, gender, and grade.

To delete a record with a specific id value, you can use the following query:

Table: students

IDNameAge
1John20
2Jane19
3Dave21
4Mary18

DELETE FROM students WHERE id = 1;

This statement will delete the record with id=1 from the student's table.

Delete multiple records

To delete records having an age as 20, you can use the following query:

DELETE FROM students WHERE age=20;

Deleting all the records

To delete all records from the student's table, you can use the following query:


DELETE FROM students;

This statement will delete all the records from the student's table.

Difference between Delete, Drop, and Truncate

In the context of databases and SQL, "delete", "drop," and "truncate" are commands that are used to remove data from tables. However, they differ in their behavior and impact on the table structure. Here's a comparison of the three commands in a table format:

CommandPurposeImpact on the table structureCommitment
DELETERemoves specific rows from a table based on a WHERE clause.Does not affect the table structure. Only the specified rows are removed.It can be rolled back using a transaction.
DROPRemoves an entire table from the database.Completely removes the table and all its data. The table structure is also removed.It cannot be rolled back.
TRUNCATERemoves all data from a table.Removes all data from the table, but the table structure remains intact.It cannot be rolled back.

Conclusion:

The online retailer utilized a Delete statement to evacuate outdated or damaged items from the inventory table. This freed up space within the table and guaranteed that clients were as it was seeing items that were accessible for purchase. The Delete statement was utilized to for all time expel any items that were now not substantial.

Key Takeaways

  1. This lesson clarifies the Delete statement in SQL, which is utilized to evacuate one or more rows from a table based on indicated conditions.
  2. The syntax of the Delete statement is given, along with cases of how to delete single and different records and all records from a table.
  3. Delete removes particular columns, DROP removes the whole table, including its structure, and TRUNCATE removes all information from the table but keeps its structure.
  4. It's vital to note that DROP and TRUNCATE cannot be fixed, so it's vital to utilize them cautiously.
  5. Also, sometime recently, performing any of these commands it's a great practice to back up your information to dodge losing imperative data.

Quiz

1. What is the purpose of the DELETE statement in SQL? 

  1. To add new records to a table 
  2. To modify existing records in table 
  3. Remove one or more records from a table based on specified conditions 
  4. To retrieve records from a table

Answer: c. To remove one or more records from a table based on specified conditions

2. What is the basic syntax of the DELETE statement in SQL? 

  1. DELETE FROM table_name; 
  2. DELETE * FROM table_name; 
  3. DELETE table_name; 
  4. DELETE table_name WHERE condition;

Answer: d. DELETE table_name WHERE condition;

3. How can you delete a single record from a table in SQL? 

  1. DELETE FROM table_name; 
  2. DELETE * FROM table_name WHERE id = 1; 
  3. DELETE FROM table_name WHERE id = 1; 
  4. DELETE FROM table_name WHERE id = '1';

Answer: c. DELETE FROM table_name WHERE id = 1;

4. How can you delete multiple records from a table in SQL? 

  1. DELETE * FROM table_name WHERE condition; 
  2. DELETE FROM table_name WHERE condition; 
  3. DELETE FROM table_name; 
  4. DELETE FROM table_name WHERE age = 20;

Answer: d. DELETE FROM table_name WHERE age = 20;

5. What happens when you execute a DELETE statement without a WHERE clause? 

  1. All records in the table will be deleted. 
  2. A specific record will be deleted based on a primary key. 
  3. An error will occur, and the statement will not be executed. 
  4. The table structure will be deleted.

Answer: a. All records in the table will be deleted.

6. What is the difference between DELETE, DROP, and TRUNCATE in SQL? 

  1. DELETE removes data from a table based on a WHERE clause, DROP removes the table and its data, and TRUNCATE removes all data from the table but keeps its structure.  
  2. DELETE removes data from a table but keeps its structure, DROP removes the table and its data, and TRUNCATE removes specific rows based on a condition. 
  3. DELETE removes data from a table but keeps its structure, DROP removes specific rows based on a condition, and TRUNCATE removes the table and its data. 
  4. DELETE removes specific rows based on a condition, DROP removes data from a table but keeps its structure, and TRUNCATE removes all data from the table but keeps its structure

Answer: a.  DELETE removes data from a table based on a WHERE clause, DROP removes the table and its data, and TRUNCATE removes all data from the table but keeps its structure.

Module 4: DML CommandsSQL DELETE

Top Tutorials

Related Articles

  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter