0

I have a query for a table, garage. Garages have cars linked to them through column (garage_id).

I want a postgresql query that will return all the garages, but also will return true in a seperate column if it has at least one car assigned to it.

For example:

Garage A name, no cars
Garage B name, 2 cars
Garage C name, 1 car
Garage D name, no cars

Should return:

Garage A name, false
Garage B name, true
Garage C name, true
Garage D name, false
0

1 Answer 1

1
select g.name, bool_or(c.garage_id is not null)
from
    garage g
    left join
    car c on g.garage_id = c.garage_id
group by g.name
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.