How do I have a select query inside a where clause?

Hello,

I am trying to implement the below query in Knime using nodes.

Select * from TableA where column1 in (Select column2 from TableB)

The query “Select column2 from TableB” needs to be in a node of its own. So how do I use the output of the node in a where clause

Hi @cesc34, and welcome to the KNIME community.

If the “TableB” data is already in a KNIME table, but you wish to use it in the SQL query, your options are to either manually turn the “Column2” values into a comma delimited string, held in a flow variable, which can then be used in the select statement against TableA, or an alternative is to use the DB Looping node.

Take a look at this thread, where both these approaches are referenced:

If the data in TableA is not massive, you could also simply return all the rows from TableA using a DB Query Reader and then use a Joiner node to join column1 to the column2 data that has been returned from TableB, but whilst less efficient, this approach can be perfectly valid, for a “quick win”.

2 Likes

Hi,
You can also use the DB Query Extractor node to turn the Select column2 from TableB into a flow variable. Then do the same with the Select * from TableA (both queries can be generated using KNIME DB nodes). Now you have two parts of your query as strings and you can use a String Manipulation node to connect them with the IN operator. Then you can use the DB Query Injector to turn the new query into a DB Data port. The whole logic can easily be wrapped into a component for reusability.
Kind regards,
Alexander

1 Like

The data in TableA is very large, so the joiner node is taking a long time and that’s the reason I am trying to explore different options. Let me check the thread and see if that helps

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.