250 likes | 331 Views
DATABASE PROGRAMMING. Sections 5-7. Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming department. Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming department.
E N D
DATABASE PROGRAMMING Sections 5-7
Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming department.
Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming department. SELECT AVG(salary), MAX(salary),MIN(salary), SUM(salary) FROM employees WHERE job_id LIKE '%PROG%';
What is the oldest hire date, and the most recent hire date for all employees in the programming department?
What is the oldest hire date, and the most recent hire date for all employees in the programming department? SELECT MIN(hire_date), MAX(hire_date) FROM employees WHERE job_id like ‘%PROG%’;
How many employees are there in department 60? How many departments are there? (Use complete employees table, not just the partial table shown here)
How many employees are there in department 60? SELECT COUNT(*) FROM employees WHERE department_id = 60; How many departments are there (using all of the employees table found in HTML DB)? SELECT COUNT(DISTINCT department_id) FROM employees;
Show the department_id and average salary for each department.
Show the department_id and average salary for each department . SELECT department_id, AVG(salary) FROM employees GROUP BY department_id;
Show the department_id, job_id, and total salary for each department.
Show the department_id, job_id and the total salary for each job within the department (using full table instead of partial table above) . SELECT department_id, job_id, SUM(salary) FROM employees GROUP BY department_id, job_id;
Show the department_id, and average salary for all departments where the average salary is greater $10,000. (Use full employees table found in HTML DB)
Show the department_id, and average salary for all departments where the average salary is greater $10,000. (Use full employees table found in HTML DB) This results in an error: “group function not allowed here” SELECT department_id, AVG(salary) FROM employees WHERE AVG(salary) > 10000 GROUP BY department_id; SELECT department_id, AVG(salary) FROM employees HAVING AVG(salary) > 10000 GROUP BY department_id
Show the department_id, and average salary for all departments where the maximum salary is greater $10,000. (Use full employees table found in HTML DB)
Show the department_id, and average salary for all departments where the maximum salary is greater $10,000. (Use full employees table found in HTML DB) SELECT department_id, AVG(salary) FROM employees GROUP BY department_id HAVING MAX(salary) > 10000
Show the job id and the total salary for all jobs that aren’t in programming where the total salary is greater than $10,000. Arrange the data by the total salary in ascending order.
Show the job id and the total salary for all jobs that aren’t in programming where the total salary is greater than $10,000. Arrange the data by the total salary in ascending order. SELECT job_id, SUM(salary) FROM employees WHERE job_id NOT LIKE '%PROG%' GROUP BY job_id HAVING SUM(salary) > 10000 ORDER BY SUM(salary)
Assuming you don’t know what Matos’ salary is, write a query which will show all employees’ with a salary less than his salary.
From subquery • SELECT last_name • FROM employees • WHERE salary < • (SELECT salary • FROM employees • WHERE last_name = 'Matos'); Assuming you don’t know what Matos’ salary is, write a query which will show all employees’ with a salary less than his salary.
Find the job with the lowest average salary.(Use the full employees table)
SELECT job_id, AVG(salary) FROM employees GROUP BY job_id HAVING AVG(salary) = (SELECT MIN(AVG(salary)) FROM employees GROUP BY job_id); From Subquery Result of full query Find the job with the lowest average salary.(Use the full employees table)
Why will the following query not work? • SELECT employee_id, last_name • FROM employees • WHERE salary = • (SELECT MIN(salary) • FROM employees • GROUP BY department_id)
Why will the following query not work? • SELECT employee_id, last_name • FROM employees • WHERE salary = • (SELECT MIN(salary) • FROM employees • GROUP BY department_id) Single row operator • This will work: • SELECT employee_id, last_name • FROM employees • WHERE salary IN • (SELECT MIN(salary) • FROM employees • GROUP BY department_id) IN, ANY, ALL are multiple-row comparison operators
Why does the following query not work? • SELECT emp.last_name • FROM employees emp • WHERE emp.employee_id NOT IN • (SELECT mgr.manager_id • FROM employees mgr);
Why does the following query not work? • SELECT last_name • FROM employees • WHERE employee_id NOT IN • (SELECT manager_id • FROM employees); Subquery returns NULL values • This one will work: • SELECT last_name • FROM employees • WHERE employee_id NOT IN • (SELECT manager_id • FROM employees • WHERE manager_id IS NOT NULL);