jtmcc
jtmcc

Reputation: 3

SQL Referencing Table Values to Count Table

I'm not sure what this is called, but I'm trying to reference values in one table to count the number of rows in another table that are less than this referenced value.

An example, if we have an employee table (EmpSal) with Name and Salary, and a second table (Salary_Summary) with a range of Salary values. How do I count the number of entries in the EmpSal table that are less than each of the values in the Salary_Summary table?

Picture explaining this: enter image description here

Any help is greatly appreciated!

Upvotes: 0

Views: 27

Answers (1)

jarlh
jarlh

Reputation: 44796

Can easily be done with a correlated sub-query:

select s.salary, (select count(*) from EmpSal e where e.Salary < s.salary)
from Salary_Summary s

Upvotes: 1

Related Questions