2-Guided Activity Tryout
2-Guided Activity Tryout
2-Guided Activity Tryout
1) Calculate the average length of all the country names. Any fractional components
must be rounded to the nearest whole number.
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.
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.
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.
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.
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);
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.
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 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.
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.
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