Boris R
Boris R

Reputation: 79

SQLite select from table based on other table

I have 2 tables, one is a list of companies and the other a list of products. The products are linked to a company in the column 'company_id'

To better understand, this is what I want to do:

SELECT * FROM Products WHERE company_id = (Company.id where name = Google)

What would be the correct way of writing this statement?

Upvotes: 2

Views: 3864

Answers (1)

Shadow
Shadow

Reputation: 34305

Write it either as a subselect

SELECT * FROM Products WHERE company_id =(SELECT id FROM companies WHERE name = 'Google' limit 1)

Or with subselect with in() operator

SELECT * FROM Products WHERE company_id in (SELECT id FROM companies WHERE name = 'Google')

Or write it as a join

SELECT p.* 
FROM Products p
inner join companies c on p.company_id=c.id
WHERE c.name='Google'

Upvotes: 5

Related Questions