These are my table details:
Table1
id name
----------------
101 syed
102 shaik
Table2
l_id sup_id
-----------------
101 102
id name sup_id sup_name
------------------------------
101 syed 102 shaik
In table-1 I have two rows, 101 and 102 which is a master table. Table 2 consists of supervisor login ids with their subordinate ids. The common column between table1 and table2 is id & l_id. So, I'm trying to pull the data as follows to get the details.
My query is as follows
SELECT
r.id,
trim(r.name) as name, trim(a.sup_id) as sup_id,
trim(select name from schema.table1
where id in (a.sup_id)) as sup_name
FROM
schema.table1 r
JOIN
schema.table2 a ON a.l_id = r.id
WHERE
r.id IN (101)
I'm able to get till supervisor id, but not supervisor name.
Any ideas would be greatly appreciated
person id - person name - supervisor id - supervisor name
table1andtable22 times by differentidfromtable2?join, but should use alias for each occurrence (JOIN tabname as tabalias). No need to do subquery in select, it often performs bad.