0
votes

There are two tables in a database as follows (Note that the 2 tables don't have any key in common.):-

Employee Table

Eid        Ename      Salary

001      John          15000

002      Mac           20000

003      Michael      6000

004      Randy        30000

005      Mandy        NULL

Salary Table

MinSal        MaxSal      Grade

1000            10000         A

11000          20000         B

21000          30000         C

31000          40000         D


Now the Output required is something like

Employee Details

Eid        Ename      Salary            Grade

001        John          15000              B

002        Mac           20000              B

003        Michael      6000               A

004        Randy       30000              C

005        Mandy       N/A               N/A

What is the logic or technique that is used to obtain the req. result. What can be the SQL Code for this.

3
John should get B, not A, right?ypercubeᵀᴹ

3 Answers

6
votes

You need a non-equi-join in your case:

select e.eid, e.ename, e.salary, s.grade
  from employee e inner join salary s on (e.salary between s.minsal and s.maxsal)

You might need to add a special case for your N/A case. Maybe a left outer join? It might help if you specify what's the RDBMS you are using.

0
votes

You need to use JOIN

Here's a useful page on how to use it: http://www.tizag.com/sqlTutorial/sqljoin.php

0
votes

I'd seriously suggest linking them, it would result in a better architecture and make coding easier.

However if you don't want to do that you can get the desired result using if statements, one for each of the pay grades.