You can build a single table containing the BOM for all products and add 1 column for the product name if it is not yet in that table. Now you can join this table with itself. The first join checks that for all rows of product1 you will have a matching row for product2. You do the same with product2 -> product1. Whatever remains are the matching products. You may have to count the number of rows for each product before and after the filtering and then compare those numbers to make sure nothing was filtered-out during the row-wise comparison.