Skip to main content
Admissions guidance, study banks, and career tools — get started on Shalyam

Rewrite an IN Subquery as a Join

The query SELECT * FROM Orders WHERE customer_id IN (SELECT id FROM Customers WHERE city = 'Pune') is slow. Rewrite it as a join that returns the same rows.

Table schema

Customers(id INT PRIMARY KEY, name VARCHAR(50), city VARCHAR(50))
Orders(id INT PRIMARY KEY, customer_id INT, amount INT)

Sample data

Customers: (1, Aman, Pune), (2, Riya, Delhi)
Orders: (1, 1, 100), (2, 2, 50), (3, 1, 70)

Discussion

No discussions yet.

Rewards · v2.1

Get Exclusive Insights with Every Issue

Join Shalyam Newsletter

Stay ahead in education, research, and innovation—straight to your inbox.