0% found this document useful (0 votes)
45 views15 pages

Assignment 3 - Shouvik (1159)

Uploaded by

rahman15-6410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views15 pages

Assignment 3 - Shouvik (1159)

Uploaded by

rahman15-6410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Assignment 3

Course Code: CSE 312


Course Title: Database Management System Lab
Topic Name: Subqueries and Table Creating Using Foreign Key and
Primary Key
Submitted To

Name: Ms. Israt Jahan


Lecturer (Senior Scale)
Department of CSE
Daffodil International University.

Submitted By
Name: Shouvik Dhali
ID: 0242220005101159
Semester: Fall 2024
2
Section: 63_G1
Department of C.S.E.
Daffodil International University.
Date of Submission : 28/11/2024
Create a Table With (Foreign key and Primary key)

CREATE TABLE Departments (

department_id INT PRIMARY KEY,

department_name VARCHAR(100)

);

CREATE TABLE Employees (

employee_id INT PRIMARY KEY,

employee_name VARCHAR(100),

department_id INT,

hire_date DATE,

salary DECIMAL(10, 2),

FOREIGN KEY (department_id) REFERENCES Departments(department_id)

);

\
Insert Values in Table

INSERT INTO Departments (department_id, department_name) VALUES

(1, 'HR'),

(2, 'IT'),

(3, 'Finance'),

(4, 'Marketing');

INSERT INTO Employees (employee_id, employee_name, department_id, hire_date, salary) VALUES

(101, 'mahtab', 1, '2015-03-01', 70000),


(102, 'tamim', 2, '2016-07-10', 80000),

(103, 'showrav', 3, '2017-01-15', 85000),


(104, 'mahu', 4, '2018-10-05', 90000),
(105, 'prottoy', 2, '2020-04-23', 75000),

(106, 'redwan', 3, '2021-02-18', 65000),

(107, 'sudip', 1, '2019-09-12', 72000),


(108, 'maya', 4, '2020-06-15', 93000),

(109, 'siam', 2, '2022-01-05', 78000),


(110, 'tahin', 3, '2018-11-30', 87000);
1. Employees with Salary Greater than Department Average
SELECT employee_name

FROM Employees

WHERE salary > (SELECT AVG(salary) FROM Employees WHERE department_id =


Employees.department_id);

OutPut

2. Top 3 Highest Salaries in the Company

SELECT employee_name,salary

FROM Employees

ORDER BY salary DESC

LIMIT 3
Output

3. Employees Hired Before the Oldest Employee in Finance


SELECT employee_name
FROM Employees
WHERE hire_date < (SELECT MIN(hire_date) FROM Employees WHERE department_id = 3);
Output

4. Employees in Departments with More than 2 Employees


SELECT employee_name
FROM Employees
WHERE department_id IN (SELECT department_id FROM Employees GROUP BY department_id
HAVING COUNT(*) > 2);

Output

5. Second Highest Salary in Each Department

SELECT employee_name, salary


FROM Employees
WHERE salary = (
SELECT MAX(salary)
FROM Employees e2
WHERE e2.department_id = Employees.department_id AND e2.salary < (
SELECT MAX(salary) FROM Employees WHERE department_id = Employees.department_id)
);

Output

6. Employees with Salary Equal to or Greater than 80% of the Maximum


Salary
SELECT employee_name
FROM Employees
WHERE salary >= (SELECT MAX(salary) * 0.8 FROM Employees);
Output

7. Employees Hired After the Most Recently Hired Employee in IT


SELECT employee_name
FROM Employees
WHERE hire_date > (SELECT MAX(hire_date) FROM Employees WHERE department_id = 2);

Output

8. Departments with All Employees Having Salaries Greater than 70,000


SELECT department_id
FROM Employees
GROUP BY department_id
HAVING MIN(salary) > 70000;
Output

9. Employees Not the Highest or Lowest Paid in Their Department

SELECT employee_name
FROM Employees
WHERE salary != (SELECT MAX(salary) FROM Employees AND salary != (SELECT MIN (salary ) FROM Employees)

Output
10. Employees Who Are Paid More Than the Average Across All Departments

SELECT employee_name
FROM Employees
WHERE salary > (SELECT AVG(salary) FROM Employees);

Output

11. Departments with Employees Earning More than the Maximum Salary in IT
SELECT DISTINCT department_id
FROM Employees
WHERE salary > (SELECT MAX(salary) FROM Employees WHERE department_id = 2);
Output

12. Employees Hired in the Last 3 Years but Not in IT

SELECT employee_name
FROM Employees
WHERE hire_date > (SELECT DATE_SUB(MAX(hire_date), INTERVAL 3 YEAR) FROM Employees)
AND department_id != 2;

Output

SELECT
FROM Employees
WHERE salary < (SELECT MAX(salary) FROM Employees WHERE department_id =
Employees.department_id);
For Set Operation

-- Create employees table


CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(50),
department_id INT,
salary DECIMAL(10, 2),
job_title VARCHAR(50),
join_date DATE,
city VARCHAR(50)
);

-- Create departments table


CREATE TABLE departments (
department_id
1.Get a combined INT PRIMARY
list of employee KEY,
names and department names
name VARCHAR(50),
location VARCHAR(50),
manager_name VARCHAR(50)
);

-- Insert sample data into employees table with updated names


INSERT INTO employees (employee_id, name, department_id, salary, job_title, join_date, city)
VALUES
(1, 'Alice Williams', 1, 5000, 'Engineer', '2015-05-10', 'New York'),
(2, 'James Taylor', 2, 7000, 'Manager', '2012-03-15', 'Los Angeles'),
Output
(3, 'Oliver Miller', 3, 6000, 'Technician', '2019-07-20', 'Chicago'),
(4, 'Sophia Anderson', 2, 9000, 'Engineer', '2018-06-22', 'San Francisco'),
(5, 'Liam Thompson', 4, 5500, 'Analyst', '2016-02-18', 'Houston'),
(6, 'Emma Johnson', 1, 8000, 'Manager', '2014-11-01', 'Boston'),
(7, 'Noah Clark', 3, 4000, 'Clerk', '2020-09-10', 'Los Angeles'),
(8, 'Isabella Martinez', 4, 10000, 'Director', '2011-01-25', 'Chicago');

-- Insert sample data into departments table with updated manager names
INSERT INTO departments (department_id, name, location, manager_name)
VALUES
(1, 'Engineering', 'New York', 'Emma Johnson'),
(2, 'HR', 'San Francisco', 'James Taylor'),
(3, 'Maintenance', 'Chicago', 'Oliver Miller'),
(4, 'Finance', 'Houston', 'Isabella Martinez');
2.Get a list of all employees working in departments located in 'ctg' or employees
earning more than $5000.

Output

3.Find employees who earn more than $7000 and also belong to department 2.

Output
4.Get employees who do not belong to any department located in 'Chicago'.

Output

5.Get employees who are not managers.

Output
LIKE OPERATOR

1.Find all employees whose first name starts with 'M'

Output

2.Find all employees whose last name ends with 'y'

Output

You might also like