I met the following MySQL code:
SELECT ServiceFee
FROM Shows
WHERE ID = (SELECT ShowID FROM Orders WHERE ID = ?)
It makes me wonder because the people who wrote this code usually use SQL joins. I would rewrite it
SELECT ServiceFee
FROM Shows
INNER JOIN Orders ON Shows.ID = Orders.ShowID
WHERE Orders.ID = ?
My question: Is there any reason why this code was written with a subquery and whether it is completely safe (producing the same result in all situations) to rewrite it with the join?
Are there any caveats?
"Is there any reason why this code was written with a subquery"
a very long time ago MySQL joins used to be slow
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With