Saurav
Saurav

Reputation: 13

Show the names of employees of Company1 having salary greater than every employee of Company2 (SQL)

I have a Table Emp_company which has many columns and one of them is Salary. I need to know how to compare the Salaries of the employees from Company1 to the salaries of employees from Company2. Please help me if you can.

Upvotes: 0

Views: 128

Answers (2)

benwad
benwad

Reputation: 6594

You can get the max of Company2 salaries like this:

SELECT MAX(salary) FROM company_table WHERE cname="company2";

...then get that parameter and find those at company1 who earn more:

SELECT * FROM company_table WHERE cname="company1" AND salary > x;

...where x is the result of the first query.

EDIT: edited to use same table for both companies, as mentioned by poster in the comments

EDIT: the full query all on one line:

SELECT * FROM company_table WHERE cname="company1" AND salary > (SELECT MAX(salary) FROM company_table WHERE cname="company2");

Upvotes: 0

Jain
Jain

Reputation: 1249

Select * from Company1 WHERE Company1.Salary > (Select Max(company2.salary) from company2)

Solution for Sourav: TRY this :

Select * from Company WHERE Salary > (Select salary from company WHERE cname = 'Company2' ORDER by salary desc LIMIT 1) AND cname = 'company1'

Upvotes: 1

Related Questions