Skip to content
Advertisement

How to delete records from a table if they don’t meet a condition in another table

I have two tables, company and company_name. company contains the columns: company_id(primary key) and company_name, while company_name contains the columns: company_id(foreign key from company table), company_name, and is_proper. Currently, the company table is filled with several records, but I wish to only keep company_names that appear in both tables and records where is_proper = 1.

Example company Table

Example company_name Table:

What I want company Table to look like:

If someone knows a SQL command to do this, it would be very helpful. Thanks in advance.

Advertisement

Answer

With NOT EXISTS:

See the demo.
Results:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement