Premium problem65. Users Who Both Buy and Sell

Medium Locked

market_orders(order_id, buyer_id, seller_id, status, ordered_at)

Among orders in 2026 with status = 'completed', return the user_id of everyone who appears as a buyer at least once and as a seller at least once, ordered by user_id.

Somebody who only ever bought, or who only sold, is out. So is anybody whose only dual role came from a cancelled order.

Stacking the two roles into one list with UNION and keeping the users who turn up twice is shorter than joining the table to itself.

Tables

market_orders

order_id  buyer_id  seller_id  status     ordered_at
--------  --------  ---------  ---------  ----------
1         1         2          completed  2026-01-05
2         2         1          completed  2026-02-05
3         3         4          cancelled  2026-03-05
4         4         3          completed  2026-04-05
5         5         6          completed  2025-06-05
6         6         5          completed  2025-07-05

Expected result

user_id
-------
1
2

Premium problem

This one's part of Premium. Unlock the full MySQL track plus every other premium problem on the site.

Write one SELECT query