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)
This depends on the ORM you're using and how intelligent it is at optimizing your queries for your backend.
Entity Framework can generate some pretty awful SQL if you don't do your linq perfectly, so I'd assume query2 is better.

The only way for you to know for sure would be to inspect the SQL being generated by the two queries.
Eyeballing it, it looks like query1 would result in both tables being pulled in their entirety and then being filtered against each other in your application, while query2 will for sure generate an INNER JOIN in the query, which will let SQL Server do what it does best - set logic.

Is that FK\_IdCategory field a member of an actual foreign key index on that table? If not, make it so (and include the name column as an included column in the index) and your query will be very highly performant.