mysql - How to simplify this subquery? -
i saw subquery http://www.programmerinterview.com/index.php/database-sql/derived-table-vs-subquery/ , has following subquery example, says written more simply. tell me how that? thank you!
a table called employee columns employee_name, last_name, employee_salary, , employee_number. want find employees have salary above average.
select employee_name employee employee_salary > (select avg(employee_salary) employee)
you can use over clause calculate average
salary , if current employee's salary bigger it. unfortunatelly, window
functions can not used in clause, should use comman table expression, too.
declare @employee table ( [employee_name] varchar(12) ,[employee_salary] int ) insert @employee ([employee_name], [employee_salary]) values ('empl1', 100) ,('empl2', 200) ,('empl3', 300) ,('empl4', 400) ,('empl5', 500) ,('empl6', 600) ,('empl7', 700) ,('empl8', 800) -- query select employee_name @employee employee_salary > (select avg(employee_salary) @employee) -- alternative query ;with datasource ( select employee_name ,employee_salary - avg(employee_salary) on () diff @employee ) select employee_name datasource diff > 0
Comments
Post a Comment