Home > Back-end >  Add join to display items based on key
Add join to display items based on key

Time:01-13

I have these Postgres tables:

create table employees
(
    id              bigint primary key,
    account_id      number,
    first_name      varchar(150),
    last_name       varchar(150)
);

create table accounts
(
    id               bigint primary key,
    account_name     varchar(150) not null
);

I need to search in table employees by account_id and print result the rows which match in table accounts.id. How I can do this using JOIN?

CodePudding user response:

I'm pretty sure this is what you're looking for.

SELECT a.id, a.account_name, e.first_name, e.last_name
FROM employees as e
    JOIN accounts as a on a.id = e.account_id
WHERE e.account_id = 3

This will allow you to search for specific account IDs in the employee table and bring back their corresponding account table information.

You can check this with my dbfiddle here - https://www.db-fiddle.com/f/pwzwQTsHuP27UDF17eAQy4/0

  •  Tags:  
  • Related