I have the following scenario where I have 2 tables and I am joining them to get some results from the second table, however I want to keep the similar column values in the order shown in the main table:
Below you can see a basic example of the issue:
EMP table DEPT table
They both have a common column called deptno (department number). When I query the department names from the DEPT table the result is the following:
Original
ACCOUNTING
RESEARCH
SALES
OPERATIONS
When I join the 2 tables as follows:
SELECT DISTINCT d.DNAME
FROM DEPT d
LEFT JOIN EMP e ON d.deptno = e.deptno
The result is the following:
JOIN
RESEARCH
SALES
ACCOUNTING
OPERATIONS
If, I sort the joined query, the result is the following:
SORTED
ACCOUNTING
OPERATIONS
RESEARCH
SALES
The question here is, How do I get the data sorted as follows when joining these 2 tables?
Original
ACCOUNTING
RESEARCH
SALES
OPERATIONS