To display the names of employees who earns more than the average salary of allemployees.SELECT last_name, first_nameFROMemployeeWHEREsalary > AVG(salary);Which change should you make to achieve the desired results?Select one:a.Use a subquery in the WHERE clause to compare the average salary value.b.Change the function in the WHERE clause.c.Move the function to the SELECT clause and add a GROUP BY clause.d.Move the function to the SELECT clause and add a GROUP BY clause and a HAVING clause.
Question
To display the names of employees who earns more than the average salary of allemployees.SELECT last_name, first_nameFROMemployeeWHEREsalary > AVG(salary);Which change should you make to achieve the desired results?Select one:a.Use a subquery in the WHERE clause to compare the average salary value.b.Change the function in the WHERE clause.c.Move the function to the SELECT clause and add a GROUP BY clause.d.Move the function to the SELECT clause and add a GROUP BY clause and a HAVING clause.
Solution
The correct answer is:
a. Use a subquery in the WHERE clause to compare the average salary value.
The reason is that you cannot directly compare a single value (in this case, the salary of an individual employee) with an aggregate function (like AVG) in the WHERE clause. Instead, you should use a subquery to first calculate the average salary of all employees, and then use this value in the WHERE clause to filter out the employees who earn more than this average value.
So, the corrected SQL query would look like this:
SELECT last_name, first_name FROM employee WHERE salary > (SELECT AVG(salary) FROM employee);
Similar Questions
SELECT c.nameFROM COMPANY cJOIN EMPLOYEE e ON c.company_id= e.company_idJOIN SALARY s ON e.employee_id = s.employee_idGROUP BY c.nameHAVING AVG(s.salary) >=40000
Raj, a Database Administrator, needs to display the average pay of workersfrom those departments which have more than five employees. He isexperiencing a problem while running the following query:SELECT DEPT, AVG(SAL) FROM EMP WHERE COUNT(*) > 5GROUP BY DEPT;Which of the following is a correct query to perform the given task?i. SELECT DEPT, AVG(SAL) FROM EMP WHERE COUNT(*) > 5GROUP BY DEPT;ii. SELECT DEPT, AVG(SAL) FROM EMP HAVING COUNT(*) >5 GROUP BY DEPT;iii. SELECT DEPT, AVG(SAL) FROM EMP GROUP BY DEPTWHERE COUNT(*) > 5;iv. SELECT DEPT, AVG(SAL) FROM EMP GROUP BY DEPTHAVING COUNT(*) > 5;
Problem StatementThe finance department wants to analyze the distribution of salaries across different departments. Write a query to calculate the average salary for each department and display the department name along with the average salary.Table details are given below:The table is created, and the records are already inserted at the backend. The sample records are given below.Note:Table names and Field Names are case-sensitiveInput format :The input records are already prepopulated, as given in the problem statement.Output format :The output displays the details of the average salary for each department as shown below.Department_name Average_SalaryEngineering 59000.000000Marketing 70000.000000
What will the following SQL query return? SELECT AVG(salary) AS average_salary FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE department_name = 'IT');Select one:a. The number of employees in the IT department.b. The total salary of employees in the IT department.c. The highest salary of employees in the IT department.d. The average salary of employees in the IT department.
1. Write a query to find all employees who earn more than the average salary.2. Write a query to retrieve the names of employees who work in the same department as ‘John’.3. Write a query to display the second highest salary from the Employee table.4. Write a query to find all customers who have made more than five orders.5. Write a query to count the number of orders placed by each customer.6. Write a query to retrieve the list of employees who joined in the last 6 months.7. Write a query to find the total sales amount per product.8. Write a query to list all products that have never been sold.9. Write a query to update the salary of employees based on their performance rating.10. Write a query to delete duplicate rows from a table.11. Write a query to find the second highest salary in a company without using the MAX function twice.12. Write a query to identify the top 10 customers who have not placed an order in the last year.13. Create a query to compute the year-over-year growth rate of revenue for each product category.14. Write a query to join three tables and filter the results to show only records that exist in exactly two of the tables.15. Construct a query to find the median sales amount for each region.16. Write a query to calculate the retention rate of customers over a given time period.17. Write a query to find duplicate records in a table and count the number of duplicates for each unique record.18. Create a query to find employees who have been with the company for more than 5 years but have never been promoted.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.