Q:

Join two of the same tables to another table and output the info of the (same) table in the same row

Sorry for the bad/long title but I don't know how else to put it.
What I want to do is join to 'A' tables and join it to the 'B' table where both 'A' have a foreign key in common and display info from both 'A' tables in the same row while preventing duplicates such as the example in the pic:

I know the query is just doing it's job, but is there a way to prevent 'duplicates' by comparing between the rows before output?
Here's what I tried, I know it may be bad performance-wise and there may be better ways but this is for a mini-project with a small DB, where performance shouldn't really matter:
SELECT w.emp_id AS emp1_id, w2.emp_id AS emp2_id, 
    e.fname || ' ' || e.lname AS emp1_name, e1.fname || ' ' || e1.lname AS emp2_name,
    e.jobtitle AS emp1_jobtitle, e1.jobtitle AS emp2_jobtitle, e2.fname || ' ' || e2.lname AS cs_name
FROM work_on w 
    LEFT JOIN work_on w2 
        on w.emp_id != w2.emp_id and w.ticket_id = w2.ticket_id 
    LEFT JOIN employee e
        on w.emp_id = e.emp_id
    LEFT JOIN employee e1
        on w2.emp_id = e1.emp_id
    LEFT JOIN ticket t 
        on t.ticket_id = w.ticket_id
    LEFT JOIN customer_problem p 
        on p.problem_id = t.problem_id
    LEFT JOIN employee e2
        on e2.emp_id = p.emp_id
WHERE e2.emp_id = 20 and p.submit_date >= '2018-04-08' 
    and p.submit_date <= '2018-04-11' and e1.emp_id != e.emp_id
ORDER BY w.emp_id;

My tables:
Employee: | Work_On:   | Ticket:      | Problem
----------+------------+--------------+------------
emp_id      work_id      ticket_id      problem_id
fname       emp_id       problem_id     emp_id
lname       ticket_id

In this case I'm trying to combine two Employee on Work_On where they have the Ticket in common and another Employee which connects to the ticket via the Problem table.

A:

Here is one option using least/greatest:
SELECT DISTINCT
    LEAST(w.emp_id, w2.emp_id) AS emp1_id,
    GREATEST(w.emp_id, w2.emp_id) AS emp2_id,
    LEAST(e.fname || ' ' || e.lname, e1.fname || ' ' || e1.lname) AS emp1_name,
    GREATEST(e.fname || ' ' || e.lname, e1.fname || ' ' || e1.lname) AS emp2_name,
    LEAST(e.jobtitle, e1.jobtitle) AS emp1_jobtitle,
    GREATEST(e.jobtitle, e1.jobtitle) AS emp2_jobtitle,
    e2.fname || ' ' || e2.lname AS cs_name
FROM work_on w 
LEFT JOIN work_on w2 
    ON w.emp_id != w2.emp_id AND w.ticket_id = w2.ticket_id 
LEFT JOIN employee e
    ON w.emp_id = e.emp_id
LEFT JOIN employee e1
    ON w2.emp_id = e1.emp_id
LEFT JOIN ticket t 
    ON t.ticket_id = w.ticket_id
LEFT JOIN customer_problem p 
    ON p.problem_id = t.problem_id
LEFT JOIN employee e2
    ON e2.emp_id = p.emp_id
WHERE
    e2.emp_id = 20 AND
    p.submit_date >= '2018-04-08' AND
    p.submit_date <= '2018-04-11' AND
    e1.emp_id != e.emp_id
ORDER BY w.emp_id;

To see why the least/greatest trick works, consider the following two records/columns:
emp1_id | emp2_id
2       | 15
15      | 2

It should be clear that while these records are distinct now, if we instead choose the least id followed by the greatest id, they appear identical:
LEAST(emp_id1, emp_id2) | GREATEST(emp_id1, emp_id2)
2                       | 15
2                       | 15

Then, using SELECT DISTINCT removes one of the two duplicate rows.

