Uploaded by 23Z312 - DAKSHIN KARTHICK S

SQL Database Lab: Departments, Employees, Bank Tables

advertisement
1. Create ‘Departments’ table with the following columns and constraints and insert records into the
table:
-- Creating the table
CREATE TABLE Departments (
deptno NUMBER PRIMARY KEY,
deptname VARCHAR2(50) NOT NULL,
location VARCHAR2(50)
);
-- Insert records into the 'Departments' table
INSERT INTO Departments (deptno, deptname, location) VALUES (10, 'HR', 'Chennai');
INSERT INTO Departments (deptno, deptname, location) VALUES (20, 'Finance', 'Pune');
INSERT INTO Departments (deptno, deptname, location) VALUES (30, 'Sales', 'Coimbatore');
INSERT INTO Departments (deptno, deptname, location) VALUES (40, 'Production', 'Coimbatore');
INSERT INTO Departments (deptno, deptname, location) VALUES (50, 'Marketing', 'Mumbai');
-- Displaying the records
SELECT * FROM Departments
Output:
2. Create ‘Employees’ table with the following columns and constraints and insert records into the
table:
-- Creating the table
CREATE TABLE Employees (
empno NUMBER PRIMARY KEY,
empname VARCHAR2(50) NOT NULL,
designation VARCHAR2(50),
manager NUMBER,
hiredate DATE,
salary NUMBER,
deptno NUMBER,
CONSTRAINT fk_deptno FOREIGN KEY (deptno) REFERENCES Departments(deptno)
);
-- Insert records into the 'Employees' table
INSERT INTO Employees VALUES (101, 'Jagan', 'Manager', NULL, TO_DATE('2019-10-01', 'YYYY-MMDD'), 60000, 20);
INSERT INTO Employees VALUES (102, 'Sujith', 'Clerk', 101, TO_DATE('2022-12-01', 'YYYY-MM-DD'),
15000, 20);
INSERT INTO Employees VALUES (103, 'Joshi', 'Analyst', NULL, TO_DATE('2020-11-01', 'YYYY-MM-DD'),
45000, 50);
INSERT INTO Employees VALUES (104, 'Santhosh', 'Programmer', NULL, TO_DATE('2022-05-01', 'YYYYMM-DD'), 25000, 40);
INSERT INTO Employees VALUES (105, 'Archana', 'Clerk', NULL, TO_DATE('2024-01-02', 'YYYY-MMDD'), 12000, 10);
INSERT INTO Employees VALUES (106, 'Lokesh', 'Analyst', NULL, TO_DATE('2021-07-01', 'YYYY-MMDD'), 70000, 30);
INSERT INTO Employees VALUES (107, 'Siva', 'Programmer', NULL, TO_DATE('2023-11-01', 'YYYY-MMDD'), 40000, 40);
INSERT INTO Employees VALUES (108, 'Tamil', 'Team Lead', NULL, TO_DATE('2022-10-01', 'YYYY-MMDD'), 60000, 50);
INSERT INTO Employees VALUES (109, 'Ajith', 'Sales Executive', NULL, TO_DATE('2023-02-01', 'YYYYMM-DD'), 20000, 30);
INSERT INTO Employees VALUES (110, 'Vijay', 'MD', NULL, TO_DATE('2017-12-01', 'YYYY-MM-DD'),
100000, 10);
-- Displaying the records
SELECT * FROM Employees
Output:
3. Write the update statement to increment the salary of employees belonging to department
number 10.
--Before updating the department number 10
--Updating
UPDATE Employees SET salary = salary + 4000 WHERE deptno = 10;
Output:
4. Display details of employees whose designation is ‘clerk’ or ‘analyst’ or ‘programmer’.
--Query
SELECT * FROM Employees WHERE designation IN ('Clerk', 'Analyst', 'Programmer');
Output:
5. Display details of employees whose name begin with the letter ‘J’.
--Query
SELECT * FROM Employees WHERE empname LIKE 'J%';
Output:
6. Display details of employees who belong to department number 10 and whose salary is
greater than 20000.
--Query
SELECT * FROM Employees WHERE deptno = 10 AND salary > 20000;
Output:
7. Display the details of employees who belong to department number 20 in the descending order of
salary.
--Query
SELECT * FROM Employees WHERE deptno = 20 ORDER BY salary DESC;
Output:
8. Display the ‘distinct’ designation in ascending order from employees table.
--Query
SELECT DISTINCT designation FROM Employees ORDER BY designation ASC;
Output:
9. Display the details of employees who are employed less than 24 months.
--Query
SELECT * FROM Employees WHERE hiredate> SYSDATE - INTERVAL '24' MONTH;
Output:
10. Display minimum, maximum, and average salary of every department.
--Query
SELECT deptno, MIN(salary) AS Minimum_Salary, MAX(salary) AS Maximum_Salary, AVG(salary) AS
Average_Salary FROM Employees GROUP BY deptno;
Output:
11. Display the department numbers and maximum salary for those departments whose
maximum salary is greater than 40000.
--Query
SELECT deptno, MAX(salary) AS Maximum_Salary FROM Employees GROUP BY deptno HAVING
MAX(salary) > 40000;
Output:
12. Display employee names and department names of all employees.
--Query
SELECT empname, d.deptname FROM Employees e JOIN Departments d ON e.deptno = d.deptno;
Output:
13. Display the details of all employees whose department is located in Chennai.
--Query
SELECT * FROM Employees e JOIN Departments d ON e.deptno = d.deptno WHERE d.location =
'Chennai';
Output:
14. Display employee name, salary, and department number of all employees whose salary is
minimum.
--Query
Ex No: 02
Bank Database – DDL and DML statements
SELECT empname, salary, deptno FROM Employees WHERE salary = (SELECT MIN(salary) FROM
Employees);
Output:
15. Display the employee name and hiredate for all employees in the same department as
‘Archana’.
--Query
SELECT empname, hiredate FROM Employees WHERE deptno = (SELECT deptno FROM Employees
WHERE empname = 'Archana');
Output:
1. Create a table ‘bank’ with the following columns and constraints and insert records into the table:
-- Creating the table
CREATE TABLE bank (
bank_id NUMBER PRIMARY KEY,
bank_name VARCHAR2(20) NOT NULL,
city VARCHAR2(20)
);
-- Insert records into the ‘bank’ table
INSERT INTO bank (bank_id, bank_name, city) VALUES (1, 'SBI', 'Coimbatore');
INSERT INTO bank (bank_id, bank_name, city) VALUES (2, 'IOB', 'Mumbai');
INSERT INTO bank (bank_id, bank_name, city) VALUES (3, 'ICICI ', 'Chennai');
INSERT INTO bank (bank_id, bank_name, city) VALUES (4, 'HDFC', 'Pune');
INSERT INTO bank (bank_id, bank_name, city) VALUES (5, 'Axis', 'Kochi');
-- Displaying the records
SELECT * FROM bank;
Output:
2. Create a table ‘customer’ with the following columns and constraints and insert records into the
table:
-- Creating the table
CREATE TABLE customer (
cus_id NUMBER PRIMARY KEY,
cus_name VARCHAR2(20) NOT NULL,
city VARCHAR2(30),
phone_no NUMBER
);
-- Insert records into the ‘customer’ table
INSERT INTO customer (cus_id, cus_name, city, phone_no) VALUES (101, 'Lokesh', 'Mumbai',
9876543210);
INSERT INTO customer (cus_id, cus_name, city, phone_no) VALUES (102, 'Naveen', 'Chennai',
9876543211);
INSERT INTO customer (cus_id, cus_name, city, phone_no) VALUES (103, 'Sam', 'Pune', 9876543212);
INSERT INTO customer (cus_id, cus_name, city, phone_no) VALUES (104, 'Santhosh', 'Kochi',
9876543213);
INSERT INTO customer (cus_id, cus_name, city, phone_no) VALUES (105, 'Siva', 'Coimbatore',
9876543214);
INSERT INTO customer (cus_id, cus_name, city, phone_no) VALUES (106, 'Ramya', 'Mumbai',
9876543215);
INSERT INTO customer (cus_id, cus_name, city, phone_no) VALUES (107, 'Abhi', 'Chennai', 9876543216);
INSERT INTO customer (cus_id, cus_name, city, phone_no) VALUES (108, 'Elisa', 'Chennai',
9876543217);
INSERT INTO customer (cus_id, cus_name, city, phone_no) VALUES (109, 'Nithya', 'Pune', 9876543218);
INSERT INTO customer (cus_id, cus_name, city, phone_no) VALUES (110, 'Santhiya', 'Coimbatore',
9876543219);
-- Displaying the records
SELECT * FROM customer
Output:
3. Create a table ‘account’ with the following columns and constraints and insert records intothe
table:
-- Creating the table
CREATE TABLE account (
acc_no NUMBER PRIMARY KEY,
acc_type VARCHAR2(20) NOT NULL,
start_date DATE,
balance NUMBER,
bank_id NUMBER,
cus_id NUMBER,
FOREIGN KEY (bank_id) REFERENCES bank(bank_id),
FOREIGN KEY (cus_id) REFERENCES customer(cus_id)
);
-- Insert records into the ‘account table
INSERT INTO account (acc_no, acc_type, start_date, balance, bank_id, cus_id) VALUES (1001, 'Savings',
TO_DATE('2020-05-05', 'YYYY-MM-DD'), 6000, 1, 105);
INSERT INTO account (acc_no, acc_type, start_date, balance, bank_id, cus_id) VALUES (1002, 'Savings',
TO_DATE('2017-03-17', 'YYYY-MM-DD'), 5500, 2, 101);
INSERT INTO account (acc_no, acc_type, start_date, balance, bank_id, cus_id) VALUES (1003, 'Current',
TO_DATE('2020-09-19', 'YYYY-MM-DD'), 45000, 3, 102);
INSERT INTO account (acc_no, acc_type, start_date, balance, bank_id, cus_id) VALUES (1004, 'Current',
TO_DATE('2023-04-23', 'YYYY-MM-DD'), 25000, 4, 103);
INSERT INTO account (acc_no, acc_type, start_date, balance, bank_id, cus_id) VALUES (1005, 'Fixed
Deposit', TO_DATE('2013-01-25', 'YYYY-MM-DD'), 1200, 5, 104);
INSERT INTO account (acc_no, acc_type, start_date, balance, bank_id, cus_id) VALUES (1006, 'Fixed
Deposit', TO_DATE('2023-06-08', 'YYYY-MM-DD'), 10000, 1, 110);
INSERT INTO account (acc_no, acc_type, start_date, balance, bank_id, cus_id) VALUES (1007,
'Reccuring Deposit', TO_DATE('2020-12-11', 'YYYY-MM-DD'), 33000, 2, 106);
INSERT INTO account (acc_no, acc_type, start_date, balance, bank_id, cus_id) VALUES (1008,
'Reccuring Deposit', TO_DATE('2019-08-01', 'YYYY-MM-DD'), 80000, 3, 107);
INSERT INTO account (acc_no, acc_type, start_date, balance, bank_id, cus_id) VALUES (1009, 'Salary
Account', TO_DATE('2020-02-10', 'YYYY-MM-DD'), 2500, 4, 109);
INSERT INTO account (acc_no, acc_type, start_date, balance, bank_id, cus_id) VALUES (1010, 'Salary
Account', TO_DATE('2015-10-07', 'YYYY-MM-DD'), 120000, 3, 108);
-- Displaying the records
SELECT * FROM customer
Output:
4. Write an update statement to increment the balance by 0.10 percent for all savings account.
--Before updating the savings account by 0.10 percent
--Updating
UPDATE account SET balance = balance * 1.001 WHERE acc_type = 'Savings';
Output:
5. Display all details of all accounts whose type is ‘savings’.
--Query
SELECT * FROM account WHERE acc_type = 'Savings';
Output:
6. Display all account numbers which have balance in the range 5000 and 10000.
--Query
SELECT acc_no FROM account WHERE balance BETWEEN 5000 AND 10000;
Output:
7. Display names of customers whose name begin with ‘s’ and has account in bank_id=1.
--Query
SELECT c.cus_name FROM customer c JOIN account a ON c.cus_id = a.cus_id WHERE c.cus_name
LIKE 'S%' AND a.bank_id = 1;
Output:
8. Display the details of accounts which were started in the year 2020.
--Query
SELECT * FROM account WHERE EXTRACT(YEAR FROM start_date) = 2020;
Output:
9. Display the customer number and account number of all accounts which were created after15th
of every month.
--Query
SELECT c.cus_id, a.acc_no FROM customer c JOIN account a ON c.cus_id = a.cus_id WHERE
EXTRACT(DAY FROM a.start_date) > 15;
Output:
10. Display the number of distinct accounts, total balance and average balance of all accounts.
--Query
SELECT COUNT(DISTINCT acc_no) AS num_accounts, SUM(balance) AS total_balance, AVG(balance)
AS avg_balance FROM account;
Output:
11. Display the bank_id which has the maximum number of accounts.
--Query
SELECT bank_id,num_accounts FROM
(SELECT bank_id, COUNT(*) AS num_accounts FROM account GROUP BY bank_id ORDER BY
num_accounts DESC)
WHERE ROWNUM = 1;
Output:
12. Display the bank name and the number of customers in that bank.
--Query
SELECT b.bank_name, COUNT(DISTINCT c.cus_id) AS num_customers FROM bank b
JOIN account a ON b.bank_id = a.bank_id
JOIN customer c ON a.cus_id = c.cus_id
GROUP BY b.bank_name;
Output:
13. Display the details of all customers who have account in ‘SBI’.
--Query
SELECT * FROM customer WHERE cus_id IN
(SELECT cus_id FROM account WHERE bank_id = (SELECT bank_id FROM bank WHERE bank_name =
'SBI'));
Output:
14. Display the details of all customers who reside in ‘coimbatore’ and whose balance is
greaterthan 20000;
--Query
SELECT c.* FROM customer c JOIN account a ON c.cus_id = a.cus_id WHERE c.city = 'Coimbatore' AND
a.balance> 20000;
Output:
15. Display the account number and start date of all accounts which are in the same bank in
which‘sam’ is having an account.
--Query
SELECT acc_no, start_date FROM account where bank_id = (select account.bank_id from account
inner join customer on customer.cus_id=account.cus_id where customer.cus_name='Sam');
Output:
Download