sqlpostgresqlselect-query

Postgres query from two different table


Hi i have queries that I use to get single result. How could I do this with just one query?

First I ask

SELECT "userId" FROM "Devices" WHERE "deviceNumber" = '0001563055080020'

Then save UserId and ask following another table

SELECT "email" FROM "Users" WHERE "id" = '1'

Any ideas?


Solution

  • NESTED QUERY

    SELECT "email" 
    FROM "Users" 
    WHERE "id" IN (
        SELECT "userId" 
        FROM "Devices" 
        WHERE "deviceNumber" = '0001563055080020')
    

    INNER JOIN

    select u.email
    from users u, devices d
    where d.userid = u.id
    and d.deviceNumber = '0001563055080020'