Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL: Delete Data in a Table

In PostgreSQL, use the DELETE statement to delete one or more rows in the table. It only deletes the data from the table and not the table structure.

Syntax: Delete Statement
DELETE FROM <table_name>
[WHERE &lt;condition<]
RETURNING * | <output_expression< AS <output_name<;

In the above syntax,

  • Specify the name of the table where you want to delete the data after the DELETE FROM keyword.
  • The WHERE clause is optional which limits the delete operation specific to the specified condition. If you do not specify the WHERE clause, Postgres will delete all the rows of a table.
  • The RETURNING clause is optional which will return a list of all deleted rows or values of the specified column.

After the DELETE statement is executed successfully, PostgreSQL will give the message as DELETE count where the count is the number of rows deleted by the DELETE statement.

Consider the following employee table:

Now, let's remove data from the employee table whose emp_id = 5.

Example: Delete a Row
DELETE FROM employee
WHERE emp_id = 5;

The above query will display the following result in pgAdmin:

The above output DELETE 1 shows 1 row is deleted from the employee table.

The DELETE statement can remove multiple records from the employee table. For example, now we will remove rows where emp_id = 3 and 4 and will return the deleted row after executing the DELETE statement.

Example:
DELETE FROM employee
WHERE emp_id IN (3, 4);

The above query will display the following result in pgAdmin:

The DELETE 2 indicates that it has deleted 2 rows from the table.

RETURNING Clause with DELETE Statement

The RETURNING clause returns the deleted rows or column values. If you specify RETURNING * then it will return all the deleted rows and if you specify RETURNING column_name then it will return values of the specified columns. You can specify multiple columns separated with a comma.

The following DELETE statement returns all the deleted rows:

Example: RETURNING *
DELETE FROM employee
WHERE emp_id = 2
RETURNING *;

The following DELETE statement returns specified columns of deleted rows:

Example: RETURNING Columns
DELETE FROM employee
WHERE emp_id IN (5, 7)
RETURNING emp_id, first_name, last_name;

Delete All Data from a Table

The DELETE statement can be used to delete all rows from a table if you omit the WHERE clause, as shown below.

Example: Delete All Data
DELETE FROM employee;

The above has deleted all the rows from the employee table. Let's verify it by running the SELECT query.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.