Reputation: 479
In the database there are two tables:
secondNames:
id | name
--------------
1 | John
2 | Peter
3 | Michael
names:
id | name | secondNames_id
----------------------------
1 | Jack | 2
2 | Harry | 2
3 | James | 1
Say I have name from secondName table and want to select names of people who has that second name. Being a begginer at SQL I do like this:
SELECT id FROM secondNames WHERE name = 'some name';
SELECT name FROM names WHERE secondNames_id = 'id from first select';
Is it possible to do this in one query and how?
Upvotes: 1
Views: 55
Reputation: 180020
You can just use a scalar subquery:
SELECT name
FROM names
WHERE secondNames_id = (SELECT id
FROM secondNames
WHERE name = 'some name');
Upvotes: 1
Reputation: 46
This should work:
Select n.name
from secondNames as sn
inner join names as n on n.secondNames_ID = sn.id
where sn.name = 'some name'
Upvotes: 3