I need to find the products which has invalid foreign key assigned.
Note :
- Company has many products
- Company has many categories
- Products has one category (foreign key = category_id)
SELECT *
FROM products
WHERE category_id NOT IN (SELECT id FROM categories);
will not help me because a company level check is required. Ie,
Company A
- has Categories c1, c2
- has Product P1 belongs to c3 (invalid), Product P2 belongs to c1 (valid)
Company B
- has category c1,c2, c3
So in the above scenario I need to find Product P1 which refers to category c3 which is not available at p1's company (Company A).