beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

DELETE Query in SQL

By Chaitanya Singh | Filed Under: SQL

Delete Query is used for deleting the existing rows(records) from table. Generally DELETE query is used along with WHERE clause to delete the certain number of rows that fulfills the specified condition. However DELETE query can be used without WHERE clause too, in that case the query would delete all the rows of specified table.

Syntax

1) To Delete a particular set of rows:

DELETE FROM TableName
WHERE condition;

2) To Delete all the rows of a table:

DELETE FROM TableName;

Example:

Lets take the same table EMPLOYEES which we had in the last tutorial.

+------+----------+---------+----------+
|SSN   | EMP_NAME | EMP_AGE |EMP_SALARY|
+------+----------+---------+----------+
|  101 | Steve    |  23     |  9000.00 |
|  223 | Peter    |  24     |  2550.00 |
|  388 | Shubham  |  19     |  2444.00 |
|  499 | Chaitanya|  29     |  6588.00 |
|  589 | Apoorv   |  21     |  1400.00 |
|  689 | Rajat    |  24     |  8900.00 |
|  700 | Ajeet    |  20     | 18300.00 |
+------+----------+---------+----------+

Delete all the records that have SSN greater than 400:

SQL> DELETE FROM EMPLOYEES
WHERE SSN > 400;

After the successful execution of above query the table would be having below mentioned records:

+------+----------+---------+----------+
|SSN   | EMP_NAME | EMP_AGE |EMP_SALARY|
+------+----------+---------+----------+
|  101 | Steve    |  23     |  9000.00 |
|  223 | Peter    |  24     |  2550.00 |
|  388 | Shubham  |  19     |  2444.00 |
+------+----------+---------+----------+

Delete the data of employees having age greater than or equal to 24:

SQL> DELETE FROM EMPLOYEES
WHERE EMP_AGE >=24;

Result:

+------+----------+---------+----------+
|SSN   | EMP_NAME | EMP_AGE |EMP_SALARY|
+------+----------+---------+----------+
|  101 | Steve    |  23     |  9000.00 |
|  388 | Shubham  |  19     |  2444.00 |
+------+----------+---------+----------+

DELETE all the records of table EMPLOYEES

SQL> DELETE FROM EMPLOYEES;

Enjoyed this post? Try these related posts

  1. SQL – CREATE TABLE Statement
  2. UNIQUE Constraint in SQL
  3. SQL Select Random Rows from Table
  4. SQL ORDER BY Clause
  5. Introduction to SQL
  6. SQL SELECT COUNT

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

SQL Tutorial

  • SQL Introduction
  • SQL Syntax
  • SQL Select
  • SQL Select Distinct
  • SQL Select Count
  • SQL Select Top
  • SQL Where
  • SQL AND, OR & NOT
  • SQL Order By
  • SQL Insert Into
  • SQL Insert Into SELECT
  • SQL Select Random
  • SQL Alias
  • SQL NULL Check
  • SQL Update
  • SQL Delete
  • SQL MIN, MAX
  • SQL SUM
  • SQL AVG

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2019 BeginnersBook . Privacy Policy . Sitemap