How to delete records in one table based on the values in another table?

how to delete records from one table that match another in access
delete records from one table that do not exist in another
access delete query based on another table
mysql delete records based on another table
delete from table based on another table access
delete from table a if matching row exists in b
delete from table where column not in another table
delete data from two table using join

Here are two tables:

table1

cm_id   cost
1       6.52
2       16.52
3       2.12
4       7.14
5       19.09
6       11.52
7       0.12

table2

um_id   order_num   name
1       517         tommy
2       518         bobby
3       519         scotty
4       520         faris
5       521         justine
6       522         sadie
7       523         nicole

cm_id and um_id represent the same thing so the cost can be tied to each order number, ie

SELECT table1.cm_id, table1.cost, table2.order_num, table2.order_num
FROM table1, table2
WHERE table1.cm_id=table2.um_id;

What is the single SQL statement I can use to delete rows from table1 where the order_num in table2 is between 518 and 520?

delete 
from table1
where cm_id IN (select um_id from table2 where order_num between 518 and 520)

Delete all rows in a table based on another table, Only MERGE is in SQL standard for deleting (or updating) rows while joining something on target table. merge table1 t1 using ( select t2.ID from table2 t2 ) as d on t1.ID = d.ID when matched then delete; MERGE has a stricter semantic, protecting from some error cases which may go unnoticed with DELETE FROM . I want to delete the information from one table but I am using a criteria from the other table to filter the records that I want. Access will not let me do this since cannot delete from both tables. For example: I have one table with a date field that I use for my criteria. In the other table I want to delete those records that match this date.

DELETE table1
FROM   table1 INNER JOIN table2 ON table1.cm_id = table2.um_id
AND    (table2.order_num BETWEEN 518 AND 520)

--OR

DELETE 
FROM  table1
USING table1 INNER JOIN table2 ON table1.cm_id = table2.um_id
WHERE   (table2.order_num BETWEEN 518 AND 520)
EDIT:

There was a duplicate FROM and query has been changed as per Andriy M comments.

How to delete records in one table based on the values in another , How to delete records in one table based on the values in another table? SELECT table1. cm_id, table1. cost, table2. While the OP doesn't want to use an 'in' statement, in reply to Ankur Gupta, this was the easiest way I found to delete the records in one table which didn't exist in another table, in a one to many relationship: DELETE FROM Table1 as t1 WHERE ID_Number NOT IN (SELECT ID_Number FROM Table2 as t2) Worked like a charm in Access 2016, for me.

I prefer this way

delete from table1
using table1, table2
where table1.cm_id = table2.um_id
and table2.order_num >= 518
and table2.order_num <= 520;

[SOLVED] Delete rows from a SQL table based on another table , You have to have an ON clause for a join. Delete rows from a SQL table based on another table. Colts I've got the query in a string value as such: SQL. I need to insert and delete records in table 1 based on the data in multiple columns in table 2. Example: Table1 and Table2 have identical columns: address, city, state, and zip. Table1 is the master table with millions and millions of records.

use DELETE with subquery:

DELETE FROM table1 WHERE table1.cm_id IN (SELECT table2.um_id FROM table2 WHERE order_num>=518 and order_num<=520)

SQL delete records using subqueries, SQL delete records using subqueries with alias. In this page, we are going to discuss, how table aliases( when two or more tables used in a query� The Yardwide Table is updated every week and the Planning Table is updated based on changes to the Yardwide Table. The Planning Table is a subset of the Yardwide Table and contains some fields from the Yardwide Table. I want to delete records from the Planning Table based on a change in the User Status field in the Yardwide Table.

As IN has some performance limitations we can use delete command with simple join query like

delete x from table1 x,table2 y where x.cm_id=y.um_id;

SQL: DELETE Statement, This SQL tutorial explains how to use the SQL DELETE statement with syntax, You may wish to delete records in one table based on values in another table. DELETE FROM tbl1 FROM tbl1 LEFT OUTER JOIN tbl2 ON tbl1.PK1 = tbl2.PK1 AND tbl1.PK2 = tbl2.PK2 AND tbl2.DateStr >= GETDATE() - 365 WHERE tbl2.PK1 IS NULL AND tbl2.PK2 IS NULL Yes, you have two FROM clauses with a multi-table DELETE statement. This is essentially just an anti-join turned into a deletion.

PostgreSQL DELETE - Deleting Rows in a Table, To delete data from a table, you use PostgreSQL DELETE statement as shown to one or more columns in another table, you use the USING clause as follows: The following statement deletes the rows whose value in the id column is 8:. Emp ID 1 has two occurrences in the Employee table Emp ID 3 has three occurrences in the Employee table We require to keep a single row and remove the duplicate rows. We need to remove only duplicate rows from the table. For example, the EmpID 1 appears two times in the table. We want to remove only one occurrence of it.

Microsoft Access Delete Query, SQL Syntax, Examples and Errors, Empty a table (delete all its records); Delete all records with a particular value in a field; Delete all records with a range of values in a field; Delete records based� You may wish to delete records in one table based on values in another table. Since you can't list more than one table in the SQL Server FROM clause when you are performing a delete, you can use the SQL Server EXISTS clause. For example:

MySQL: Delete records in one table that are not in another, Example tables. The examples below are the same as in last week's post and use three tables as follows: content: contains the content pages for a website�

Comments
  • I believe this should answer your question. Just use a join.
  • Thanks for the reference, I've got a lot of learning to do!
  • Duplicates stackoverflow.com/questions/1590799/…
  • Why not? What would be efficient? Is there another way?
  • This would work in SQL Server. In MySQL you should either a) remove the first FROM or b) replace the second FROM with USING.
  • Also, how do these two queries differ from each other? (I mean, apart from the fact that the second one contains fewer spaces.)
  • Oh, I actually forgot to put WHERE clause to 2nd query, I'm changing accordingly, thanks
  • Just to clarify: when I was saying, ‘remove FROM… replace FROM…’, I meant just the keywords themselves, not the entire clauses. Well, you can see the right syntax in the linked article.
  • Oh, I understand now, so should I edit again? Thanks for your time.
  • DELETE FROM not DELETE * FROM