Practicals 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

CE246 – Database Management System D22DCS146

Practical 1
Date:14/12/2022
Aim: To Demonstrate DDL-create and DML-insert commands.
Create tables according to the following definition.
Solution:
(1) Describe deposit, branch.

 DESC DEPOSIT;

 DESC BRANCH;

(2) Describe borrow, customers.

 DESC BORROW;

 DESC CUSTOMERS;

P a g e 1 | 33
CE246 – Database Management System
D22DCS146
(3) List all data from table DEPOSIT.
 SELECT * FROM DEPOSIT;

(4) List all data from table BORROW.


 SELECT * FROM BORROW;

(5) List all data from table CUSTOMERS.


 SELECT * FROM CUSTOMERS;
P a g e 2 | 33
CE246 – Database Management System
D22DCS146

(6) List all data from table BRANCH.


 SELECT * FROM BRANCH;

(7) Give account no and amount of depositors.


 SELECT ACTNO,AMOUNT FROM DEPOSIT

P a g e 3 | 33
CE246 – Database Management System
D22DCS146

(8) Give name of depositors having amount greater than 4000.


 SELECT AMOUNT FROM DEPOSIT WHERE AMOUNT>=4000

(9) Give name of customers who opened account after date '1-12-96'.
 SELECT ADATE FROM DEPOSIT WHERE ADATE>='12/1/96'

(10) Give name of city where branch karolbagh is located.


 SELECT CITY FROM BRANCH WHERE BNAME>='KAROLBAGH'

11) Give account no and amount of customer having account opened between date 1-12-96 and 1-6-96.
 SELECT ACTNO,AMOUNT FROM DEPOSIT WHERE ADATE>='6-1-96' AND ADATE<='12-1-
96'

(12) Give names of depositors having account at AJNI.


 SELECT * FROM DEPOSIT WHERE BNAME='AJNI'
P a g e 4 | 33
CE246 – Database Management System
D22DCS146

Conclusion/Summary:

Student Signature & Date Marks: Evaluator Signature & Date

Practical-2
Date:21/12/2022
Aim: Create the below given table and insert the data accordingly.
Perform following queries
Solution:
(2) Describe deposit, branch.
 DESC job;
P a g e 5 | 33
CE246 – Database Management System
D22DCS146

 DESC Employee

 DESC deposit_1

(2)Give details of account no. and deposited rupees of customers having account opened between dates 01-01-
06 and 25-07-06.
 SELECT A_NO,AMOUNT FROM DEPOSIT_1 WHERE A_DATE>='01/01/06' AND
A_DATE>='07/25/06'

P a g e 6 | 33
CE246 – Database Management System
D22DCS146

(3)Display all jobs with minimum salary is greater than 4000.


 SELECT job_title FROM job WHERE min_sal>=4000

(4)Display name and salary of employee whose department no is 20. Give alias name to name of employee.
 SELECT emp_name,emp_sal FROM Employee WHERE dept_no =20

(5)Display employee no, name and department details of those employee whose department lies in (10,20).
 SELECT emp_name,emp_no,dept_name FROM Employee WHERE dept_no in (10,20)

(6)Display the non-null values of employees.


 SELECT * FROM Employee WHERE emp_comm is not null

P a g e 7 | 33
CE246 – Database Management System
D22DCS146

(7)Display name of customer along with its account no (both columns should be displayed as one) whose amount
is not equal to 8000 Rs.
 SELECT a_no,cname FROM deposit_1 WHERE amount != 8000

(8)Display the content of job details with minimum salary either 2000 or 4000.
 select job_title from job where min_sal=2000 or min_sal=4000

Like practical

(1) Display all employee whose name start with ‘A’ and third character is ‘‘a’.

P a g e 8 | 33
CE246 – Database Management System
D22DCS146

 SELECT * FROM EMPLOYEE WHERE EMP_NAME LIKE 'A_a%'

(2) Display name, number and salary of those employees whose name is 5 characters long and first three
characters are‘Ani’.
 SELECT emp_no,emp_name,emp_sal FROM EMPLOYEE WHERE EMP_NAME LIKE 'Ani__%'

(3) Display all information of employee whose second character of name is either ‘M’ or ‘N’.
 SELECT * FROM Employee WHERE emp_name LIKE '_m%' or emp_name like'_n%'

(4) Find the list of all customer name whose branch is in ‘andheri’ or ‘dadar’ or ‘virar’.
 SELECT cname FROM deposit_1 WHERE bname ='andheri'or bname ='dadar' or bname ='virar'

(5) Display the job name whose first three character in job id field is ‘FI_’.
 SELECT job_title FROM job WHERE job_title LIKE 'Fi_%'

P a g e 9 | 33
CE246 – Database Management System
D22DCS146

(6) Display the title/name of job who’s last three character are ‘_MGR’ and their maximum salary is greater
than Rs12000.
 SELECT job_id FROM job WHERE job_id LIKE '%_mgr' and min_sal>=1200

(7) Display the non-null values of employees and also employee name second character should be ‘n’ and
string should be 5-character long.
 SELECT * FROM Employee WHERE emp_comm is not null and emp_name LIKE '_n___%'

(8) Display the null values of employee and also employee name’s third character should be ‘a’.
 SELECT * FROM EMPLOYEE WHERE EMP_COMM IS NULL AND EMP_NAME LIKE '__a%';

(9) What will be output if you are giving LIKE predicate as ‘%\_%’ ESCAPE ‘\’
 SELECT * FROM JOB WHERE JOB_ID LIKE '%\_%' ESCAPE '\'

P a g e 10 | 33
CE246 – Database Management System
D22DCS146

Student Signature & Date Marks: Evaluator Signature & Date

Practical-3
Date:21/12/2022
Aim: To Perform various data manipulation commands, aggregate functions and sorting concept on all created
tables.
Solution:
P a g e 11 | 33
CE246 – Database Management System
D22DCS146

(1) List total deposit from deposit.


 select sum(amount) from deposit

(2) List total loan from karolbagh branch


 select sum(amount) from deposit where bname='Karolbagh'

(3) Give maximum loan from branch vrce.


 select max(amount) from borrow where bname='VRCE'

(4) Count total number of customers


 select count(cname) from CUSTOMERS;

(5) Count total number of customer’s cities.


 select count(CITY) from CUSTOMERS;

(6) Create table supplier from employee with all the columns.
P a g e 12 | 33
CE246 – Database Management System
D22DCS146

 create table supplier as(select * from Employee)

(7) Create table sup1 from employee with first two columns.
 create table sup1 as(select emp_no,emp_name from Employee )

(8) Create table sup2 from employee with no data


 create table sup2 as(select * from Employee where 1=0 )

(9) Insert the data into sup2 from employee whose second character should be ‘n’ and string should
be 5 characters long in employee name field.
 insert into sup2 select * from Employee where emp_name like '_n___'

(10) Delete all the rows from sup1.


 delete from sup1

(11) Delete the detail of supplier whose sup_no is 103.


 delete from supplier where emp_no=103

(12) Rename the table sup2.


 rename sup2 to bob

P a g e 13 | 33
CE246 – Database Management System
D22DCS146

(13) Destroy table sup1 with all the data.


 drop table sup1

(14) Update the value dept_no to 10 where second character of emp. name is ‘m’.
 update Employee set dept_no=10 where emp_name like '_m%'

(15) Update the value of employee name whose employee number is 103.
 update Employee set emp_name='jack' where emp_no=103

(16) Add one column phone to employee with size of column is 10.
 alter table Employee add Phone number(10)

(17)Modify the column emp_name to hold maximum of 30 characters.


 alter table Employee modify emp_name varchar(40)

(18) Count the total no as well as distinct rows in dept_no column with a condition of salary
greater than 1000 of
 select count(distinct dept_no) from Employee where emp_sal>1000

(19)Display the detail of all employees in ascending order, descending order of their name and no.
 select * from Employee order by emp_name asc,emp_no desc

P a g e 14 | 33
CE246 – Database Management System
D22DCS146

(20) Display the dept_no in ascending order and accordingly display emp_comm in descending order.
 SELECT * FROM EMPLOYEE ORDER BY DEPT_NO ASC, EMP_COMM ASC

(21) Update the value of emp_comm to 500 where dept_no is 20


 UPDATE EMPLOYEE SET EMP_COMM=500 WHERE DEPT_NO=20

(22) Display the emp_comm in ascending order with null value first and accordingly sort employee
salary in descending order.
 SELECT * FROM EMPLOYEE ORDER BY EMP_COMM ASC NULLS FIRST, EMP_SAL
DESC;

(23) Display the emp_comm in ascending order with null value last and accordingly sort emp_no in
descending order.
P a g e 15 | 33
CE246 – Database Management System
D22DCS146

 SELECT * FROM EMPLOYEE ORDER BY EMP_COMM ASC NULLS LAST, EMP_NO


DESC;

Student Signature & Date Marks: Evaluator Signature & Date

Practical 4
Date:14/12/2022
Aim: To Implement Single-row functions.
Solution:

(1) Write a query to display the current date. Label the column Date.
 SELECT SYSDATE FROM DUAL

(2) For each employee, display the employee number, salary, and salary increased by 15% and expressed as a
whole number. Label the column New Salary.
P a g e 16 | 33
CE246 – Database Management System
D22DCS146

 SELECT EMP_NO,EMP_SAL,(EMP_SAL+EMP_SAL*0.15) AS NEW_SAL FROM EMPLOYEE

(3) Modify your query no (2) to add a column that subtracts the old salary from the new salary. Label the column
Increase.
 SELECT EMP_NO,EMP_SAL,(EMP_SAL+EMP_SAL*0.15) AS NEW_SAL,
(EMP_SAL+EMP_SAL*0.15-EMP_SAL) AS INC_SAL FROM EMPLOYEE

(4) Write a query that displays the employee’s names with the first letter capitalized and all other letters lowercase,
and the length of the names, for all employees whose name starts with J, A, or M. Give each column an appropriate
label.
Sort the results by the employees’ last names..
 SELECT INITCAP(EMP_NAME) AS EMPLOYRR_NAME, LENGTH(EMP_NAME) AS
LENGTH_OF_NAME FROM EMPLOYEE WHERE EMP_NAME LIKE 'A%' OR EMP_NAME
LIKE 'J%' OR EMP_NAME LIKE 'M%'

P a g e 17 | 33
CE246 – Database Management System
D22DCS146

(5) Write a query that produces the following for each employee:
<employee last name> earns <salary> monthly.
 SELECT EMP_NAME||' EARNS '||EMP_SAL||' MONTHLY '
EMPLOYEE_NAME_AND_SALARY FROM EMPLOYEE

(6) Display the name, date, number of months employed and day of the week on which the employee has started.
Order the results by the day of the week starting with Monday..
 SELECT CNAME, ADATE, ROUND(MONTHS_BETWEEN(SYSDATE, ADATE), 0)
MONTHS_WORKED, TO_CHAR(ADATE, 'Day') DAY_OF_THE_WEEK FROM
DEPOSIT ORDER BY(ADATE - NEXT_DAY(ADATE, 'Monday'));

P a g e 18 | 33
CE246 – Database Management System
D22DCS146

(7) Display the date of emp in a format that appears as Seventh of June 1994 12:00:00 AM.
 SELECT TO_CHAR(A_DATE,'DDTH MONTH YYY MM:MM:SS') FROM DEPOSIT_1

(8) Write a query to calculate the annual compensation of all employees (sal +comm.).
 SELECT SUM(EMP_SAL+EMP_COMM) FROM EMPLOYEE

P a g e 19 | 33
CE246 – Database Management System
D22DCS146

Conclusion/Summary:

Student Signature & Date Marks: Evaluator Signature & Date

Practical 5
Date:14/12/2022
Aim: Displaying data from Multiple Tables (join)
Solution:

(1) Give details of customersSANDIP.


 SELECT * FROM DEPOSIT NATURAL JOIN CUSTOMERS WHERE CNAME=SANDIP

P a g e 20 | 33
CE246 – Database Management System
D22DCS146

(2) Give name of customer who are borrowers and depositors and having living city nagpur.
 SELECT CNAME FROM CUSTOMERS INNER JOIN BORROW ON
CUSTOMERS.CNAME=BORROW.CNAME INNER JOIN DEPOSIT ON
CUSTOMERS.CNAME=DEPOSIT.CNAME AND CITY='NAGPUR'

(3) Give city as their city name of customers having same living branch.
 SELECT CUSTOMERS.CITY FROM CUSTOMERS INNER JOIN BORROW ON
BORROW.CNAME=CUSTOMERS.CNAME INNER JOIN BRANCH ON
BORROW.BNAME=BRANCH.BNAME

(4) Write a query to display the last name, department number, and department name for all employees.
 SELECT L_NAME,DEPT_NO,DEPT_NAME FROM EMPLOYEE

(5) Create a unique listing of all jobs that are in department 30. Include the location of the department in the output.
 SELECT JOB_TITLE,EMPLOYEE.LOCATION FROM JOB INNER JOIN EMPLOYEE ON
EMPLOYEE.JOB_ID = JOB.JOB_ID WHERE EMPLOYEE.DEPT_NO='30'

P a g e 21 | 33
CE246 – Database Management System
D22DCS146

(6) Write a query to display the employee’s name, department number, and department name for all employees
who work in NEW YORK.
 SELECT EMP_NAME,DEPT_NO,DEPT_NAME FROM EMPLOYEE WHERE LOCATION='new
york'

(7) Display the employee’s last name and employee number along with their manager’s last name and manager
number. Label the columns Employee, Emp#, Manager, and Mgr#, respectively..
 SELECT EMPLOYEE.L_NAME AS EMPLOYEE_LAST_NAME,
EMPLOYEE.EMP_NO AS EMP#, JOB.JOB_ID AS JOB_INFO, JOB.JOB_TITLE AS
JOB# FROM EMPLOYEE LEFT OUTER JOIN JOB ON EMPLOYEE.JOB_ID = JOB.JOB_ID;

(8) Create a query to display the name and hire date of any employee hired after employee “smith”.
 SELECT EMP_NAME, HIREDATE FROM EMPLOYEE WHERE HIREDATE >
(SELECT HIREDATE FROM EMPLOYEE WHERE EMP_NAME = 'Smith')

P a g e 22 | 33
CE246 – Database Management System
D22DCS146

Conclusion/Summary:

Student Signature & Date Marks: Evaluator Signature & Date

Practical-6
Date:04/01/2023
Aim: To apply the concept of Aggregating Data using Group functions.
Solution:
(1)List total deposit of customer having account date after 1-jan-96.

P a g e 23 | 33
CE246 – Database Management System
D22DCS146

 select sum(AMOUNT) from deposit where adate>'jan-1-96'

(2)List total deposit of customers living in city Nagpur.

 select sum(AMOUNT) from customers,deposit where deposit.cname=customers.cname and


city='NAGPUR'

(3) List maximum deposit of customers living in bombay.

 select max(AMOUNT) from customers,deposit where deposit.cname=customers.cname and


city='BOMBAY'

(4) Display the highest, lowest, sum, and average salary of all employees. Label the columns Maximum, Minimum,
Sum, and Average, respectively. Round your results to the nearest whole number.

 select max(emp_sal)"highest",min(emp_sal)"lowest",sum(emp_sal)"sum",round
(avg(emp_sal))"average salary" from Employee;

(5) Write a query that displays the difference between the highest and lowest salaries. Label the column
DIFFERENCE.

 select max(emp_sal)-min(emp_sal)"DIFFERENCE" from Employee;

P a g e 24 | 33
CE246 – Database Management System
D22DCS146

(6) Create a query that will display the total number of employees and, of that total, the number of employees
hired in 1995, 1996, 1997, and 1998

 SELECT TO_CHAR(HIREDATE,'YYYY') "YEAR",COUNT(*) "EMPLOYEE" FROM


EMPLOYEE GROUP BY TO_CHAR(HIREDATE,'YYYY') ORDER BY YEAR ASC

(7) Find the average salaries for each department without displaying the respective department numbers.
 SELECT DEPT_NO, ROUND(AVG(EMP_SAL)) "SALARY_DEPT_WISE" FROM EMPLOYEE
GROUP BY DEPT_NO;

(8) Write a query to display the total salary being paid to each job title, within each department.

 SELECT DEPT_NAME, SUM(EMP_SAL) AS DEPT_SALARY_TOTAL FROM EMPLOYEE


GROUP BY DEPT_NAME;

P a g e 25 | 33
CE246 – Database Management System
D22DCS146

(9) Find the average salaries > 2000 for each department without displaying the respective department
numbers.

 SELECT DEPT_NO, AVG(EMP_SAL) FROM EMPLOYEE GROUP BY DEPT_NO HAVING


AVG(EMP_SAL) > 2000

(10) Display the job and total salary for each job with a total salary amount exceeding 3000 and sorts the list by
the total salary.

 SELECT DEPT_NAME, SUM((EMP_SAL) + 3000) AS SalaryExtend FROM EMPLOYEE GROUP BY


DEPT_NAME ORDER BY DEPT_NAME ASC;

(11) List the branches having sum of deposit more than 5000 and located in city bombay.
 SELECT BRANCH.BNAME, SUM(AMOUNT) AS SUM FROM DEPOSIT INNER JOIN
BRANCH ON DEPOSIT.BNAME = BRANCH.BNAME WHERE BRANCH.CITY='BOMBAY'
GROUP BY BRANCH.BNAME HAVING SUM(AMOUNT)>5000

P a g e 26 | 33
CE246 – Database Management System
D22DCS146

Conclusion/Summary:

Student Signature & Date Marks: Evaluator Signature & Date

Practical-7
Date:18/01/2023
Aim: To solve queries using the concept of sub query.
Solution:
(1) Write a query to display the last name and hire date of any employee in the same department as smith. Exclude
smith.

 select l_name,hiredate from employee where dept_name=(select dept_name from employee


where emp_name='Smith')and emp_name!='Smith'
P a g e 27 | 33
CE246 – Database Management System
D22DCS146

(2) Give name of customers who are depositors having same branch city of mr. sunil.

 SELECT DEPOSIT.CNAME FROM DEPOSIT JOIN BRANCH ON DEPOSIT.BNAME =


BRANCH.BNAME AND BRANCH.CITY = (SELECT BRANCH.CITY FROM BRANCH
JOIN DEPOSIT ON BRANCH.BNAME = DEPOSIT.BNAME AND DEPOSIT.CNAME =
'SUNIL');

(3) Give deposit details and loan details of customer in same city where pramod is living.

 SELECT * FROM DEPOSIT, BORROW, CUSTOMERS WHERE CUSTOMERS.CNAME =


DEPOSIT.CNAME AND DEPOSIT.CNAME = BORROW.CNAME AND CUSTOMERS.CITY
IN (SELECT CITY FROM CUSTOMERS WHERE CNAME LIKE 'PRAMOD');

(4) Create a query to display the employee numbers and last names of all employees who earn more than the
average salary. Sort the results in ascending order of salary.

 SELECT EMP_NO, L_NAME "LASTNAME", EMP_SAL FROM EMPLOYEE WHERE


EMP_SAL > (SELECT ROUND(AVG(EMP_SAL)) FROM EMPLOYEE) ORDER BY
EMP_SAL ASC;

P a g e 28 | 33
CE246 – Database Management System
D22DCS146

(5) Give names of depositors having same living city as mr. anil and having deposit amount greater than 2000
 SELECT D.CNAME, D.AMOUNT, C.CITY FROM DEPOSIT D, CUSTOMERS C WHERE
C.CNAME = D.CNAME AND AMOUNT > 2000 AND CITY = (SELECT CITY FROM
CUSTOMERS WHERE CNAME = 'SUNIL');

(6) Display the last name and salary of every employee who reports to ford.

 SELECT L_NAME, EMP_SAL FROM EMPLOYEE WHERE MANAGER_ID = 105

(7) Display the department number, name, and job for every employee in the accounting department.

 SELECT DEPT_NO, DEPT_NAME, JOB_ID FROM EMPLOYEE WHERE JOB_ID=(SELECT


JOB_ID FROM JOB WHERE UPPER(JOB_TITLE)='ACCOUNT');

(8) List the name of branch having highest number of depositors.

 select bname from deposit group by bname having count(bname)>=all(select count(cname) from
deposit group by bname)

P a g e 29 | 33
CE246 – Database Management System
D22DCS146

(9) Give the name of cities where in which the maximum numbers of branches are located.

 select city from branch group by city having count(bname)>=all(select count(bname) from
branch group by city

(10)Give name of customers living in same city where maximum depositors are located.

 select cname,city from customers where city in (select city from customers group by city having
count(city)>1 and city in(select city from branch group by city having count(city)=(select
max(count(city)) from branch group by city)));

Conclusion/Summary:

Student Signature & Date Marks: Evaluator Signature & Date

Practical-8
Date:19/01/2023
Aim: Manipulating Data
Solution:
(1)Give 10% interest to all depositors.
P a g e 30 | 33
CE246 – Database Management System
D22DCS146

 update deposit set amount=amount + amount * 0.1

(2)Give 10% interest to all depositors having branch vrce

 update deposit set amount=amount + amount * 0.1 where BNAME = 'VIRAR'

(3)Give 10% interest to all depositors living in nagpur and having branch city bombay.

 UPDATE DEPOSIT SET AMOUNT = AMOUNT + AMOUNT * 0.1 WHERE CNAME


IN(SELECT CNAME FROM CUSTOMERS WHERE CITY = 'NAGPUR') AND BNAME
IN(SELECT BNAME FROM BRANCH WHERE CITY = 'BOMBAY');

(4) Write a query which changes the department number of all employees with empno 7788’s job to employee
7844’current department number.

 update Employee set dept_no =(select dept_no from Employee where emp _no = 101) where
emp_no = 102;

(5)Transfer 10 Rs from account of anil to sunil if both are having same branch.

update deposit set amount = amount - 10 where cname ='SUNIL' and bname =(select bname from deposit where
cname ='AJNI');

update deposit set amount = amount + 10 where cname ='MEHUL' and bname =(select bname from deposit where
cname ='POWAI');

(6)Give 100 Rs more to all depositors if they are maximum depositors in their respective branch.
P a g e 31 | 33
CE246 – Database Management System
D22DCS146

 update deposit set amount = amount + 100 where cname in (select cname from deposit where
amount = any (select max(amount) from deposit group by bname));

(7) Delete depositors of branches having number of customers between 1 to 3.

 delete from deposit where bname in (select banme from deposit group by bname having
count(bname>1) and count(bname<3);

(8) Delete deposit of vijay.

 delete from deposit where cname='MEHUL'

(9) Delete borrower of branches having average loan less than 1000.

 delete from deposit where amount<1000

Conclusion/Summary:

Student Signature & Date Marks: Evaluator Signature & Date

Practical-9
Date:15/02/2023
Aim: Add and Remove Constraint
Solution:
(1) (1) Add primary key constraint on job_id in job table..
• ALTER TABLE JOB ADD CONSTRAINT JOB_CON PRIMARY KEY(JOB_ID)

P a g e 32 | 33
CE246 – Database Management System
D22DCS146

(2) Give name of customers who are depositors having same branch city of mr. sunil.
• ALTER TABLE EMPLOYEE ADD CONSTRAINT JOB_FK FOREIGN KEY (JOB_ID)
REFERENCES JOB (JOB_ID)

(3) Add composite primary key on lock table (lock table does not exist, while creating table add composite key).
• CREATE TABLE LOCK2 (ID NUMBER(10), FIRSTNAME VARCHAR2(20), CONSTRAINT
PK_ID PRIMARY KEY (ID))

((4) Remove primary key constraint on job_id.


• ALTER TABLE JOB DROP CONSTRAINT JOB_CON;

(5) Remove foreign key constraint on employee table.


• ALTER TABLE EMPLOYEE DROP CONSTRAINT JOB_FK

Conclusion/Summary:

Student Signature & Date Marks: Evaluator Signature & Date

P a g e 33 | 33

You might also like