Q (kdb): nested where query

Viewed 6007

What are the ways to in Q to use the results of a nested query in a where clause?

I'm looking for something similar to SQL statement.

select from food where type_id in (
    select type_id from types where type_name = "fruit"
)
3 Answers

while that's the direct answer to your question, the best way do this is probably foreign keys:

q)types:([type_id:`apple`orange`cucumber]type_name:`fruit`fruit`vegetable)
q)food:([type_id:`types$`apple`orange`cucumber]price:3?2.)
q)meta food
c      | t f     a
-------| ---------
type_id| s types  
price  | f        
q)select from food where type_id.type_name=`fruit
type_id| price    
-------| ---------
apple  | 0.4593231
orange | 1.383906 
q)
Related