0% found this document useful (0 votes)
3 views13 pages

Id

The document contains SQL assignments focused on creating and manipulating database tables for an academic institution. It includes commands for creating tables such as Instructor, Course, and Department, inserting data, and performing various SQL queries to retrieve and update information. Additionally, it covers the use of aggregate functions and scalar functions for data analysis.

Uploaded by

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

Id

The document contains SQL assignments focused on creating and manipulating database tables for an academic institution. It includes commands for creating tables such as Instructor, Course, and Department, inserting data, and performing various SQL queries to retrieve and update information. Additionally, it covers the use of aggregate functions and scalar functions for data analysis.

Uploaded by

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

ASSIGNMENT 1

-- Creating Tables
CREATE TABLE Instructor (
ID INT(5),
Name VARCHAR(30),
Dept_name VARCHAR(20),
Salary INT(6)
);

CREATE TABLE Course (


Course_id VARCHAR(10),
Title VARCHAR(30),
Dept_name VARCHAR(20),
Credits INT(2)
);

CREATE TABLE Prereq (


Course_id VARCHAR(10),
Prereq_id VARCHAR(10)
);

CREATE TABLE Department (


Dept_name VARCHAR(20),
Building VARCHAR(20),
Budget INT(10)
);

CREATE TABLE Teaches (


ID INT(5),
Course_id VARCHAR(10),
Sec_id INT(2),
Semester VARCHAR(10),
Year INT(4)
);

-- Inserting Data into Instructor Table


INSERT INTO Instructor VALUES (10101, 'Srinivasan', 'Comp. Sci.', 65000);
INSERT INTO Instructor VALUES (12121, 'Wu', 'Finance', 90000);
INSERT INTO Instructor VALUES (15151, 'Mozart', 'Music', 40000);
INSERT INTO Instructor VALUES (22222, 'Einstein', 'Physics', 95000);
INSERT INTO Instructor VALUES (32343, 'El Said', 'History', 60000);
INSERT INTO Instructor VALUES (33456, 'Gold', 'Physics', 87000);
INSERT INTO Instructor VALUES (45565, 'Katz', 'Comp. Sci.', 75000);
INSERT INTO Instructor VALUES (58583, 'Califieri', 'History', 62000);
INSERT INTO Instructor VALUES (76543, 'Singh', 'Finance', 80000);
INSERT INTO Instructor VALUES (76766, 'Crick', 'Biology', 72000);
INSERT INTO Instructor VALUES (83821, 'Brandt', 'Comp. Sci.', 92000);
INSERT INTO Instructor VALUES (98345, 'Kim', 'Elec. Eng.', 80000);

-- Inserting Data into Course Table


INSERT INTO Course VALUES ('BIO-101', 'Intro. to Biology', 'Biology', 4);
INSERT INTO Course VALUES ('BIO-301', 'Genetics', 'Biology', 4);
INSERT INTO Course VALUES ('BIO-399', 'Computational Biology', 'Biology', 3);
INSERT INTO Course VALUES ('CS-101', 'Intro. to Computer Science', 'Comp. Sci.',
4);
INSERT INTO Course VALUES ('CS-190', 'Game Design', 'Comp. Sci.', 4);
INSERT INTO Course VALUES ('CS-315', 'Robotics', 'Comp. Sci.', 3);
INSERT INTO Course VALUES ('CS-319', 'Image Processing', 'Comp. Sci.', 3);
INSERT INTO Course VALUES ('CS-347', 'Database System Concepts', 'Comp. Sci.', 3);
INSERT INTO Course VALUES ('EE-181', 'Intro. to Digital Systems', 'Elec. Eng.', 3);
INSERT INTO Course VALUES ('FIN-201', 'Investment Banking', 'Finance', 3);
INSERT INTO Course VALUES ('HIS-351', 'World History', 'History', 3);
INSERT INTO Course VALUES ('MU-199', 'Music Video Production', 'Music', 3);
INSERT INTO Course VALUES ('PHY-101', 'Physical Principles', 'Physics', 4);

-- Inserting Data into Prereq Table


INSERT INTO Prereq VALUES ('CS102', 'CS101');
INSERT INTO Prereq VALUES ('EC101', 'CS101');
INSERT INTO Prereq VALUES ('BIO-301', 'BIO-101');
INSERT INTO Prereq VALUES ('BIO-399', 'BIO-101');
INSERT INTO Prereq VALUES ('CS-190', 'CS-101');
INSERT INTO Prereq VALUES ('CS-315', 'CS-101');
INSERT INTO Prereq VALUES ('CS-319', 'CS-101');
INSERT INTO Prereq VALUES ('CS-347', 'CS-101');
INSERT INTO Prereq VALUES ('EE-181', 'PHY-101');

-- Inserting Data into Department Table


INSERT INTO Department VALUES ('Biology', 'Watson', 90000);
INSERT INTO Department VALUES ('Comp. Sci.', 'Taylor', 100000);
INSERT INTO Department VALUES ('Elec. Eng.', 'Taylor', 85000);
INSERT INTO Department VALUES ('Finance', 'Painter', 120000);
INSERT INTO Department VALUES ('History', 'Painter', 50000);
INSERT INTO Department VALUES ('Music', 'Packard', 80000);
INSERT INTO Department VALUES ('Physics', 'Watson', 70000);

-- Inserting Data into Teaches Table


INSERT INTO Teaches VALUES (10101, 'CS-101', 1, 'Fall', 2009);
INSERT INTO Teaches VALUES (10101, 'CS-315', 1, 'Spring', 2010);
INSERT INTO Teaches VALUES (10101, 'CS-347', 1, 'Fall', 2009);
INSERT INTO Teaches VALUES (12121, 'FIN-201', 1, 'Spring', 2010);
INSERT INTO Teaches VALUES (15151, 'MU-199', 1, 'Spring', 2010);
INSERT INTO Teaches VALUES (22222, 'PHY-101', 1, 'Fall', 2009);
INSERT INTO Teaches VALUES (32343, 'HIS-351', 1, 'Spring', 2010);
INSERT INTO Teaches VALUES (45565, 'CS-101', 1, 'Spring', 2010);
INSERT INTO Teaches VALUES (45565, 'CS-319', 1, 'Spring', 2010);
INSERT INTO Teaches VALUES (76766, 'BIO-101', 1, 'Summer', 2009);
INSERT INTO Teaches VALUES (76766, 'BIO-301', 1, 'Summer', 2010);
INSERT INTO Teaches VALUES (83821, 'CS-190', 1, 'Spring', 2009);
INSERT INTO Teaches VALUES (83821, 'CS-190', 2, 'Spring', 2009);

-----------------------------------------------------------------------------------
------------------------------------------------------------------------------

ASSIGNMENT 2

-- (a) Display structure of all tables from Assignment 1


SHOW TABLES;
DESCRIBE faculty_info;
DESCRIBE course;
DESCRIBE department;
DESCRIBE prerequired;

-- (b) Display contents of all tables from Assignment 1


SELECT * FROM faculty_info;
SELECT * FROM course;
SELECT * FROM department;
SELECT * FROM prerequired;

-- (c) Display name and department of each instructor


SELECT name, department FROM faculty_info;

-- (d) Display name and salary of Comp Sci. instructors


SELECT name, salary FROM faculty_info WHERE department = 'Comp. Sci';

-- (e) Display instructors from Physics department earning less than 90000
SELECT * FROM faculty_info WHERE department = 'Physics' AND salary < 90000;

-- (f) Display names of instructors not in Comp Sci Department


SELECT name FROM faculty_info WHERE department <> 'Comp. Sci';

-- (g) Display distinct department names from Faculty_Info table


SELECT DISTINCT department FROM faculty_info;

-- (h) Display Course IDs taught in Spring semester of 2009


SELECT course_id FROM course WHERE semester = 'Spring' AND year = 2009;

-- (i) Display course titles from Comp Sci Department not having 3 credits
SELECT title FROM course WHERE department = 'Comp. Sci' AND credits <> 3;

-- (j) Display all columns of Course table sorted by department names in descending
order
SELECT * FROM course ORDER BY department DESC;

-- (k) Add a date_of_join column to Faculty_Info table


ALTER TABLE faculty_info ADD date_of_join DATE;

-- (l) Insert date values to existing rows in Faculty_Info table


UPDATE faculty_info SET date_of_join = '2010-01-01' WHERE id = 10101;
UPDATE faculty_info SET date_of_join = '2011-03-15' WHERE id = 12121;
UPDATE faculty_info SET date_of_join = '2012-06-20' WHERE id = 15151;
UPDATE faculty_info SET date_of_join = '2009-09-10' WHERE id = 22222;
UPDATE faculty_info SET date_of_join = '2013-11-05' WHERE id = 32343;
UPDATE faculty_info SET date_of_join = '2014-07-23' WHERE id = 33456;
UPDATE faculty_info SET date_of_join = '2015-04-17' WHERE id = 45565;
UPDATE faculty_info SET date_of_join = '2016-08-12' WHERE id = 58583;
UPDATE faculty_info SET date_of_join = '2017-02-28' WHERE id = 76543;
UPDATE faculty_info SET date_of_join = '2018-12-01' WHERE id = 76766;
UPDATE faculty_info SET date_of_join = '2019-10-20' WHERE id = 83821;
UPDATE faculty_info SET date_of_join = '2020-05-14' WHERE id = 98345;

-- (m) Change dept_name to department in all tables


ALTER TABLE faculty_info CHANGE dept_name department VARCHAR(20);
ALTER TABLE course CHANGE dept_name department VARCHAR(20);
ALTER TABLE department CHANGE dept_name department VARCHAR(20);

-- (n) Rename "Prereq" table to "Prerequired"


RENAME TABLE prereq TO prerequired;

-- (o) Change course_id column name to sub_code


ALTER TABLE course CHANGE course_id sub_code VARCHAR(10);

-- (p) Change data type of name to VARCHAR(50)


ALTER TABLE faculty_info MODIFY name VARCHAR(50);

-- (q) Rename "Instructor" table to "Faculty_Info"


RENAME TABLE instructor TO faculty_info;

-- (r) Change column size of course_id in Course table from 10 to 8


ALTER TABLE course MODIFY sub_code VARCHAR(8);

-- (s) Delete content of Prerequired table along with its description


DROP TABLE prerequired;

-- (t) Rename "Building" column in Department table to "Builder"


ALTER TABLE department CHANGE building builder VARCHAR(20);

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------

ASSIGNMENT 3

-- 1. SQL Expressions using Suitable SQL Operators

-- (a) display course_ids, titles, and credits for specified departments


select sub_code as course_id, title, credits
from course
where department in ('physics', 'music', 'finance', 'biology');

-- (b) display instructors with name starting with 'k' and salary > 65000
select *
from faculty_info
where name like 'k%' and salary > 65000;

-- (c) display name, department, gross salary, and net salary


select name, department,
salary + (salary * 0.105) + (salary * 0.20) as gross_salary,
(salary + (salary * 0.105) + (salary * 0.20)) - (salary * 0.30) as
net_salary
from faculty_info;

-- (d) display instructors with salary between 60000 and 80000


select *
from faculty_info
where salary between 60000 and 80000;

-- (e) display instructors with second letter 'r' in their name


select *
from faculty_info
where name like '_r%';

-- (f) display comp.sci. instructors in descending order of salary


select name
from faculty_info
where department = 'comp. sci.'
order by salary desc;
-- (g) update all instructor records with a 15% salary hike
update faculty_info
set salary = salary * 1.15;

-- (h) update comp.sci. instructors with salary < 70000 by 3%


update faculty
set salary = salary * 1.03
where department = 'comp. sci.' and salary < 70000;

-- (i) display annual salary of each instructor


select name, salary * 12 as annual_salary
from faculty_info;

-- (j) update course title 'game design' to 'game theory'


update course
set title = 'game theory'
where title = 'game design';

-- (k) delete instructor records of the history department


delete from faculty_info
where department = 'history';

-- (l) delete courses with course_id starting with 'bio'


delete from course
where sub_code like 'bio%';

-- 2. SQL Expressions using Suitable SQL Aggregate Functions

-- (a) display the avg. salary of instructors of physics department


select avg(salary) as avg_salary
from faculty_info
where department = 'physics';

-- (b) display the dept_name and average salary paid to instructors of each
department
select department, avg(salary) as avg_salary
from faculty_info
group by department;

-- (c) display the id, name & department of the instructor drawing the highest
salary
select id, name, department
from faculty_info
where salary = (select max(salary) from faculty);

-- (d) display the number of instructors available in comp.sci. department


select count(*) as instructor_count
from faculty_info
where department = 'comp.sci.';

-- (e) display the total credits of all courses offered in comp.sci. department
select sum(credits) as total_credits
from course
where department = 'comp. sci.';

-- (f) display the number of instructors and total salary drawn by physics and
comp.sci. departments
select department, count(*) as instructor_count, sum(salary) as total_salary
from faculty_info
where department in ('physics', 'comp. sci.')
group by department;

-- (g) display the total credits of comp.sci. and biology departments from course
table
select department, sum(credits) as total_credits
from course
where department in ('comp. sci.', 'biology')
group by department;

-- (h) display building wise total budget values


select builder, sum(budget) as total_budget
from department
group by builder;

-- (i) display the number of instructors of each department


select department, count(*) as instructor_count
from faculty_info
group by department;

-- (j) display the number of instructors of each department sorted in high to low
select department, count(*) as instructor_count
from faculty_info
group by department
order by instructor_count desc;

-- (k) display the number of courses offered semester wise


select semester, count(*) as course_count
from teaches
group by semester;

-- (l) display the name of departments having number of instructors less than 2
select department
from faculty_info
group by department
having count(*) < 2;

-- (m) list the number of instructors of each department having 2 or more


instructors except finance department, sorted in high to low order
select department, count(*) as instructor_count
from faculty_info
where department != 'finance'
group by department
having count(*) >= 2
order by instructor_count desc;

-- (n) display the dept_name that has paid total salary more than 50000
select department
from faculty_info
group by department
having sum(salary) > 50000;

-- (o) display the total budget for the building built by watson
select sum(budget) as total_budget
from department
where builder = 'watson';

-- (p) display the highest salary of the instructor of comp.sci. department


select max(salary) as highest_salary
from faculty_info
where department = 'comp.sci.';

-- 3. SQL Expressions using Suitable SQL Scalar Functions

-- (a) display your name with first letter being capital, where the entered name is
in lower case
select concat(upper(left('dinanath', 1)), lower(substring('dinanath', 2))) as
name_with_capital;

-- (b) display 2nd-6th characters of your name


select substring('dinanath', 2, 5) as sub_name;

-- (c) find length of your full university name


select length('Siksha O Anusandhan') as university_name_length;

-- (d) display all the instructor names with its first letter in upper case
select concat(upper(left(name, 1)), lower(substring(name, 2))) as formatted_name
from faculty_info;

-- (e) list the department name of each instructor as a three letter code
select upper(left(department, 3)) as dept_code
from faculty_info;

-- (f) display the month of the joining of each instructor


select monthname(date_of_join) as joining_month
from faculty_info;

-- (g) display the date of joining of each instructor in dd/mm/yy format


select date_format(date_of_join, '%d/%m/%y') as formatted_join_date
from faculty_info;

-- (h) display the experience of each instructor in terms of months


select timestampdiff(month, date_of_join, curdate()) as experience_in_months
from faculty_info;

-- (i) display the experience of each instructor in terms of years and months
select concat(timestampdiff(year, date_of_join, curdate()), ' years, ',
mod(timestampdiff(month, date_of_join, curdate()), 12), ' months') as experience
from faculty_info;

-- (j) display the day of joining of each instructor


select dayname(date_of_join) as joining_day
from faculty_info;

-- (k) display the date corresponding to 15 days after today's date


select curdate() + interval 15 day as future_date;

-- (l) display the value 94204.27348 truncated up to 2 digits after decimal point
select truncate(94204.27348, 2) as truncated_value;

-- (m) display the value of the expression 5 + 89


select 5 + 89 as result;

-- (n) find out the square root of 6464312


select sqrt(6464312) as square_root;

-- (o) display the string “hello iter” in lower case with a column heading lower
case
select lower('HELLO ITER') as lower_case;

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------

ASSIGNMENT 4

-- 1. SQL Expressions using Suitable SQL Operators

-- (a) CREATE TABLE CUSTOMER (


CUST_NO CHAR(5) PRIMARY KEY CHECK (CUST_NO LIKE 'C____'),
NAME VARCHAR(50) NOT NULL,
PHONE_NO VARCHAR(15),
CITY VARCHAR(50) NOT NULL
);

-- (b) CREATE TABLE ACCOUNT (


ACCOUNT_NO CHAR(5) PRIMARY KEY CHECK (ACCOUNT_NO LIKE 'A____'),
TYPE ENUM('SB', 'FD', 'CA'),
BALANCE INT CHECK (BALANCE < 10000000),
BRANCH_CODE VARCHAR(5),
FOREIGN KEY (BRANCH_CODE) REFERENCES BRANCH(BRANCH_CODE)
);

-- (c) CREATE TABLE DEPOSITOR (


CUST_NO CHAR(5),
ACCOUNT_NO CHAR(5),
PRIMARY KEY (CUST_NO, ACCOUNT_NO),
FOREIGN KEY (CUST_NO) REFERENCES CUSTOMER(CUST_NO),
FOREIGN KEY (ACCOUNT_NO) REFERENCES ACCOUNT(ACCOUNT_NO)
);

-- (d) CREATE TABLE LOAN (


LOAN_NO CHAR(5) PRIMARY KEY CHECK (LOAN_NO LIKE 'L____'),
CUST_NO CHAR(5),
AMOUNT INT CHECK (AMOUNT > 1000),
BRANCH_CODE VARCHAR(5),
FOREIGN KEY (CUST_NO) REFERENCES CUSTOMER(CUST_NO),
FOREIGN KEY (BRANCH_CODE) REFERENCES BRANCH(BRANCH_CODE)
);

-- (e) CREATE TABLE BRANCH (


BRANCH_CODE VARCHAR(5) PRIMARY KEY,
BRANCH_NAME VARCHAR(50) NOT NULL,
BRANCH_CITY ENUM('DELHI', 'MUMBAI', 'KOLKATA', 'CHENNAI')
);

-- (f) CREATE TABLE INSTALLMENT (


INST_NO INT CHECK (INST_NO <= 10),
LOAN_NO CHAR(5),
INST_AMOUNT INT NOT NULL,
PRIMARY KEY (INST_NO, LOAN_NO),
FOREIGN KEY (LOAN_NO) REFERENCES LOAN(LOAN_NO)
);

-- 2. SQL Expressions using Suitable SQL Aggregate Functions

-- (a) INSERT INTO CUSTOMER VALUES


('C0001', 'RAJ ANAND SINGH', '9861258466', 'DELHI'),
('C0002', 'ANKITA SINGH', '9879958651', 'BANGALORE'),
('C0003', 'SOUMYA JHA', '9885623344', 'MUMBAI'),
('C0004', 'ABHIJIT MISHRA', '9455845425', 'MUMBAI'),
('C0005', 'YASH SARAF', '9665854585', 'KOLKATA'),
('C0006', 'SWAROOP RAY', '9437855466', 'CHENNAI'),
('C0007', 'SURYA NARAYAN PRADHAN', '9937955212', 'GURGAON'),
('C0008', 'PRANAV PRAVEEN', '9336652441', 'PUNE'),
('C0009', 'STUTI MISRA', '7870266534', 'DELHI'),
('C0010', 'ASLESHA TIWARI', NULL, 'MUMBAI');

-- (b) INSERT INTO ACCOUNT VALUES


('A0001', 'SB', 200000, 'B003'),
('A0002', 'SB', 15000, 'B002'),
('A0003', 'CA', 850000, 'B004'),
('A0004', 'CA', 35000, 'B004'),
('A0005', 'FD', 28500, 'B005'),
('A0006', 'FD', 550000, 'B005'),
('A0007', 'SB', 48000, 'B001'),
('A0008', 'SB', 7200, 'B002'),
('A0009', 'SB', 18750, 'B003'),
('A0010', 'FD', 99000, 'B004');

-- (c) INSERT INTO DEPOSITOR VALUES


('C0003', 'A0001'),
('C0004', 'A0001'),
('C0004', 'A0002'),
('C0006', 'A0003'),
('C0006', 'A0004'),
('C0001', 'A0005'),
('C0002', 'A0005'),
('C0010', 'A0006'),
('C0009', 'A0007'),
('C0008', 'A0008'),
('C0007', 'A0009'),
('C0006', 'A0010');

-- (d) INSERT INTO LOAN VALUES


('L0001', 'C0005', 3000000, 'B006'),
('L0002', 'C0001', 50000, 'B005'),
('L0003', 'C0002', 8000000, 'B004'),
('L0004', 'C0010', 100000, 'B004'),
('L0005', 'C0009', 9500000, 'B005'),
('L0006', 'C0008', 25000, 'B006');

-- (e) INSERT INTO BRANCH VALUES


('B001', 'JANAKPURI BRANCH', 'DELHI'),
('B002', 'CHANDNICHOWK BRANCH', 'DELHI'),
('B003', 'JUHU BRANCH', 'MUMBAI'),
('B004', 'ANDHERI BRANCH', 'MUMBAI'),
('B005', 'SALTLAKE BRANCH', 'KOLKATA'),
('B006', 'SRIRAMPURAM BRANCH', 'CHENNAI');

-- (f) INSERT INTO INSTALLMENT VALUES


(1, 'L0005', 500000),
(1, 'L0002', 10000),
(1, 'L0003', 50000),
(1, 'L0004', 20000),
(2, 'L0005', 500000),
(1, 'L0006', 3000),
(2, 'L0002', 10000),
(3, 'L0002', 10000),
(2, 'L0003', 50000),
(2, 'L0004', 20000);

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------

ASSIGNMENT 5

-- 1. SQL Expressions using Suitable SQL Operators

-- (a) SELECT name, phone_no, cust_no


FROM CUSTOMER
WHERE cust_no = (
SELECT cust_no
FROM ACCOUNT
WHERE account_no = 'A0004'
);

-- (b) SELECT name


FROM CUSTOMER
WHERE cust_no NOT IN (
SELECT cust_no
FROM LOAN
);

-- (c) SELECT A.ACCOUNT_NO, A.BALANCE


FROM ACCOUNT A
JOIN DEPOSITOR D ON A.ACCOUNT_NO = D.ACCOUNT_NO
WHERE D.CUST_NO = 'C0010';

-- (d) SELECT B.BRANCH_CITY


FROM BRANCH B
WHERE B.BRANCH_CODE = (
SELECT L.BRANCH_CODE
FROM LOAN L
WHERE L.CUST_NO = (
SELECT CUST_NO FROM CUSTOMER WHERE NAME = 'ASLESHA TIWARI'
)
);

-- (e) SELECT I.*


FROM INSTALLMENT I
WHERE I.LOAN_NO IN (
SELECT L.LOAN_NO
FROM LOAN L
WHERE L.CUST_NO = (
SELECT CUST_NO FROM CUSTOMER WHERE NAME = 'ANKITA SINGH'
)
);

-- (f) SELECT B.BRANCH_NAME, B.BRANCH_CITY


FROM BRANCH B
WHERE B.BRANCH_CODE IN (
SELECT A.BRANCH_CODE
FROM ACCOUNT A
JOIN DEPOSITOR D ON A.ACCOUNT_NO = D.ACCOUNT_NO
WHERE D.CUST_NO = (
SELECT CUST_NO FROM CUSTOMER WHERE NAME = 'ABHIJIT MISHRA'
)
);

-- (g) CREATE TABLE ACCOUNT_TYPE AS


SELECT ACCOUNT_NO, TYPE
FROM ACCOUNT
WHERE 1=0;

-- (h) INSERT INTO ACCOUNT_TYPE (ACCOUNT_NO, TYPE)


SELECT ACCOUNT_NO, TYPE
FROM ACCOUNT
WHERE BALANCE < 50000;

-- (i) UPDATE ACCOUNT_TYPE


SET TYPE = 'FD'
WHERE ACCOUNT_NO IN (
SELECT A.ACCOUNT_NO
FROM ACCOUNT A
JOIN DEPOSITOR D ON A.ACCOUNT_NO = D.ACCOUNT_NO
WHERE D.CUST_NO = 'C0007'
);

-- (j) DELETE FROM ACCOUNT_TYPE


WHERE ACCOUNT_NO IN (
SELECT ACCOUNT_NO
FROM ACCOUNT
WHERE BALANCE < 20000
);

-- (k) SELECT ACCOUNT_NO


FROM ACCOUNT
WHERE BALANCE > SOME (
SELECT BALANCE
FROM ACCOUNT
WHERE TYPE = 'FD'
);

-- (l) SELECT ACCOUNT_NO


FROM ACCOUNT
WHERE BALANCE > ALL (
SELECT BALANCE
FROM ACCOUNT
WHERE TYPE = 'FD'
);

-- (m) SELECT *
FROM BRANCH B
WHERE EXISTS (
SELECT 1
FROM LOAN L
WHERE L.BRANCH_CODE = B.BRANCH_CODE
);

-- (n) SELECT *
FROM LOAN L
WHERE NOT EXISTS (
SELECT 1
FROM INSTALLMENT I
WHERE I.LOAN_NO = L.LOAN_NO
);

-- (o) UPDATE ACCOUNT


SET BALANCE = BALANCE +
CASE
WHEN BALANCE > 80000 THEN BALANCE * 0.06
ELSE BALANCE * 0.05
END;

-- 2. SQL Expressions using Suitable SQL Aggregate Functions

-- (a) SELECT L.LOAN_NO


FROM LOAN L
JOIN BRANCH B ON L.BRANCH_CODE = B.BRANCH_CODE
WHERE B.BRANCH_CITY = 'MUMBAI';

-- (b) SELECT DISTINCT A.TYPE


FROM ACCOUNT A
JOIN BRANCH B ON A.BRANCH_CODE = B.BRANCH_CODE
WHERE B.BRANCH_CITY = 'DELHI';

-- (c) SELECT DISTINCT C.NAME, C.PHONE_NO


FROM CUSTOMER C
JOIN DEPOSITOR D ON C.CUST_NO = D.CUST_NO
JOIN ACCOUNT A ON D.ACCOUNT_NO = A.ACCOUNT_NO
WHERE A.BALANCE > 100000;

-- (d) SELECT I.INST_NO, I.INST_AMOUNT


FROM INSTALLMENT I
JOIN LOAN L ON I.LOAN_NO = L.LOAN_NO
JOIN CUSTOMER C ON L.CUST_NO = C.CUST_NO
WHERE C.NAME = 'RAJ ANAND SINGH';

-- (e) SELECT DISTINCT C.NAME


FROM CUSTOMER C
WHERE C.CUST_NO NOT IN (
SELECT D.CUST_NO
FROM DEPOSITOR D
JOIN ACCOUNT A ON D.ACCOUNT_NO = A.ACCOUNT_NO
WHERE A.TYPE = 'SB'
);
-- (f) SELECT DISTINCT C.NAME
FROM CUSTOMER C
JOIN LOAN L ON C.CUST_NO = L.CUST_NO
JOIN INSTALLMENT I ON L.LOAN_NO = I.LOAN_NO
WHERE I.INST_AMOUNT = 50000;

-- (g) SELECT DISTINCT C.PHONE_NO


FROM CUSTOMER C
JOIN DEPOSITOR D ON C.CUST_NO = D.CUST_NO
JOIN ACCOUNT A ON D.ACCOUNT_NO = A.ACCOUNT_NO
JOIN BRANCH B ON A.BRANCH_CODE = B.BRANCH_CODE
WHERE B.BRANCH_NAME = 'SALTLAKE BRANCH';

-- (h) SELECT DISTINCT B.BRANCH_NAME, B.BRANCH_CITY


FROM CUSTOMER C
JOIN DEPOSITOR D ON C.CUST_NO = D.CUST_NO
JOIN ACCOUNT A ON D.ACCOUNT_NO = A.ACCOUNT_NO
JOIN BRANCH B ON A.BRANCH_CODE = B.BRANCH_CODE
WHERE C.NAME = 'ABHIJIT MISHRA';

-- (i) SELECT A.TYPE, A.BALANCE


FROM CUSTOMER C
JOIN DEPOSITOR D ON C.CUST_NO = D.CUST_NO
JOIN ACCOUNT A ON D.ACCOUNT_NO = A.ACCOUNT_NO
WHERE C.NAME = 'SWAROOP RAY';

-- (j) WITH branch_total AS (


SELECT BRANCH_CODE, SUM(BALANCE) AS total_balance
FROM ACCOUNT
GROUP BY BRANCH_CODE
),
avg_total AS (
SELECT AVG(total_balance) AS avg_bal
FROM branch_total
)
SELECT BRANCH_CODE
FROM branch_total
WHERE total_balance > (SELECT avg_bal FROM avg_total);

INSERT INTO Teaches VALUES (83821, 'CS-319', 2, 'Spring', 2010);


INSERT INTO Teaches VALUES (98345, 'EE-181', 1, 'Spring', 2009);

You might also like