Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute "IN" subquery only once

I have a slow SQL Server query:

SELECT Style
FROM Storage
WHERE Style NOT IN (SELECT ValidStyle FROM ValidStyles)

This syntax causes SELECT ValidStyle FROM ValidStyles to execute once for each row of the parent query. How can I rewrite this to run the subquery one time only?

like image 546
Chris Schiffhauer Avatar asked Oct 30 '25 14:10

Chris Schiffhauer


1 Answers

SELECT Style 
FROM Storage s
WHERE NOT EXISTS(SELECT * FROM ValidStyles vs WHERE vs.ValidStyle=s.Style)
like image 190
Tab Alleman Avatar answered Nov 01 '25 08:11

Tab Alleman