Introduction
Sometimes you want to find common data between two lists, but MySQL does not have a direct INTERSECT command. We use other ways to get the same result.
SELECT column_list FROM table1 WHERE column_name IN (SELECT column_name FROM table2);
SELECT id FROM table1 WHERE id IN (SELECT id FROM table2);
SELECT name FROM employees WHERE name IN (SELECT name FROM managers);
CREATE TABLE products_a (id INT); CREATE TABLE products_b (id INT); INSERT INTO products_a VALUES (1), (2), (3); INSERT INTO products_b VALUES (2), (3), (4); SELECT id FROM products_a WHERE id IN (SELECT id FROM products_b);
table1 and table2 when INTERSECT is not supported?WHERE column IN (subquery) returns rows in table1 that also appear in table2.SELECT * FROM table1 WHERE column IN (SELECT column FROM table2) -> Option Cid values between tableA and tableB in MySQL?SELECT id FROM tableA WHERE id IN (SELECT id FROM tableB); to get common ids.table1:table2:SELECT id FROM table1 WHERE id IN (SELECT id FROM table2);
user_id values:SELECT user_id FROM users WHERE user_id IN (SELECT user_id FROM orders);
IN must return exactly one column.product_id values that appear in both sales and returns tables. Both tables have product_id and date columns. You only want products sold and returned on the same date. Which query correctly finds this intersection?(product_id, date) IN (SELECT product_id, date FROM returns) to find exact matches.Pass the test and this pattern moves unknown