By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table company (company_id int, company_name varchar(50));
insert into company(company_id, company_name) values
(1, 'Apple'),
(2, 'Apple Inc.'),
(3, 'Fcebook'),
(4, 'Facebook'),
(5, 'Facebook Inc.'),
(6, 'Google Inc.');
create table company_name(company_id int, company_name varchar(50), is_proper bit);
insert into company_name(company_id, company_name, is_proper) values
(2, 'Apple', 0),
(2, 'Apple Inc.', 1),
(5, 'Fcebook', 0),
(5, 'Facebook', 0),
(5, 'Facebook Inc.', 1),
(6, 'Google Inc.', 1);
12 rows affected
delete c from company c
where not exists (
select 1 from company_name
where company_id = c.company_id and company_name = c.company_name and is_proper = 1
)
3 rows affected
select * from company
company_id | company_name |
---|---|
2 | Apple Inc. |
5 | Facebook Inc. |
6 | Google Inc. |