Requesting help with creating an SQL query to select similar records

earwig75
earwig75 used Ask the Experts™
on
This is a little hard to explain but I will do my best.  I need to write/update the example query below  so that it will return the records requested, but also return records based on what was returned. In my example below, two records are returned with "carnumber 55". I want to show those 2 records, but also any other records with the same "carname". In my example below, 3 additional records are displayed because they have the same car name as those returned from the original query. Can someone assist?

Select id, carnumber, carname
from tblOne 
where carnumber = 55

What I want returned from the new query

id   |   carnumber   |   carname
1    |      55       |   bobs car
5    |      55       |   johns car
7    |      88       |   bobs car
8    |      77       |   bobs car
21   |      97       |   johns car

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Muhammad BurhanManager I.T.
Top Expert 2015

Commented:
try this
select * from table01
where carname = 'bobs car'
order by carnumber

Author

Commented:
@muhammad, I will not have the carname in my query, I will only have the carnumber, that's why I need to use the results from the initial query to get the carname.
Senior DBA
Most Valuable Expert 2018
Top Expert 2014
Commented:
Select id, carnumber, carname
from tblOne
where carname in (select carname from tblOne where carnumber = 55)

Author

Commented:
Thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial