Wishlist Items Also in Cart
A shop tracks saved products in wishlist_items and products currently in the basket in cart_items. The same pair can appear more than once in either table.
Find each distinct (user, product) pair that is both wishlisted and in the cart.
Return columns user_id, product_id. Row order does not matter.
Schema
CREATE TABLE wishlist_items ( user_id INTEGER NOT NULL, product_id INTEGER NOT NULL, added_at TEXT NOT NULL ); CREATE TABLE cart_items ( user_id INTEGER NOT NULL, product_id INTEGER NOT NULL, quantity INTEGER NOT NULL );
Sample data
wishlist_items
| user_id | product_id | added_at |
|---|---|---|
| 1 | 501 | 2026-05-01 |
| 1 | 502 | 2026-05-02 |
| 2 | 501 | 2026-05-02 |
| 3 | 777 | 2026-05-03 |
| 2 | 600 | 2026-05-04 |
cart_items
| user_id | product_id | quantity |
|---|---|---|
| 1 | 502 | 1 |
| 2 | 501 | 2 |
| 3 | 778 | 1 |
| 4 | 501 | 1 |
| 2 | 600 | 1 |
Hint 1
INTERSECT returns the rows that appear in both queries and removes duplicates.
Hint 2
Both columns must match. User 11 with product 6 is not user 6 with product 11.
Good practice for: Data Analyst, Backend Developer, Digital Marketing Specialist, Product Manager
SQL (SQLite)
Run checks your query on the sample data (Ctrl + '). Submit also checks hidden databases.