0% found this document useful (0 votes)
21 views

DBMS Lab4 146

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

DBMS Lab4 146

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

LABSHEET - 4

Question - 1

1. create table branch (branch_name varchar(10) primary key, branch_city


varchar(10), assets numeric(8,2));

create table account (accno int primary key, customer_no varchar(5),


branch_name varchar(10), balance numeric(8,2), foreign key (branch_name)
references branch(branch_name));

create table customer (customer_no varchar(5) primary key, customer_name


varchar(10), customer_street varchar(15), customer_city varchar(10));

create table loan (loan_number int primary key, customer_no varchar(5),


branch_name varchar(10), amount numeric(8,2), foreign key (customer_no)
references customer(customer_no), foreign key (branch_name) references
branch(branch_name));

create table depositor (customer_no varchar(5), accno int, primary key


(customer_no, accno), foreign key (customer_no) references
customer(customer_no), foreign key (accno) references account(accno));

create table borrower (customer_no varchar(5), loan_number int, primary key


(customer_no, loan_number), foreign key (customer_no) references
customer(customer_no), foreign key (loan_number) references
loan(loan_number));

INSERT VALUES:

insert into branch (branch_name, branch_city, assets) values ('Main',


'Mumbai', 50000.00), ('Kollam', 'Kollam', 30000.00), ('Delhi', 'Delhi', 40000.00);

insert into account (accno, customer_no, branch_name, balance) values (101,


'C001', 'Main', 15000.00), (102, 'C002', 'Main', 12000.00), (103, 'C003',
'Kollam', 18000.00);
insert into customer (customer_no, customer_name, customer_street,
customer_city) values ('C001', 'Alice', 'Park St', 'Mumbai'), ('C002', 'Bob', 'Hill
St', 'Kollam'), ('C003', 'Charlie', 'High St', 'Delhi');

insert into loan (loan_number, customer_no, branch_name, amount) values


(201, 'C001', 'Main', 10000.00), (202, 'C002', 'Kollam', 15000.00), (203, 'C003',
'Main', 20000.00);

insert into depositor (customer_no, accno) values ('C001', 101), ('C002', 102),
('C003', 103);

insert into borrower (customer_no, loan_number) values ('C001', 201),


('C002', 202), ('C003', 203);

QUERIES:

1. Find customer numbers of all customers who have at least two accounts
at the ‘Main’ branch.

select customer_no from account where branch_name = 'Main' group by


customer_no having count(accno) >= 2;

2. Find the average account balance at the ‘Kollam’ branch.

select avg(balance) from account where branch_name = 'Kollam';

3. Find the number of customers for each branch.

select branch_name, count(distinct customer_no) as num_customers from


account group by branch_name;
4. Find the names of all branches where the average account balance is
more than RS. 1,2000.

select branch_name from account group by branch_name having


avg(balance) > 12000;

5. Find all customers who have a loan, an account, or both.

select distinct customer_no from account union select distinct customer_no


from loan;

6. Find all customers who have both a loan and an account.

select distinct customer_no from account where customer_no in (select


customer_no from loan);

7. Find the number of branches that currently have loans.

select count(distinct branch_name) from loan;

8. Find the average loan amount for each branch.


select branch_name, avg(amount) from loan group by branch_name;

9. Find all customers with more than one loan

select customer_no from loan group by customer_no having


count(loan_number) > 1;

10. Find the total of all loan amounts

select sum(amount) from loan;

Question - 2

CREATE TABLE Employee (


FName VARCHAR(50),
Minit CHAR(1),
LName VARCHAR(50),
SSN CHAR(9) PRIMARY KEY,
BDate DATE,
Address VARCHAR(100),
Sex CHAR(1),
Salary NUMERIC(10, 2),
SuperSSN CHAR(9),
FOREIGN KEY (SuperSSN) REFERENCES Employee(SSN),
DepNo INT);

CREATE TABLE Department (


DName VARCHAR(50),
DepNo INT PRIMARY KEY,
MgrSSN CHAR(9) REFERENCES Employee(SSN),
MgrSDate DATE);

ALTER TABLE Employee


ADD CONSTRAINT supersnn_emp FOREIGN KEY (DepNo) REFERENCES
Department (DepNo);

CREATE TABLE Dept_Locations (


DepNo INT REFERENCES Department(DepNo),
DLocation VARCHAR(50));

CREATE TABLE Project (


PName VARCHAR(20),
PNumber INT PRIMARY KEY,
PLocation VARCHAR(50),
DepNo INT REFERENCES Department(DepNo));

CREATE TABLE Works_On (


ESSN CHAR(9) REFERENCES Employee(SSN),
PNo INT REFERENCES Project(PNumber),
Hours NUMERIC(5, 2),
PRIMARY KEY (ESSN, PNo));

CREATE TABLE Dependent(


ESSN CHAR(9) REFERENCES Employee(SSN),
Dependent_Name VARCHAR(50),
Sex CHAR(1),
BDate DATE,
Relationship VARCHAR(50),
PRIMARY KEY (ESSN, Dependent_Name));

INSERT INTO Department (DName, DepNo, MgrSDate)


VALUES
('Research', 1, '2015-01-01'),
('Development', 2, '2016-03-15'),
('HR', 3, '2017-06-20'),
('Sales', 4, '2018-09-12'),
('Finance', 5, '2019-11-25'),
('Marketing', 6, '2020-02-10'),
('IT', 7, '2021-04-18'),
('Operations', 8, '2022-07-23'),
('Logistics', 9, '2023-05-07'),
('Customer Support', 10, '2024-08-29');
INSERT INTO Employee (FName, Minit, LName, SSN, BDate, Address, Sex, Salary,
SuperSSN, DepNo)
VALUES
('John', 'A', 'Smith', '123456789', '1980-01-15', '123 Elm St, Springfield', 'M',
55000.00, NULL, 1),
('Alice', 'B', 'Johnson', '987654321', '1985-03-22', '456 Oak St, Springfield', 'F',
65000.00, '123456789', 2),
('Bob', 'C', 'Williams', '456789123', '1978-06-30', '789 Pine St, Springfield', 'M',
72000.00, '987654321', 1),
('Clara', 'D', 'Brown', '789123456', '1990-07-12', '101 Maple St, Springfield', 'F',
48000.00, '123456789', 3),
('David', 'E', 'Davis', '321654987', '1975-04-05', '202 Birch St, Springfield', 'M',
82000.00, '456789123', 2),
('Eve', 'F', 'Miller', '654987321', '1995-11-10', '303 Cedar St, Springfield', 'F',
51000.00, '987654321', 3),
('Frank', 'G', 'Wilson', '159753486', '1983-08-21', '404 Walnut St, Springfield', 'M',
68000.00, '654987321', 1),
('Grace', 'H', 'Taylor', '951753486', '1972-12-19', '505 Ash St, Springfield', 'F',
73000.00, '321654987', 3),
('Henry', 'I', 'Lee', '753159486', '1988-09-01', '606 Chestnut St, Springfield', 'M',
90000.00, '951753486', 2),
('Isabel', 'J', 'Harris', '852369741', '1993-02-14', '707 Poplar St, Springfield', 'F',
54000.00, '123456789', 1);

UPDATE Department
SET MgrSSN = '123456789' WHERE DepNo = 1;
UPDATE Department
SET MgrSSN = '987654321' WHERE DepNo = 2;
UPDATE Department
SET MgrSSN = '654987321' WHERE DepNo = 3;
UPDATE Department
SET MgrSSN = '159753486' WHERE DepNo = 4;
UPDATE Department
SET MgrSSN = '951753486' WHERE DepNo = 5;

INSERT INTO Dept_Locations (DepNo, DLocation)


VALUES
(1, 'New York'),
(2, 'Los Angeles'),
(3, 'Chicago'),
(4, 'Houston'),
(5, 'Phoenix'),
(6, 'Philadelphia'),
(7, 'San Antonio'),
(8, 'San Diego'),
(9, 'Dallas'),
(10, 'San Francisco');

INSERT INTO Project (PName, PNumber, PLocation, DepNo)


VALUES
('Project Alpha', 101, 'New York', 1),
('Project Beta', 102, 'Los Angeles', 2),
('Project Gamma', 103, 'Chicago', 3),
('Project Delta', 104, 'Houston', 4),
('Project Epsilon', 105, 'Phoenix', 5),
('Project Zeta', 106, 'Philadelphia', 6),
('Project Eta', 107, 'San Antonio', 7),
('Project Theta', 108, 'San Diego', 8),
('Project Iota', 109, 'Dallas', 9),
('Project Kappa', 110, 'San Francisco', 10);

INSERT INTO Works_On (ESSN, PNo, Hours)


VALUES
('123456789', 101, 35.5),
('987654321', 102, 40.0),
('456789123', 103, 30.0),
('789123456', 104, 45.0),
('321654987', 105, 20.0),
('654987321', 106, 25.0),
('159753486', 107, 50.0),
('951753486', 108, 38.0),
('753159486', 109, 42.0),
('852369741', 110, 36.5);

INSERT INTO Dependent (ESSN, Dependent_Name, Sex, BDate, Relationship)


VALUES
('123456789', 'Anna Smith', 'F', '2005-05-10', 'Daughter'),
('987654321', 'Mark Johnson', 'M', '2010-08-22', 'Son'),
('456789123', 'Nancy Williams', 'F', '2012-12-01', 'Daughter'),
('789123456', 'Peter Brown', 'M', '2007-09-14', 'Son'),
('321654987', 'Sara Davis', 'F', '2000-01-02', 'Daughter'),
('654987321', 'Emily Miller', 'F', '2015-07-30', 'Daughter'),
('159753486', 'John Wilson', 'M', '2011-11-11', 'Son'),
('951753486', 'Grace Taylor', 'F', '2003-03-21', 'Daughter'),
('753159486', 'Ella Lee', 'F', '2018-06-06', 'Daughter'),
('852369741', 'Sophia Harris', 'F', '2020-04-25', 'Daughter');
SELECT * from Employee;

SELECT * from Department;

SELECT * from Dept_Locations;

SELECT * from Project;


SELECT * from Works_On;

SELECT * from Dependent;

a. SELECT FName, LName FROM Employee WHERE DepNo = 5;

b. SELECT FName, LName, Salary FROM Employee ORDER BY Salary;


c. SELECT FName, LName FROM Employee WHERE Salary BETWEEN 30000
AND 50000;

d. SELECT FName, LName, Salary FROM Employee WHERE Address LIKE


'%Kollam%';

e. SELECT FName, LName FROM Employee WHERE SuperSSN IS NULL;

f. SELECT DepNo, COUNT(SSN) FROM Employee GROUP BY DepNo


HAVING COUNT(SSN) > 2 ORDER BY DepNo;

g. SELECT ESSN FROM Works_On WHERE PNo IN (3388, 1945);

h. SELECT DepNo,DLocation FROM Dept_Locations WHERE DepNo IN (1, 3,


5);
i. SELECT FName, LName FROM Employee WHERE Sex = 'F';

You might also like