Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

2-Guided Activity Tryout

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

REPORTING AGGREGATED DATA USING THE GROUP FUNCTIONS

1) Calculate the average length of all the country names. Any fractional components
must be rounded to the nearest whole number.

Select ROUND(AVG(Length(COUNTRY_NAME)))AS "COUNTRY NAME" From


Countries;

The length of the country name value for each row is calculated using the
LENGTH function.
The average length may be determined using the AVG function. This is rounded to
the nearest whole number using the ROUND function.

2) Analysis of staff turnover is a common reporting requirement. You are required to


create a report containing the number of employees who left their jobs, grouped by
the year in which they left. The jobs they performed are also required. The results
must be sorted in descending order based on the number of employees in each
group. The report must list the year, the JOB_ID, and the number of employees who
left a particular job in that year.

SELECT TO_CHAR(END_DATE,'YYYY') "quitting Year",Job_id,


Count(*) "Number OF Employees" From Job_history
GROUP BY TO_CHAR(END_DATE,'YYYY'),Job_id
Order by Count(*) DESC;

The year component is extracted using the TO_CHAR function. The number of
employees who quit a particular job in each year is obtained using the COUNT(*)
function.

Since the report requires employees to be listed by year and JOB_ID, these two
items must appear in the GROUP BY clause.

The sorting is performed with ORDER BY COUNT(*) DESC

3) The company is planning a recruitment drive and wants to identify the days of the
week on which 20 or more staff members were hired. Your report must list the days
and the number of employees hired on each of them.

SELECT TO_CHAR(HIRE_DATE,'DAY')Hire_DAY ,count(*)


FROM Employees
GROUP BY TO_CHAR(HIRE_DATE,'DAY')
HAVING COUNT(*) >= 20;

EMPLOYEES records must be divided into groups based on the day component of
the HIRE_DATE column.
The number of employees per group may be obtained using the COUNT function.

Page 1 of 5
The COUNT function in the SELECT clause then lists the number of staff members
employed on each day. The HAVING clause must be used to restrict these seven
rows to only those where the count is greater than or equal to 20.

DISPLAYING DATA FROM MULTIPLE TABLES USING JOINS

1) Fetch the EMPLOYEE_ID, JOB_ID, DEPARTMENT_ID, LAST_NAME, HIRE_DATE, and


END_DATE values for all rows retrieved using a pure natural join. Alias the
EMPLOYEES table

selectEmployee_id,job_id,department_id,
EMP.last_Name,EMP.hire_date,JH.end_date From JOb_history JH Natural
JOIN Employees EMP;

The Above query performs natural join between 2 table’s i.ejob_history and Employees,
Where We can find three identifiers common in 2 tables they are as follows.
EMPLOYEE_ID, JOB_ID, and DEPARTMENT_ID, So Keeping this in mind Student Are
expected perform, Natural join between Job history and Employees. They can make use
alias Where Ever Required

2) Produce a report with one column aliased as Managers. with each ach row containing a
sentence of the format FIRST_NAME LAST_NAME is manager of the
DEPARTMENT_NAME
Select E.First_Name||' '||E.last_Name||' '||'is the manager for
the'||' '||D.department_Name||' '||'Department' "Managers"
from Employees E JOIN Departments D
ON(E.Employee_id=D.Manager_id);

3) You are required to retrieve the employee’s LAST_NAME, EMPLOYEE_ID, manager’s


LAST_NAME, and employee’s DEPARTMENT_ID for the rows with DEPARMENT_ID
values of 10, 20, or 30. Alias the EMPLOYEES table as E and the second instance of the
EMPLOYEES table as M. Sort the results based on the DEPARTMENT_ID column

Select
E.last_Nameemployee,E.employee_id,E.manager_id,M.last_name,E.dep
artment_id from Employees E JOIN Employees M
ON(E.manager_id=M.Employee_id) Where E.department_id IN
(10,20,30) ORDER BY E.department_id;

In this case student need to perform Self Join Operation, keeping Manager_id as their
reference. And giving specific departments such as 10, 20, and 30 which has to be in
order giving department_id as reference. There is a hierarchical relationship between
employees and their managers. For each row in the EMPLOYEES table the
MANAGER_ID column stores the EMPLOYEE_ID of every employee’s manager.

Page 2 of 5
4) You are required to retrieve the DEPARTMENT_NAME and DEPARTMENT_ID values
for those departments to which no employees are currently assigned.

Select D.department_Name,D.department_id from Departments D LEFT


OUTER JOIN Employees E ON E.Department_id=d.department_id Where
E.department_id IS NULL;

We Expect Student to perform left outer join, which gives the all matching values from
Department table and Employees tables
USING SUBQUERIES TO SOLVE QUERIES

1) A query that uses subqueries in the column projection list. – Write a query a query to
retrieve todays date, No. of departments aliased as dept_count and no of employees in
the employees table aliased as Emp_count. The query should report on the current
numbers of departments and staff as on date

Select Sysdate today,(Select Count (*) from departments)


Dept_count,(Select count (*) from employees) Emp_Count from
dual;
2) Write a query to identify all the employees who are managers.
Select last_Name from employees Where Employee_idIN(Select
Manager_id from employees);
This uses subquery in the WHERE clause to select all the employees who’s
EMPLOYEE_ID appears as a MANAGER_ID:

3) Write a query to identify the highest salary paid in each country.

Hint : This will require using a subquery in the FROM clause:

Select MAX(salary),Country_id
from (Select Salary,Department_id,Location_id,Country_id
from employees Natural JOIN Departments Natural join
locations)
group by Country_id;
4) Write a query that will identify all employees who work in departmentslocated in the
United Kingdom.

Hint : This will require three levels of nested subqueries:

selectlast_name from employees where department_id in


(selectdepartment_id from departments
wherelocation_id in
(selectlocation_id from locations where country_id =
(selectcountry_id from countries
where country_name=’United Kingdom’)));

Page 3 of 5
5) Write a query to identify all the employees who earn more than the average and who
work in any of the IT departments.

Hint : This will require two subqueries,

selectlast_name from employees


wheredepartment_id in
(selectdepartment_id from departments where department_name
like ’IT%’)
and salary > (select avg(salary) from employees);

6) Write a query to determine who earns more than Mr. Tobias:

selectlast_name from employees where


salary> (select salary from employees where
last_name=’Tobias’)
order by last_name;

This will return 86 names, in alphabetical order.


7) Write a query to determine who earns more than Mr. Taylor:
selectlast_name from employees where
salary> (select salary from employees where
last_name=’Taylor’)
order by last_name;

This query fail with the error “ORA-01427: single-row subquery returns more than one
row.

Let’s now try to determine why the query in exercise 6 succeeded but failed in exercise
7. let’s first Find out the No. Employees who has the last name as ‘Tobias’ and ‘Yaylor’

This means that there are more than one employee with the last name ‘Taylor’. The use
of the “greater than” operator would require the sub-query to return a single row. To fix
the code in exercises 6 and 7 so that the statements will succeed no matter
whatLAST_NAME is used. There are two possible solutions:
a) The first solution: Using a multi-row comparison operator that can handle a multi-row
sub-query
selectlast_name from employees where
salary> all (select salary from employees where
last_name=’Taylor’)
order by last_name;

b) The second solution: Use a sub-query that always return a single row

Page 4 of 5
selectlast_name from employees where salary >
(select max(salary) from employees where
last_name=’Taylor’)
order by last_name;

Page 5 of 5

You might also like