Lab Assignment 2
Lab Assignment 2
Database Systems
Lab Assignment # 1
1
Lab Assignment # 2
Lab 04
Using Single Row Functions to Customize Output
Lab Task 1:
The HR department needs a report to display the employee number, last name, salary,
and salary increased by 15.5% (expressed as a whole number) for each employee.
Write a SQL statement for this need. Label the last column as New Salary.
Lab Task 2:
Write a SQL statement that displays the last name (with the first letter in uppercase
and all the other letters in lowercase) and the length of the last name for all
employees whose name starts with the letters “J”, “A”, or “M”. Give each column an
appropriate label. Sort the results by the employees ‘last names. Rewrite the query so
that the user is prompted to enter a letter that the last name starts with. For example,
if the user enters H (capitalized) when prompted for a letter, then the output should
show all employees whose last name starts with the letter H
2
Lab Assignment # 2
(a)
SELECT INITCAP(last_name)NAME, LENGTH(last_name)LENGTH
FROM employees
WHERE INSTR(last_name,'A')=2 OR INSTR(last_name,'J')=1 OR INSTR(last_name,'M')=1
ORDER BY last_name;
(b)
SELECT INITCAP(last_name)NAME, LENGTH(last_name)LENGTH
FROM employees
WHERE INSTR(last_name,'&input_letter')=1
ORDER BY last_name;
3
Lab Assignment # 2
Lab Task 3:
The HR department wants to find the duration of employment for each employee.
Write a SQL statement that for each employee, display the last name and calculate the
number of months between today and the date on which the employee was hired.
Label the column as MONTHS_WORKED. Order your results by the number of months
employed. Round the number of months up to the closest whole number.
Lab Task 4:
Write a SQL statement that displays the employee’s last names and commission
amounts. If an employee does not earn commission, show ― No Commission. Label
the column as COMM.
4
Lab Assignment # 2
FROM employees;
Lab 05
5
Lab Assignment # 2
Lab Task 2
Write a SQL statement to display the number of people with the same job.
SELECT COUNT(job_id)
FROM employees
GROUP BY job_id;
Lab Task 3
Write a SQL statement to determine the number of managers without listing them.
Label the column as Number of Managers.
6
Lab Assignment # 2
Lab Task 4
Write a SQL statement that finds the difference between the highest and lowest
salaries. Label the column DIFFERENCE.
Lab Task 5
Write a SQL statement that displays the manager number and the salary of the lowest-
paid 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.
7
Lab Assignment # 2
Lab Task 6
Write a SQL statement that displays 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.
SELECT job_id,salary,department_id,SUM(salary)
FROM employees
HAVING department_id=20 or department_id=50 or department_id=80
GROUP BY department_id, job_id,salary;
8
Lab Assignment # 2
Lab 06
Displaying Data from Multiple Tables Using Joins
Lab Task 1
Write a SQL statement for 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.
SELECT location_id,street_address,state_province,country_name,city
FROM locations
NATURAL JOIN countries;
Lab Task 2
The HR department needs a report of all employees. Write a SQL statement to display
the last name, department number, and department name for all the employees.
9
Lab Assignment # 2
Lab Task 3
Write a SQL statement to retrieves all the employees who works in Sales deparment
and are from Africa.
SELECT last_name AS "Name"
FROM employees NATURAL JOIN departments
NATURAL JOIN locations NATURAL JOIN countries
WHERE country_name = ‘Africa’ AND department_name = ‘Sales’;
Lab Task 4
Write a SQL query to find the employees name, department name, full name (first and
last name) of the manager and their city.
SELECT a.Name, a.Department,b.first_name ||' '||b.last_name AS Manager,a.city AS
"ManagerID"
FROM (
SELECT first_name||' '||last_name AS Name,department_name AS Department,
manager_id,city
FROM employees NATURAL JOIN departments NATURAL JOIN locations
a INNER JOIN employees b
10
Lab Assignment # 2
ON a.manager_id = b.employee_id;
Lab Task 5
Write a SQL query to find those employees who joined in 2001. Return job title,
department name, employee name, and joining date of the job.
SELECT job_title,department_name,first_name||' '||last_name AS "Employee Name"
FROM ((employees NATURAL JOIN departments) NATURAL JOIN jobs)
WHERE TO_NUMBER(SUBSTR(hire_date,-2)) >= 1;
Lab Task 6
The HR department needs a report of employees in Toronto. Write a SQL statement to
display the last name, job, department number, and the department name for all
employees who work in Toronto.
11
Lab Assignment # 2
12