mtbrooks1993
mtbrooks1993

Reputation: 3

SQL Comparing a value with two values in another table to return a value

I am trying to get a value from a table by comparing a value to two other values.

I have a table like

        Employee  
EmployeeID       Salary  
001---------------10  
002---------------25  

...
And a table like

         Salary  
LevelNo LowerLimit UpperLimit  
1--------01---------15  
2--------16---------30  

I need to take the salary from the Employee table and display the level number. I can get the lowest to display but not the rest. I am new to this so any help is greatly appreciated. This is my code so far using Sqlite3:

SELECT EmployeeID, Salary, LevelNO  
FROM Employee_T, SalaryLevel_T  
WHERE LevelNO = (Salary < UpperLimit AND Salary > LowerLimit);  

This only displays one EmployeeID, Salary, and LevelNO and it's the lowest.

Upvotes: 0

Views: 69

Answers (1)

juergen d
juergen d

Reputation: 204904

SELECT e.EmployeeID, e.Salary, s.LevelNO
FROM Employee_T e
JOIN SalaryLevel_T s ON e.Salary between s.LowerLimit and s.UpperLimit

If you don't want to use between you can do

JOIN SalaryLevel_T s ON e.Salary >= s.LowerLimit and e.Salary <= s.UpperLimit

Upvotes: 3

Related Questions