0% found this document useful (0 votes)
25 views22 pages

Ass 04

The document contains SQL commands to create and manage two databases: BankingDB and EmployeeDB. It includes the creation of tables for branches, customers, loans, employees, and companies, along with sample data insertion and various queries to retrieve specific information. Additionally, it features updates and deletions of records, as well as calculations for total grade points and GPA for students.

Uploaded by

cloudyrishav0317
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)
25 views22 pages

Ass 04

The document contains SQL commands to create and manage two databases: BankingDB and EmployeeDB. It includes the creation of tables for branches, customers, loans, employees, and companies, along with sample data insertion and various queries to retrieve specific information. Additionally, it features updates and deletions of records, as well as calculations for total grade points and GPA for students.

Uploaded by

cloudyrishav0317
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/ 22

DBMS ASSIGNMENT – 4

Name – Gourab Bistu


Reg. No – 2023pgcsca089
Q1.
-- Create the Database
CREATE DATABASE BankingDB;
USE BankingDB;

-- Create the branch table


CREATE TABLE branch (
branch_name VARCHAR(50) PRIMARY KEY,
branch_city VARCHAR(50),
assets DECIMAL(15, 2)
);

-- Create the customer table


CREATE TABLE customer (
ID INT PRIMARY KEY,
customer_name VARCHAR(100),
customer_street VARCHAR(100),
customer_city VARCHAR(50)
);

-- Create the loan table


CREATE TABLE loan (
loan_number INT PRIMARY KEY,
branch_name VARCHAR(50),
amount DECIMAL(15, 2),
FOREIGN KEY (branch_name) REFERENCES
branch(branch_name)
);

-- Create the borrower table


CREATE TABLE borrower (
ID INT,
loan_number INT,
PRIMARY KEY (ID, loan_number),
FOREIGN KEY (ID) REFERENCES customer(ID),
FOREIGN KEY (loan_number) REFERENCES
loan(loan_number)
);

-- Create the account table


CREATE TABLE account (
account_number INT PRIMARY KEY,
branch_name VARCHAR(50),
balance DECIMAL(15, 2),
FOREIGN KEY (branch_name) REFERENCES
branch(branch_name)
);

-- Create the depositor table


CREATE TABLE depositor (
ID INT,
account_number INT,
PRIMARY KEY (ID, account_number),
FOREIGN KEY (ID) REFERENCES customer(ID),
FOREIGN KEY (account_number) REFERENCES
account(account_number)
);

-- Insert sample data into branch


INSERT INTO branch (branch_name, branch_city, assets)
VALUES
('Downtown', 'New York', 1000000.00),
('Uptown', 'Harrison', 500000.00),
('Suburban', 'Harrison', 750000.00);

-- Insert sample data into customer


INSERT INTO customer (ID, customer_name,
customer_street, customer_city) VALUES
(1, 'John Doe', 'Main St', 'Harrison'),
(2, 'Jane Smith', 'Maple St', 'Harrison'),
(3, 'Alice Johnson', 'Oak St', 'New York'),
(4, 'Bob Brown', 'Main St', 'New York');

-- Insert sample data into loan


INSERT INTO loan (loan_number, branch_name, amount)
VALUES
(101, 'Downtown', 250000.00),
(102, 'Uptown', 50000.00),
(103, 'Suburban', 150000.00);

-- Insert sample data into borrower


INSERT INTO borrower (ID, loan_number) VALUES
(1, 101),
(2, 102);

-- Insert sample data into account


INSERT INTO account (account_number, branch_name,
balance) VALUES
(201, 'Downtown', 5000.00),
(202, 'Uptown', 10000.00),
(203, 'Suburban', 7500.00);

-- Insert sample data into depositor


INSERT INTO depositor (ID, account_number) VALUES
(1, 201),
(2, 202),
(3, 203);

-- Query a: Find the ID of each customer of the bank who has


an account but not a loan
SELECT DISTINCT d.ID
FROM depositor d
LEFT JOIN borrower b ON d.ID = b.ID
WHERE b.ID IS NULL;
-- Query b: Find the ID of each customer who lives on the
same street and in the same city as customer '12345'
SELECT c2.ID
FROM customer c1
JOIN customer c2
ON c1.customer_street = c2.customer_street
AND c1.customer_city = c2.customer_city
WHERE c1.ID = '12345' AND c2.ID != '12345';

-- Query c: Find the name of each branch that has at least


one customer who has an account in the bank and who lives
in "Harrison"
SELECT DISTINCT a.branch_name
FROM account a
JOIN depositor d ON a.account_number = d.account_number
JOIN customer c ON d.ID = c.ID
WHERE c.customer_city = 'Harrison';

Q2.
-- Create the Database
CREATE DATABASE EmployeeDB;
USE EmployeeDB;

-- Create the employee table


CREATE TABLE employee (
ID INT PRIMARY KEY,
person_name VARCHAR(100),
street VARCHAR(100),
city VARCHAR(50)
);

-- Create the company table


CREATE TABLE company (
company_name VARCHAR(50) PRIMARY KEY,
city VARCHAR(50)
);

-- Create the works table


CREATE TABLE works (
ID INT,
company_name VARCHAR(50),
salary DECIMAL(10, 2),
PRIMARY KEY (ID, company_name),
FOREIGN KEY (ID) REFERENCES employee(ID),
FOREIGN KEY (company_name) REFERENCES
company(company_name)
);

-- Create the manages table


CREATE TABLE manages (
ID INT PRIMARY KEY,
manager_id INT
);
-- Insert sample data into employee
INSERT INTO employee (ID, person_name, street, city)
VALUES
(1, 'Alice', 'Maple St', 'Harrison'),
(2, 'Bob', 'Oak St', 'Harrison'),
(3, 'Charlie', 'Pine St', 'New York'),
(4, 'David', 'Elm St', 'New York');

-- Insert sample data into company


INSERT INTO company (company_name, city) VALUES
('First Bank Corporation', 'Harrison'),
('Small Bank Corporation', 'New York');

-- Insert sample data into works


INSERT INTO works (ID, company_name, salary) VALUES
(1, 'First Bank Corporation', 12000),
(2, 'First Bank Corporation', 8000),
(3, 'Small Bank Corporation', 15000),
(4, 'Small Bank Corporation', 11000);

-- Insert sample data into manages


INSERT INTO manages (ID, manager_id) VALUES
(1, 2),
(2, 3),
(3, 4);

-- Query a: Find the ID, name, and city of residence of each


employee who works for "First Bank Corporation".
SELECT e.ID, e.person_name, e.city
FROM employee e
JOIN works w ON e.ID = w.ID
WHERE w.company_name = 'First Bank Corporation';

-- Query b: Find the ID, name, and city of residence of each


employee who works for "First Bank Corporation" and earns
more than $10000.
SELECT e.ID, e.person_name, e.city
FROM employee e
JOIN works w ON e.ID = w.ID
WHERE w.company_name = 'First Bank Corporation' AND
w.salary > 10000;
-- Query c: Find the ID of each employee who does not work
for "First Bank Corporation".
SELECT e.ID
FROM employee e
WHERE e.ID NOT IN (SELECT w.ID FROM works w WHERE
w.company_name = 'First Bank Corporation');

-- Query d: Find the ID of each employee who earns more


than every employee of "Small Bank Corporation".

SELECT w1.ID
FROM works w1
WHERE w1.salary > ALL (SELECT w2.salary FROM works w2
WHERE w2.company_name = 'Small Bank Corporation');
-- Query e: Find the name of each company that is located in
every city in which "Small Bank Corporation" is located.
SELECT c1.company_name
FROM company c1
WHERE NOT EXISTS (
SELECT c2.city
FROM company c2
WHERE c2.company_name = 'Small Bank Corporation'
AND c2.city NOT IN (
SELECT c3.city
FROM company c3
WHERE c3.company_name = c1.company_name
)
);

-- Query f: Find the name of the company that has the most
employees.
SELECT w.company_name
FROM works w
GROUP BY w.company_name
ORDER BY COUNT(w.ID) DESC
LIMIT 1;

-- Query g: Find the name of each company whose employees


earn a higher salary on average than the average salary at
"First Bank Corporation".
SELECT w1.company_name
FROM works w1
GROUP BY w1.company_name
HAVING AVG(w1.salary) > (
SELECT AVG(w2.salary)
FROM works w2
WHERE w2.company_name = 'First Bank Corporation'
);

-- Q3--
-- a. Modify the database so that the employee whose ID is
'12345' now lives in "Newtown".
UPDATE employee
SET city = 'Newtown'
WHERE ID = 12345;
-- b. Give each manager of "First Bank Corporation" a 10
percent raise unless the salary becomes greater than
$100000; in such cases, give only a 3 percent raise.
UPDATE works
SET salary = CASE
WHEN salary * 1.10 <= 100000 THEN salary * 1.10
ELSE salary * 1.03
END
WHERE company_name = 'First Bank Corporation'
AND ID IN (SELECT manager_id FROM manages);

-- Bank Database Queries (Figure 3.18)


USE BankingDB;
-- Q4 --
-- a. Find each customer who has an account at every branch
located in "Brooklyn":
SELECT DISTINCT C.customer_name
FROM Customer C
WHERE NOT EXISTS (
SELECT B.branch_name
FROM Branch B
WHERE B.branch_city = 'Brooklyn'
AND NOT EXISTS (
SELECT A.account_number
FROM Account A
WHERE A.branch_name = B.branch_name
AND A.customer_name = C.customer_name
)
);

-- b. Find the total sum of all loan amounts in the bank:


SELECT SUM(L.amount) AS total_loan_amount
FROM Loan L;
-- c. Find the names of all branches that have assets greater
than those of at least one branch located in "Brooklyn":
SELECT B1.branch_name
FROM Branch B1
WHERE B1.assets > (
SELECT MIN(B2.assets)
FROM Branch B2
WHERE B2.branch_city = 'Brooklyn'
);

-- Q5
-- Employee Database Queries (Figure 3.19)
USE EmployeeDB;
-- a. Find ID and name of each employee who lives in the
same city as the location of the company for which the
employee works:
SELECT E.ID, E.person_name
FROM employee E
JOIN works W ON E.ID = W.ID
JOIN company C ON W.company_name = C.company_name
WHERE E.city = C.city;
-- b. Find ID and name of each employee who lives in the
same city and on the same street as does her or his manager:
SELECT E.ID, E.person_name
FROM employee E
JOIN manages M ON E.ID = M.ID
JOIN employee EM ON M.manager_id = EM.ID
WHERE E.city = EM.city AND E.street = EM.street;

-- c. Find ID and name of each employee who earns more


than the average salary of all employees of her or his
company:
SELECT E.ID, E.person_name
FROM employee E
JOIN works W ON E.ID = W.ID
WHERE W.salary > (
SELECT AVG(W2.salary)
FROM works W2
WHERE W2.company_name = W.company_name
);

-- d. Find the company that has the smallest payroll:


SELECT W.company_name
FROM works W
GROUP BY W.company_name
ORDER BY SUM(W.salary) ASC
LIMIT 1;

-- Q6 --
-- Consider the employee database of Figure 3.19.

-- a. Give all employees of "First Bank Corporation" a 10


percent raise:
UPDATE works
SET salary = salary * 1.10
WHERE company_name = 'First Bank Corporation';

-- b. Give all managers of "First Bank Corporation" a 10


percent raise:
UPDATE works
SET salary = salary * 1.10
WHERE ID IN (
SELECT M.ID
FROM manages M
JOIN works W ON M.ID = W.ID
WHERE W.company_name = 'First Bank Corporation'
);

-- c. Delete all tuples in the works relation for employees of


"Small Bank Corporation":
DELETE FROM works
WHERE company_name = 'Small Bank Corporation';
Q7.
-- Find the total grade points earned by the student with ID '12345', across all courses taken by the
student.

SELECT takes.ID, sum(course.credits*gp.points) as


total_grade_points
FROM takes
JOIN course
ON takes.course_id = course.course_id
JOIN grade_points as gp
ON gp.grade = takes.grade
WHERE takes.ID = '12345' ;

b. Find the grade point average (GPA) for the above student, that is, the total all courses taken by
the student.

SELECT takes.ID,
(sum(course.credits*gp.points)/NULLIF(sum(course.credits), 0)) as
total_grade_points
FROM takes
JOIN course
ON takes.course_id = course.course_id
JOIN grade_points as gp;
ON gp.grade = takes.grade
WHERE takes.ID = '12345' ;
c. Find the ID and the grade-point average of each student

SELECT takes.ID, (sum(course.credits*gp.points)/NULLIF(sum(course.credits),


0)) as total_grade_points
FROM takes
JOIN course
ON takes.course_id = course.course_id
JOIN grade_points as gp
ON gp.grade = takes.grade
GROUP BY takes.ID ;

d. Now consider your answers to the earlier parts of this exercise under the assumption that some
grades might be null. Explain whether your solutions still work and if not provide the versions that
handle nulls properly.

SELECT takes.ID, (SUM(COALESCE(course.credits, 0) * COALESCE(gp.points, 0)) /


NULLIF(SUM(COALESCE(course.credits, 0)), 0)) AS GPA

FROM takes JOIN course ON takes.course_id = course.course_id

JOIN grade_points AS gp ON gp.grade = takes.grade

GROUP BY takes.ID;

You might also like