3 Aggregating Data Sol PDF
3 Aggregating Data Sol PDF
The following abstract summary represents the notation used for aggregating data using group functions: SELECT FROM [ WHERE [ GROUP BY [ HAVING column, group_function(column) table group_by_expression ] column ] group_condition ];
Exercises 1. Display the highest, lowest, sum, and average salary of all employees. SELECT MAX(sal), MIN(sal), SUM(sal), AVG(sal) FROM emp; 2. Modify your query from 1 to display the highest, lowest, sum, and average salary for each job type. SELECT job, MAX(sal), MIN(sal), SUM(sal), AVG(sal) FROM emp GROUP BY job; 3. Write a query to display for each job the number of people with this job. SELECT job, COUNT(*) FROM emp GROUP BY job; 4. Determine the number of managers. SELECT COUNT(DISTINCT mgr) FROM emp; 5. Display the manager number and the salary of the lowest paid employee for that manager. Exclude anyone where the manager id is not known. Exclude any groups where the minimum salary is less than $1,000. Sort the output in descending order of salary. SELECT mgr, MIN(sal) FROM emp WHERE mgr IS NOT NULL GROUP BY mgr HAVING MIN(sal) > 1000 ORDER BY MIN(sal) DESC;
6. Write a query to display the department name, location name, number of employees, and the average salary for all employees in that department. SELECT d.dname, d.loc, COUNT(*), AVG(sal) FROM emp e, dept d WHERE e.deptno = d.deptno GROUP BY d.dname, d.loc; 7. Create a query that will display the total number of employees and of that total number who were hired in 1980, 1981, 1982, and 1983. SELECT COUNT(*), SUM(DECODE(TO_CHAR(hiredate, SUM(DECODE(TO_CHAR(hiredate, SUM(DECODE(TO_CHAR(hiredate, SUM(DECODE(TO_CHAR(hiredate, FROM emp;
1, 1, 1, 1,
Syntax for the use of DECODE function: DECODE(col/expression, search1, result1 [ , searchN, resultN ] [ , default ] ) to be read as: IF col/expression = searchN THEN col/expression = resultN ELSE col/expression = default Syntax for the use of TO_CHAR function: TO_CHAR(date, format)