Search code examples
oracle-databasejoinstored-proceduresoracle-sqldeveloperbind-variables

How to create a procedure by joining two tables that doesn't have a common column in oracle sql?


Table 1: EMP

Columns: EMPNO,ENAME,SAL,JOB,DEPTNO

Table 2 : SALGRADE

Columns : GRADE,HISAL,LOSAL

Given the EMPNO of an employee as input to the procedure, I'll have to get the corresponding Grade and SAL as output.


Solution

  • Join on the sal being between the losal and hisal. Something like:

    SELECT g.grade,
           e.sal
           FROM emp e
                LEFT JOIN salgrade g
                          ON g.losal <= e.sal
                             AND g.hisal >= e.sal
           WHERE e.empno = ?;