Skip to main content
edited body
Source Link
Alvin Thompson
  • 5.5k
  • 4
  • 28
  • 43

I'm assuming you know what they do, and thus are used differently, so I'm going to understand your question as: When would it be a good idea to rewrite the SQL to use IN instead of EXISTS, or vice versa.

Is that a fair assumption?


Edit: The reason I'm asking is that in many cases you can rewrite an SQL based on IN to use an EXISTS instead, and vice versa, and for some database engines, the query optimizer will treat the two differently.

For instance:

SELECT * FROM Customers WHERE ExistsEXISTS ( SELECT * FROM Orders WHERE Orders.CustomerID = Customers.ID ) 

can be rewritten to:

SELECT * FROM Customers WHERE ID IN ( SELECT CustomerID FROM Orders ) 

or with a join:

SELECT Customers.* FROM Customers INNER JOIN Orders ON Customers.ID = Orders.CustomerID 

So my question still stands, is the original poster wondering about what IN and EXISTS does, and thus how to use it, or does he ask wether rewriting an SQL using IN to use EXISTS instead, or vice versa, will be a good idea?

I'm assuming you know what they do, and thus are used differently, so I'm going to understand your question as: When would it be a good idea to rewrite the SQL to use IN instead of EXISTS, or vice versa.

Is that a fair assumption?


Edit: The reason I'm asking is that in many cases you can rewrite an SQL based on IN to use an EXISTS instead, and vice versa, and for some database engines, the query optimizer will treat the two differently.

For instance:

SELECT * FROM Customers WHERE Exists ( SELECT * FROM Orders WHERE Orders.CustomerID = Customers.ID ) 

can be rewritten to:

SELECT * FROM Customers WHERE ID IN ( SELECT CustomerID FROM Orders ) 

or with a join:

SELECT Customers.* FROM Customers INNER JOIN Orders ON Customers.ID = Orders.CustomerID 

So my question still stands, is the original poster wondering about what IN and EXISTS does, and thus how to use it, or does he ask wether rewriting an SQL using IN to use EXISTS instead, or vice versa, will be a good idea?

I'm assuming you know what they do, and thus are used differently, so I'm going to understand your question as: When would it be a good idea to rewrite the SQL to use IN instead of EXISTS, or vice versa.

Is that a fair assumption?


Edit: The reason I'm asking is that in many cases you can rewrite an SQL based on IN to use an EXISTS instead, and vice versa, and for some database engines, the query optimizer will treat the two differently.

For instance:

SELECT * FROM Customers WHERE EXISTS ( SELECT * FROM Orders WHERE Orders.CustomerID = Customers.ID ) 

can be rewritten to:

SELECT * FROM Customers WHERE ID IN ( SELECT CustomerID FROM Orders ) 

or with a join:

SELECT Customers.* FROM Customers INNER JOIN Orders ON Customers.ID = Orders.CustomerID 

So my question still stands, is the original poster wondering about what IN and EXISTS does, and thus how to use it, or does he ask wether rewriting an SQL using IN to use EXISTS instead, or vice versa, will be a good idea?

added clarification for why I'm asking
Source Link
Lasse V. Karlsen
  • 393.9k
  • 107
  • 651
  • 845

I'm assuming you know what they do, and thus are used differently, so I'm going to understand your question as: When would it be a good idea to rewrite the SQL to use IN instead of EXISTS, or vice versa.

Is that a fair assumption?


Edit: The reason I'm asking is that in many cases you can rewrite an SQL based on IN to use an EXISTS instead, and vice versa, and for some database engines, the query optimizer will treat the two differently.

For instance:

SELECT * FROM Customers WHERE Exists ( SELECT * FROM Orders WHERE Orders.CustomerID = Customers.ID ) 

can be rewritten to:

SELECT * FROM Customers WHERE ID IN ( SELECT CustomerID FROM Orders ) 

or with a join:

SELECT Customers.* FROM Customers INNER JOIN Orders ON Customers.ID = Orders.CustomerID 

So my question still stands, is the original poster wondering about what IN and EXISTS does, and thus how to use it, or does he ask wether rewriting an SQL using IN to use EXISTS instead, or vice versa, will be a good idea?

I'm assuming you know what they do, and thus are used differently, so I'm going to understand your question as: When would it be a good idea to rewrite the SQL to use IN instead of EXISTS, or vice versa.

Is that a fair assumption?

I'm assuming you know what they do, and thus are used differently, so I'm going to understand your question as: When would it be a good idea to rewrite the SQL to use IN instead of EXISTS, or vice versa.

Is that a fair assumption?


Edit: The reason I'm asking is that in many cases you can rewrite an SQL based on IN to use an EXISTS instead, and vice versa, and for some database engines, the query optimizer will treat the two differently.

For instance:

SELECT * FROM Customers WHERE Exists ( SELECT * FROM Orders WHERE Orders.CustomerID = Customers.ID ) 

can be rewritten to:

SELECT * FROM Customers WHERE ID IN ( SELECT CustomerID FROM Orders ) 

or with a join:

SELECT Customers.* FROM Customers INNER JOIN Orders ON Customers.ID = Orders.CustomerID 

So my question still stands, is the original poster wondering about what IN and EXISTS does, and thus how to use it, or does he ask wether rewriting an SQL using IN to use EXISTS instead, or vice versa, will be a good idea?

Source Link
Lasse V. Karlsen
  • 393.9k
  • 107
  • 651
  • 845

I'm assuming you know what they do, and thus are used differently, so I'm going to understand your question as: When would it be a good idea to rewrite the SQL to use IN instead of EXISTS, or vice versa.

Is that a fair assumption?