Staff Wages in SQL
Asked By: user2164535
Originally Asked On: 2014-01-05 22:23:06
Asked Via: stackoverflow
I’m Learning Databases and I have a question regarding queries:
How can I workout Average Wage including Total Wage and Wage Difference?
My table is as follows:
Staff: staffNo(PK), staffSalary
The query that I have written is
SELECT staffNo, staffSalary,staffSalary - ( SELECT AVG( staffSalary ) FROM Staff ) AS salDiff FROM Staff WHERE staffSalary > ( SELECT AVG( staffSalary ) FROM Staff )
This query only gives me the Average Salary difference and Staff Actual Salary, how can i find out the actual Salary Average for every column of data i have?
Also is it possible to use joins in this example?
He received 2 answers
eventually accepting:
Dan B’s answer to
Staff Wages in SQL
Assuming there is one row per employee, here is what I believe you are asking for (for all employees making above average):
select staffNo, (Select avg(staffsalary) from staff) as avgSalary, (select staffSalary from staff where staffNo = S.StaffNo) as Salary, (Select staffSalary - (select avg(staffSalary) from staff) from staff where staffNo = S.staffNo ) as salDiff from staff S where staffSalary > (Select avg(staffSalary) from staff)
Not sure why you need a join… unless you want to see salaries from all users, in which case you can simply remove the where clause and it will show you all staff members.
If the selected answer did not help you out, the other answers might!
All Answers For: Staff Wages in SQL
Tin Tran’s answer to
Staff Wages in SQL
try this
SELECT staffNo, staffSalary,staffSalary - (SELECT AVG(staffSalary) FROM Staff ) AS salDiff, (SELECT AVG(staffSalary) FROM Staff WHERE staffSalary > (SELECT AVG(staffSalary) FROM Staff )) as avSalForStaffGreaterThanAverageSal FROM Staff WHERE staffSalary > (SELECT AVG(staffSalary) FROM Staff )
Dan B’s answer to
Staff Wages in SQL
Assuming there is one row per employee, here is what I believe you are asking for (for all employees making above average):
select staffNo, (Select avg(staffsalary) from staff) as avgSalary, (select staffSalary from staff where staffNo = S.StaffNo) as Salary, (Select staffSalary - (select avg(staffSalary) from staff) from staff where staffNo = S.staffNo ) as salDiff from staff S where staffSalary > (Select avg(staffSalary) from staff)
Not sure why you need a join… unless you want to see salaries from all users, in which case you can simply remove the where clause and it will show you all staff members.
Of course, you should really check out the original question.
The post Staff Wages in SQL [ANSWERED] appeared first on Tech ABC to XYZ.