Question: I have the following query:

```
select * from email_contacts
where extract(month from created_at) = 4
and call_type = 'Membership NPS'
and email NOT IN
(
    select invite_email from nps_responses
    where extract(month from fs_created_at) = 4
    and invite_email is not null
)

```

Then I have a rails 4 app that has two corresponding models, `EmailContact` and `NpsResponse`.

How can I convert this query to run within my controller method in Rails? I need to select all `email_contacts` with the above criteria.

Answer: 
Performance of the two queries really depends on which LINQ provider and which RDBMS you're using. Assuming SQL Server, the first would generate the following query:

```
select i.name, c.name
from Item i, Category c
where i.FK_idCategory = c.IdCategory

```

Whereas the second would generate:

```
select i.name, c.name
from Item i
inner join Category c
    on i.FK_idCategory = c.IdCategory

```

Which operate exactly the same in SQL Server as is explained in: [Explicit vs implicit SQL joins](https://stackoverflow.com/questions/44917/explicit-vs-implicit-sql-joins)
With linq2Sql or EntityFramework, you would probably do something like this:

```
var query = from i in db.Item
            select new {i.name, i.Category.Name}

```

This will generate a proper SQL inner join. 

I do assume that there is a foreign key relation between Item and Category defined.