You are on page 1of 6

20105287

Mariam Zakarya
BIS Northampton
Advanced Database
Dr. kesmat Yehia

Assignment 4

Determine the validity of the following three statements. Circle either True or False.
1. Group functions work across many rows to produce one result per group.
True
2. Group functions include nulls in calculations.
False
3. The WHERE clause restricts rows prior to inclusion in a group calculation.
True
4. Find the highest, lowest, sum, and average salary of all employees. Label the columns
Maximum, Minimum, Sum, and Average, respectively. Round your results to the nearest
whole number. Place your SQL statement in a text file named lab_04_04.sql.
Solution: 
SELECT ROUND(MAX(salary),0) "Maximum",
         ROUND(MIN(salary),0) "Minimum",
         ROUND(SUM(salary),0) "Sum",
       ROUND(AVG(salary),0) "Average"
  FROM   employees; 
5. Modify the query in lab_04_04.sql to display the minimum, maximum, sum, and average
salary for each job type. Resave lab_04_04.sql as lab_04_05.sql. Run the statement in
lab_04_05.sql.
Solution: 
SELECT job_id, ROUND(MAX(salary),0) "Maximum",
                 ROUND(MIN(salary),0) "Minimum",
                 ROUND(SUM(salary),0) "Sum",
                 ROUND(AVG(salary),0) "Average"
  FROM   employees
  GROUP BY job_id;
20105287

6. Write a query to display the number of people with the same job. Solution:

Solution:
SELECT job_id, COUNT(*)
  FROM   employees
  GROUP BY job_id; 

Generalize the query so that the user in the HR department is prompted for a job title.
Save the script to a file named lab_04_06.sql.

SELECT job_id, COUNT(*)
  FROM   employees
  WHERE  job_id = '&job_title
'  GROUP BY job_id; 

7. Determine the number of managers without listing them. Label the column Number of
Managers. Hint: Use the MANAGER_ID column to determine the number of managers.

Solution:

SELECT COUNT(DISTINCT manager_id) "Number of Managers"

FROM   employees;  

8. Find the difference between the highest and lowest salaries. Label the column
DIFFERENCE.

Solution:

SELECT   MAX(salary) - MIN(salary) DIFFERENCE

FROM     employees;

9. Create a report to display the manager number and the salary of the lowest-paid
The employee for that manager. Exclude anyone whose manager is not known. Exclude
any groups where the minimum salary is $6,000 or less. Sort the output in descending
order of salary.
20105287

Solution: 
SELECT   manager_id, MIN(salary)
FROM     employees
WHERE    manager_id IS NOT NULL
  GROUP BY manager_id
 HAVING   MIN(salary) > 6000
   ORDER BY MIN(salary) DESC; 

10. Create a query to display the total number of employees and, of that total, the number
of employees hired in 1995, 1996, 1997, and 1998. Create appropriate column
headings.

Solution: SELECT  COUNT(*) total,

          SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1995,1,0))"1995",

          SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1996,1,0))"1996",

          SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1997,1,0))"1997",

          SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1998,1,0))"1998"

  FROM    employees;

11. Create a matrix query to display the job, the salary for that job based on department
number, and the total salary for that job, for departments 20, 50, 80, and 90, giving each
column an appropriate heading.
Solution: 
SELECT job_id "Job", SUM(DECODE(department_id , 20, salary)) "Dept 20" ,
SUM(DECODE(department_id , 50, salary)) "Dept 50" ,
SUM(DECODE(department_id , 80, salary)) "Dept 80" ,
SUM(DECODE(department_id , 90, salary)) "Dept 90" , SUM(salary) "Total"
FROM employees GROUP BY job_id;
20105287

Assignment 5

1. Write a query for the HR department to produce the addresses of all the departments. Use
the LOCATIONS and COUNTRIES tables. Show the location ID, street address, city,
state or province, and country in the output. Use a NATURAL JOIN to produce the
results.
Solution:
SELECT location_id, street_address, city, state_province, country_name
FROM locations
NATURAL JOIN countries;
2. The HR department needs a report of all employees. Write a query to display the last
name, department number, and department name for all employees.
Solution:
SELECT last_name,
department_id, department_name
FROM employees
JOIN departments
USING (department_id);

3. The HR department needs a report of employees in Toronto. Display the last name, job,
department number, and department name for all employees who work in Toronto.

Solution:
SELECT e.last_name, e.job_id, e.department_id, d.department_name
FROM employees e
JOIN departments d ON (e.department_id = d.department_id)
JOIN locations l ON (d.location_id = l.location_id)
WHERE LOWER(l.city) = 'toronto'; 
20105287

4.  Create a report to display employees’ last name and employee number along with their
manager’s last name and manager number. Label the columns Employee, Emp#,
Manager, and Mgr#, respectively. Place your SQL statement in a text file named
lab_05_04.sql.
Solution: 
SELECT w.last_name "Employee", w.employee_id "EMP#", m.last_name "Manager",
m.employee_id "Mgr#"
FROM employees w join employees m
ON (w.manager_id = m.employee_id);

5.  Modify lab_05_04.sql to display all employees including King, who has no manager.
Order the results by the employee number. Place your SQL statement in a text file named
lab_05_05.sql. Run the query in lab_05_05.sql.

Solution:
SELECT w.last_name "Employee", w.employee_id "EMP#", m.last_name "Manager",
m.employee_id "Mgr#"
FROM employees w
LEFT OUTER JOIN employees m
ON (w.manager_id = m.employee_id); 

6. Create a report for the HR department that displays employee last names, department
numbers, and all the employees who work in the same department as a given employee.
Give each column an appropriate label. Save the script to a file named lab_05_06.sql.

Solution:
SELECT e.department_id department, e.last_name employee, c.last_name colleague
FROM employees e JOIN employees c
ON (e.department_id = c.department_id)
WHERE e.employee_id <> c.employee_id
ORDER BY e.department_id, e.last_name, c.last_name;
20105287

7. TheHRdepartmentneedsareportonjobgradesandsalaries.Tofamiliarizeyourself with the


JOB_GRADES table, first show the structure of the JOB_GRADES table. Then create a
query that displays the name, job, department name, salary, and grade for all employees
Solution: DESC JOB_GRADES
SELECT e.last_name, e.job_id, d.department_name, e.salary, j.grade_level
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
JOIN job_grades j
ON (e.salary BETWEEN j.lowest_sal AND j.highest_sal);

8. TheHRdepartmentwantstodeterminethenamesofallemployeeswhowerehiredafter Davies.
Create a query to display the name and hire date of any employee hired after employee
Davies.

Solution: SELECT e.last_name, e.hire_date

FROM employees e JOIN employees davies

ON (davies.last_name = 'Davies')

WHERE davies.hire_date < e.hire_date;

9.  The HR department needs to find the names and hire dates for all employees who were
hired before their managers, along with their managers’ names and hire dates. Save the
script to a file named lab5_09.sql.

Solution: 
SELECT w.last_name, w.hire_date, m.last_name, m.hire_date
FROM employees w JOIN employees m
ON (w.manager_id = m.employee_id)
WHERE w.hire_date < m.hire_date;

You might also like