Assignment No.
– 4
Name: James
Alieu SLamin
KeitaKamara
University Roll No:231312018
231312014
Course: B.SC IT 3 Sem
Q 1. Create the following table “Employees”
CREATE TABLE Employees (
emp_id NUMBER(10) PRIMARY KEY,
emp_name VARCHAR2(20),
dept_id VARCHAR2(10),
salary DECIMAL(10,2),
joining_date DATE
);
INSERT INTO Employees (emp_id, emp_name, dept_id, salary, joining_date)
VALUES (1, 'Alice', '10', 5000, '2020-01-10');
INSERT INTO Employees (emp_id, emp_name, dept_id, salary, joining_date)
VALUES (2, 'Bob', '20', 6000, '2019-03-15');
INSERT INTO Employees (emp_id, emp_name, dept_id, salary, joining_date)
VALUES (3, 'Charlie', '30', 7000, '2021-07-01');
INSERT INTO Employees (emp_id, emp_name, dept_id, salary, joining_date)
VALUES (4, 'David', NULL, 4000, '2022-05-20');
INSERT INTO Employees (emp_id, emp_name, dept_id, salary, joining_date)
VALUES (5, 'Emma', '10', 8000, '2018-08-22');
INSERT INTO Employees (emp_id, emp_name, dept_id, salary, joining_date)
VALUES (6, 'Frank', '20', 5500, '2020-09-30');
INSERT INTO Employees (emp_id, emp_name, dept_id, salary, joining_date)
VALUES (7, 'Grace', NULL, 3000, '2023-01-01');
CREATE TABLE Departments (
dept_id NUMBER PRIMARY KEY,
dept_name VARCHAR2(20)
);
INSERT INTO Departments (dept_id, dept_name)
VALUES
(10, 'HR'),
(20, 'IT'),
(30, 'Finance'),
(40, 'Marketing');
1. Display all employee names in uppercase along with their salaries.
SELECT UPPER(emp_name) AS emp_name_uppercase, salary
FROM Employees;
2. Retrieve the total number of employees.
SELECT COUNT(*) AS total_employees
FROM Employees;
3. Count the number of employees in each department.
SELECT AVG(salary) AS average_salary
FROM Employees;
4. Count the number of employees in each department.
SELECT dept_id, COUNT(*) AS num_employees
FROM Employees
GROUP BY dept_id;
5. Calculate the total salary paid in each department
SELECT dept_id, SUM(salary) AS total_salary
FROM Employees
GROUP BY dept_id;
6. Display the length of each employee's name.
SELECT emp_name, LENGTH(emp_name) AS name_length
FROM Employees;
7. Display the first three characters of each employee's name.
SELECT emp_name, SUBSTR(emp_name, 1, 3) AS first_three_chars
FROM Employees;
8. List all employees who joined after January 1, 2020.
SELECT emp_name, joining_date
FROM Employees
WHERE joining_date > TO_DATE('2020-01-01', 'YYYY-MM-DD');
9. Find the department name and total salary paid in that department using LEFT JOIN.
SELECT d.dept_name, SUM(e.salary) AS total_salary
FROM Departments d
LEFT JOIN Employees e ON d.dept_id = e.dept_id
GROUP BY d.dept_name;
10. Calculate the total salary paid to employees, rounded to two decimal places.
SELECT ROUND(SUM(salary), 2) AS total_salary
FROM Employees;