Questions tagged [sql-delete]

The SQL DELETE statement allows you to delete a single row or multiple rows from a SQL table.

The DELETE statement removes one or more rows from a table. A subset may be defined for deletion using a condition, otherwise all rows are removed. Some DBMSs, like MySQL, allow to delete rows from multiple tables with one DELETE statement (this is sometimes called multi-table DELETE).

Use this tag instead of which is too broad.

Reference

3082 questions
30
votes
8 answers

Deleting duplicates rows from redshift

I am trying to delete some duplicate data in my redshift table. Below is my query:- With duplicates As (Select *, ROW_NUMBER() Over (PARTITION by record_indicator Order by record_indicator) as Duplicate From table_name) delete from duplicates Where…
Neil
  • 1,715
  • 6
  • 30
  • 45
29
votes
7 answers

SQL Delete based on condition in join

It is possible to delete records based on a satisfied condition with a join query? For instance, I have a linking table joining 3 records. The query I have at the moment deletes records from this table where one of the id's isn't IN() an imploded…
user275074
28
votes
3 answers

Deleting rows from SQLite table when no match exists in another table

I need to delete rows from an SQLite table where their row IDs do not exist in another table. The SELECT statement returns the correct rows: SELECT * FROM cache LEFT JOIN main ON cache.id=main.id WHERE main.id IS NULL; However, the delete statement…
Marek Jedliński
  • 7,088
  • 11
  • 47
  • 57
28
votes
1 answer

Disable DELETE on table in PostgreSQL?

For a security sensitive design, I'd like to disable DELETEs on certain tables. The DELETE should merely set a deleted flag on a row (which would be then visible on a view, which would be used by the application layer). As I understand a rule would…
miku
  • 181,842
  • 47
  • 306
  • 310
26
votes
2 answers

PostgreSQL DELETE FROM (SELECT * FROM table FETCH FIRST 10 ROWS ONLY)

How do I delete only a few rows in postgreSQL? I want to fetch 10 rows to delete in a subquery. My table
ArthurDatur
  • 265
  • 1
  • 3
  • 7
26
votes
2 answers

how can I delete duplicates in SQLite?

I have a SQLite DB where the statement: SELECT messdatum, count(*) as anzahl from lipo GROUP BY Messdatum ORDER BY anzahl desc; results in some lines, which indicates that I have some duplicates with the same Messdatum. How can I delete the…
Walter Schrabmair
  • 1,251
  • 2
  • 13
  • 26
26
votes
1 answer

Delete first X lines of a database

is there an SQL command to delete the first X lines of a database table? I have a database table containing some information but no id or auto-incrementing value and a program that processes the first X lines of this table. Afterwards these X lines…
coroner
  • 513
  • 2
  • 6
  • 13
25
votes
5 answers

Deleting from multiple tables with foreign constraints

I am trying to delete from multiple tables. Here's what my tables look like A_has_B ---- B ---- C_has_B (many to many) (many to many) I am trying to delete all rows from A_has_B, B and C_has_B given the ID of a record in B. I am using…
F21
  • 32,163
  • 26
  • 99
  • 170
25
votes
2 answers

Difference between DELETE and DELETE FROM in SQL?

Is there one? I am researching some stored procedures, and in one place I found the following line: DELETE BI_Appointments WHERE VisitType != ( SELECT TOP 1 CheckupType FROM BI_Settings WHERE DoctorName = @DoctorName) Would that do the…
Joe M
  • 3,060
  • 3
  • 40
  • 63
25
votes
5 answers

How to delete data from multiple tables?

I have these tables: event (evt_id, evt_code, reg_id) magnitude (mag_id, evt_id, value) trace (trace_id, pt_id) point (pt_id, evt_id) I want to delete all rows from all tables related to evt_id=1139. How do I do it?
user1202766
  • 451
  • 2
  • 6
  • 7
24
votes
3 answers

Delete all records except the most recent one?

I have two DB tables in a one-to-many relationship. The data looks like this: select * from student, application Resultset: +-----------+---------------+---------------------+ | StudentID | ApplicationID | ApplicationDateTime…
sim
  • 3,552
  • 5
  • 23
  • 23
24
votes
5 answers

How can I insert the return of DELETE into INSERT in postgresql?

I am trying to delete a row from one table and insert it with some additional data into another. I know this can be done in two separate commands, one to delete and another to insert into the new table. However I am trying to combine them and it is…
lanrat
  • 4,344
  • 10
  • 35
  • 41
24
votes
8 answers

what does "delete from table where NULL = NULL" means?

What does delete from table where NULL = NULL mean?
rociiu
  • 241
  • 2
  • 4
23
votes
9 answers

How to Delete All Items From SQLite in Android

I would like to make an app where the user clicks a button, and the SQLite database is cleared. Here's what I've tried so far: db.delete(TABLE_NAME, null, null); What am I doing wrong?
Cg2916
  • 1,117
  • 6
  • 23
  • 36
23
votes
2 answers

android Room, how to delete multiple rows in the table by id list

Having a Android Room with three tables timestamp, index, details, and all three have @PrimaryKey @ColumnInfo(name = "id") var id: Int = 0 having fun clearDataByID(idList: List) to clear data from all three tables by the id in the idList Dao…
lannyf
  • 9,865
  • 12
  • 70
  • 152