0% found this document useful (0 votes)
178 views52 pages

Distinct: Practical - 1 Aim To Study DDL-create and DML-insert Commands Q-1 A Query With Output

This document contains the details of several SQL queries and their outputs related to database concepts like DDL, DML, aggregation, sorting, and data manipulation. It includes 12 questions with the SQL queries to answer each question and the corresponding output. The questions cover concepts like selecting data, filtering on conditions, aggregation, creating and populating tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
178 views52 pages

Distinct: Practical - 1 Aim To Study DDL-create and DML-insert Commands Q-1 A Query With Output

This document contains the details of several SQL queries and their outputs related to database concepts like DDL, DML, aggregation, sorting, and data manipulation. It includes 12 questions with the SQL queries to answer each question and the corresponding output. The questions cover concepts like selecting data, filtering on conditions, aggregation, creating and populating tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 52

CE246-DBMS 18CE078

Practical – 1
Aim To study DDL-create and DML-insert commands

Q-1 Find the names of all the students whose total credits are greater than 100

A Query with output

SELECT distinct s.STUDENT_FIRSTNAME,c.CREDIT from stu s,cou1 c where


s.STUDENT_ID=c.student_id and c.CREDIT>100;

Q-2 Find the course id and grades of all courses taken by any student named 'Tanaka'

A Query with output

SELECT s.STUDENT_FIRSTNAME,c.C_ID,C.GRADE FROM Stu s, COU1 c where


s.student_id=c.student_id and s.student_firstname='TANAKA' ;

Q-3 Find the ID and name of instructors who have taught a course in the Comp. Sci. department,
even if they are themselves not from the Comp. Sci. department. To test this query, make

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

sure you add appropriate data, and include the corresponding insert statements along with
your query.

A Query with output

select distinct i.ins_id,i.ins_firstname from ins i,stu s where i.ins_dept='CSE' and i.ins_dept!
=s.student_deptname;

Q-4 Find the courses which are offered in both 'Fall' and 'Spring' semester (not necessarily in the
same year).

A Query with output

select distinct course_title,year,c_id from cre where sem='Spring' or sem='Fall';

Practical – 2

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Aim To study DDL-create and DML-insert commands with constraints

Write the following simple SQL Queries on the University Schema Railway
Schema Bottom Up Approach

Q-1 Find pairs of stations (station codes) that have a track (direct connection) with distance
less than 20Kms between them.

A Query with output

select station_code1,station_code2 from tb_track where distance < 20;

Q-2 Find the IDs of all the trains which have a stop at THANE.

A Query with output

select tb_station.station_name,tb_trainhault.train_id

from tb_station,tb_trainhault

where tb_station.station_code = tb_trainhault.station_code

and tb_station.station_name = ‘THANE’;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-3 Find the names of all trains that start at MUMBAI.

A Query with output

select train_name from train, tb_trainhault where sequence_no = 1

and station_code = ‘st610’ and train.train_id = tb_trainhault.train_id;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-4 List all the stations in order of visit by the train 'CST-AMR_LOCAL'.

A Query with output

Select station_name from tb_station, tb_trainhault, tb_train where tb_train.train_name =


‘CS-AMR_LOCAL’ and tb_station.station_code = tb_trainhault.station_code and
tb_train.train_id = tb_trainhault.train_id;

Q-5 Find the name of the trains which have stop at Thane, before the 6th station in the route
of the train.

A Query with output

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Practical-3

Aim To study various options of like predicate and some Built in Functions

Q-1 Retrieve all data from employee, jobs and deposit.

A Query with output

Select * from job,employee,deposit;

Q-2 Give details of account no. And deposited rupees of customers having account opened
between dates 01-01-06 and 25-07-06.

A Query with output

select a_no,cname,amount from deposit where a_date between '01-JAN-2006' and '07-
JUL-2006';

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-3 Display all jobs with minimum salary is greater than 4000.

A Query with output

select job_id,job_title from job where min_sal>20000;

Q-4 Display name and salary of employee whose department no is 20. Give alias name to

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

name of employee.

A Query with output

select emp_name,emp_sal as employee from employee where dept_no='20';

Q-5 Display employee no,name and department details of those employee whosedepartment
lies in(10,20).

A Query with output

select emp_no,emp_name,dept_no from employee where dept_no between '10' and '20

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-6 Display the non-null values of employee’s commission.

A Query with output

select emp_no,emp_name,emp_sal,dept_no from employee where emp_comm is not


null;

Q-7 Display name of customer along with its account no (both column should be displayed

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

as one) whose amount is not equal to 8000 Rs.

A Query with output

select concat(emp_no,emp_name),emp_sal as emp from employee where emp_sal!


=45000;

Q-8 Display the content of job details with minimum salary either 2000 or 4000.

A Query with output

select job_title from job where min_sal=20000 or max_sal=200000;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Practical-4

Aim To perform various data manipulation commands, aggregate functions and sorting
concept on all created tables.

Q-1 List total amount from deposit.

A Query with output

select sum(amount) from deposit;

Q-2 List total loan amount from andheri branch.

A Query with output

select sum(amount) from borrow where bname='Andheri';

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-3 Give maximum amount of loan from branch andheri.

A Query with output

select max(amount) from borrow where bname='Andheri';

Q-4 Count total number of customers.

A Query with output

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

select count(cname) from deposit;

Q-5 Count total number of customer’s cities.

A Query with output

select count(distinct bname) as cities from borrow;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-6 Create table supplier from employee with all the columns with data.

A Query with output

create table supplier as (select * from employee);

select * from supplier;

Q-7 Create table sup1 from employee with first two columns.

A Query with output

create table sup1 as(select emp_no,emp_name from employee);

select * from sup1;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-8 Create table sup2 from employee with no data.

A Query with output

create table sup2 as(select * from employee where 1=0);

select * from sup2;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-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.

A Query with output

insert into sup2 select * from employee where length(emp_name) = 4 and emp_name
like '_a__';

select * from sup2;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-10 Delete all the rows from sup1.

A Query with output

delete from sup1;

select * from sup1;

Q-11 Delete the detail of supplier whose sup_no is 103.

A Query with output

delete from supplier where emp_comm=0;

select * from supplier;

Q-12 Rename the table sup2.

A Query with output

alter table sup2 rename to supplier2;

select * from supplier2;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-13 Destroy table sup1 with all the data.

A Query with output

drop table sup1; select * from sup1;

Q-14 Update the value dept_no to 10 where second character of emp. Name is ‘m’.

A Query with output

update employee set dept_no=10 where emp_name like 'Y%';

select * from employee;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-15 Update the value of employee name whose employee number is 103.

A Query with output

update employee set emp_name='Amit' where dept_no = 12;

select * from employee;

Q-16 Add one column phone to employee with size of column is 10.

A Query with output

ALTER TABLE employee ADD phone VARCHAR(15) AFTER dept_no;

select * from employee;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-17 Modify the column emp_name to hold maximum of 30 characters.

A Query with output

alter table employee modify emp_name varchar(30);

desc employee;

Q-18 Count the total no as well as distinct rows in dept_no column with a condition of salary
greater than 1000 of employee.

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

A Query with output

SELECT DISTINCT BNAME FROM DEPOSIT;

select count(distinct dept_no) from employee where emp_sal>40000;

Q-19 Display the detail of all employees in ascending order, descending order of their
name and no.

A Query with output

select * from employee order by emp_name asc,emp_no desc;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-20 Display the dept_no in ascending order and accordingly display emp_comm in
descending order.

A Query with output

select dept_no,emp_comm from employee order by dept_no asc, emp_comm desc;

Q-21 Update the value of emp_comm to 500 where dept_no is 20.

A Query with output

update employee set dept_no=11 where emp_name like 'Y%';

select * from employee;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-22 Display the emp_comm in ascending order with null value first and accordingly sort
employee salary in descending order.

A Query with output

select emp_comm,emp_sal from employe order by emp_comm asc NULLS first;

Q-23 Display the emp_comm in ascending order with null value last and accordingly sort
emp_no in descending order.

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

A Query with output

select emp_comm,emp_sal from employee order by emp_comm asc nulls last,emp_no


desc;

Practical-5

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Aim To apply the concept of aggregating data using group functions and single raw
functions.

Q-1 List total deposit of customer having account date after 1-jan-96.

A Query with output

select sum(amount) from deposit where a_date>'1996-01-01';

Q-2 List total deposit of customers living in city nagpur.

A Query with output

select sum(amount) from deposit,customer where customer.a_no = deposit.a_no and


city = ‘Nagpur’;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-3 List maximum deposit of customers living in bombay.

A Query with output

select max(amount) from deposit,customer where customer.a_no= deposit.a_no and city


= ‘Nagpur’;

Q-4 Display the highest, lowest, sum, and average salary of all employees. Label the
columns.

A Query with output


U & P U Patel Department of Computer Engineering
7
CE246-DBMS 18CE078

SELECT MAX(emp_sal) ‘Maximum’ , MIN(emp_sal) as ‘Minimum’,SUM(emp_sal)


as ‘Sum’, AVG(emp_sal) as ‘Average’ from employee;

Q-5 Maximum, minimum, sum, and average, respectively. Round your results to the nearest
whole number.

SELECT round(MAX(emp_sal)) ‘Maximum’ , round(MIN(emp_sal)) as ‘Minimum’,


round(SUM(emp_sal)) as ‘Sum’, round(AVG(emp_sal)) as ‘Average’ from employee;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-6 Write a query that displays the difference between the highest and lowest salaries. Label
the column difference.

A Query with output

select max(amount)-min(amount) as DIFFERENCE from deposit;

Q-7 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.

A Query with output

select count(cname) from employee where DATE_FORMAT(hire_date,’fmyyyy’) IN

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

(‘2001’,’2003’,’2004’);

Q-8 Find the average salaries for each department without displaying the respective
department numbers.

A Query with output

select avg(emp_sal) from employee group by dept_no;

Q-9 Write a query to display the total salary being paid to each job title, within each
department.

A Query with output

select job_title,max_sal as salary from job;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-10 Find the average salaries > 2000 for each department without displaying the respective
department numbers.

A Query with output

SELECT AVG(emp_sal) FROM employee WHERE emp_sal>5000 group by dept_no;

Q-11 Display the job and total salary for each job with a total salary amount exceeding 3000,
in which excludes president and sorts the list by the total salary.

A Query with output

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-12 List the branches having sum of deposit more than 5000 and located in city
bombay.write a query to display the current date. Label the column date for each
employee, display the employee number, job, salary, and salary increased by 15% and
expressed as a whole number. Label the column new salary.

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-13 Modify your query no 5.(2) to add a column that subtracts the old salary from the new
salary. Label the column increase.

A Query with output

Select emp_no as employee_number , emp_sal as Salary,(emp_sal + (0.15*emp_sal))


as New_Salary, ,(emp_sal + (0.15*emp_sal)) – emp_sal as Difference from employee;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-14 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.

A Query with output

Q-15 Write a query that produces the following for each employee: <employee last name>

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

earns <salary> monthly.

A Query with output

Q-16 Display the name, hire 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.

A Query with output

Q-17 Display the hiredate of emp in a format that appears as seventh of june 1994 12:00:00

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

am.

A Query with output

Q-18 Write a query to calculate the annual compensation of all employees (sal+comm.).

A Query with output

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Practical 6
Aim To Study various Data manipulation methods.

Q-1 Give 10% interest to all depositors.

A QUERY WITH OUTPUT:

Q-2 Give 10% interest to all depositors having branch vrce.

A QUERY WITH OUTPUT:

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

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

A QUERY WITH OUTPUT:

Q-4 Write a query which changes the department number of all employees with empno 7788’s job
to employee 7844’current department number.
A QUERY WITH OUTPUT:

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

SELECT * FROM EMPLOYEE;

UPDATE employee SET dept_no=(SELECT dept_no FROM employee WHERE


emp_no=505) WHERE emp_no=509;

Q-5 Transfer 10 rs from account of anil to sunil if both are having same branch.

A QUERY WITH OUTPUT:


UPDATE deposit SET amount = amount + 10 WHERE bname = 'Andheri' AND cname
IN(SELECT cname FROM deposit WHERE bname = 'Dadar');

UPDATE deposit SET amount = amount + 10WHERE bname = 'Dadar' AND cname
IN(SELECT cname FROM deposit WHERE bname = 'Andheri');

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

SELECT * FROM DEPOSIT;

Q-6 Give 100 rs more to all depositors if they are maximum depositors in their respective
branch
A QUERY WITH OUTPUT:
UPDATE DEPOSIT SET AMOUNT=AMOUNT+100 WHERE CNAME IN (SELECT
CNAME FROM DEPOSIT WHERE AMOUNT IN (SELECT MAX(AMOUNT)
FROM DEPOSIT GROUP BY BNAME));

Q-7 Delete depositors of branches having number of customers between 1 to 3.

A QUERY WITH OUTPUT:


Create table copy_deposit as(select * from deposit);

DELETE FROM copy_deposit

WHERE bname IN (SELECT bname FROM deposit GROUP BY bname HAVING


COUNT(cname) <= 3);

Q-8 Delete deposit of Virar.

A QUERY WITH OUTPUT:


DELETE FROM DEPOSIT WHERE BNAME='Virar';

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Q-9 Delete borrower of branches having average loan less than 2000.

A QUERY WITH OUTPUT:


SELECT * FROM BORROW;

Create table copy_brw as (SELECT * FROM BORROW);

DELETE FROM copy_brw WHERE AMOUNT<2000;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Sign: Grade:

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Practical 8

CASE-STUDY PART OF JOIN :


Displaying data from multiple tables – Apply inner , outer, natural join must be used

create table class2(id number(5),name varchar2(25));

insert into class2(1,’abhi’);

insert into class2(2,’adam’);

insert into class2(3,’dax);

insert into class2(4,’anami’);

insert into class2(5,’hiteshree’);

 select * from class2;

create table class_info(id number(5) PRIMARY KEY,address varchar2(25));

insert into class_info(1,’delhi’);

insert into class_info(2,’mumbai’);

insert into class_info(3,’chennai’);

insert into class_info(7,’noida’);

insert into class_info(8,’panipat’);

 select * from class_info;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

CROSS JOIN QUERY:


select * from class2 CROSS JOIN class_info;

INNER JOIN OR EQUI JOIN QUERY:


select * from class2 INNER JOIN class_info on class2.id=class_info.id;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

NATURAL JOIN QUERY:


select id,address from class2 NATURAL JOIN class_info;

LEFT OUTER JOIN QUERY:


select * from class2 LEFT OUTER JOIN class_info on(class2.id=class_info.id);

Right OUTER JOIN QUERY:


select * from class2 RIGHT OUTER JOIN class_info on(class2.id=class_info.id);

FULL OUTER JOIN QUERY:


select * from class2 FULL OUTER JOIN class_info on (class2.id=class_info.id);

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Sign: Grade:

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Practical 10
Aim To perform basic pl/sql blocks

Q Write a PL-SQL block for checking weather a given year is a Leap year or not

A QUERY WITH OUTPUT:


SET SERVEROUTPUT ON;

DECLARE
year NUMBER;

BEGIN
year:=&year;
IF MOD(year, 4)=0
AND
MOD(year, 100)!=0
OR
MOD(year, 400)=0 THEN
dbms_output.Put_line(year
|| ' is a leap year ');
ELSE
dbms_output.Put_line(year
|| ' is not a leap year.');
END IF;

END;
SET SERVEROUTPUT ON;

DECLARE
year NUMBER;

BEGIN
year:=&year;
IF MOD(year, 4)=0
AND
MOD(year, 100)!=0
OR
MOD(year, 400)=0 THEN
dbms_output.Put_line(year

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

|| ' is a leap year ');


ELSE
dbms_output.Put_line(year
|| ' is not a leap year.');
END IF;

END;

CONCLUSION :- In this practical, we learned about basics of PL/SQL block.

Sign: Grade:

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Practical 11
Aim To perform the concept of loop

Q Find out whether given string is palindrome or not using For, While and Simple
Loop.
A QUERY WITH OUTPUT:
FOR LOOP :
Code :
SET SERVEROUTPUT ON

DECLARE
N NUMBER;
M NUMBER;
TEMP NUMBER:=0;
R NUMBER;
C NUMBER:=0;
Q NUMBER;

BEGIN
N:=&N;
M:=N;
Q:=N;
WHILE(Q>0)
LOOP
Q:=TRUNC(Q/10);
C:=C+1;
END LOOP;
FOR I IN 1..C
LOOP
R:=MOD(N,10);
TEMP:=(TEMP*10)+R;
N:=TRUNC(N/10);
END LOOP;
IF M=TEMP
THEN
DBMS_OUTPUT.PUT_LINE('TRUE');
ELSE
DBMS_OUTPUT.PUT_LINE('FALSE');
END IF;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

END;

WHILE LOOP :
Code :
SET SERVEROUTPUT ON

DECLARE
N NUMBER;
M NUMBER;
TEMP NUMBER:=0;
R NUMBER;

BEGIN
N:=&N;
M:=N;
WHILE N>0
LOOP
R:=MOD(N,10);
TEMP:=(TEMP*10)+R;
N:=TRUNC(N/10);
END LOOP;
IF M=TEMP
THEN
DBMS_OUTPUT.PUT_LINE('TRUE');
ELSE
DBMS_OUTPUT.PUT_LINE('FALSE');
END IF;

END;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

SIMPLE LOOP :
Code :
SET SERVEROUTPUT ON
DECLARE
INPUT VARCHAR(20):='OPPO';
R VARCHAR(20);
I INTEGER(10);
BEGIN
I:=LENGTH(INPUT);
LOOP
IF(I>=1) THEN
R:=R||SUBSTR(INPUT,I,1);
ELSE
EXIT;
END IF;
I:=I-1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('REVERSE : '||R);
IF INPUT=R THEN
DBMS_OUTPUT.PUT_LINE('THE GIVEN STRING '||INPUT||' IS A PALINDROME');
ELSE
DBMS_OUTPUT.PUT_LINE('THE GIVEN STRING '||INPUT||' IS
NOT A PALINDROME');
END IF;
END;

CONCLUSION :- we learned about different types of loops in PL/SQL block.

Sign : Grade:

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Practical 12
Aim To understand the concept of “select into” and “% type” attribute.

Q Create an employees table that is a replica of the emp table. Add a new column, stars, of
varchar2 data type and length of 50 to the employees table for storing asterisk (*).

Create a pl/sql block that rewards an employee by appending an asterisk in the stars column
for every rs1000/- of the employee’s salary. For example, if the employee has a salary
amount
of rs8000/-, the string of asterisks should contain eight asterisks. If the employee has a salary
amount of rs12500/-, the string of asterisks should contain 13 asterisks.
A QUERY WITH OUTPUT:
create table employees as (select * from employee);
declare v_asterisk employees.asterisk%type;
v_empno employees.emp_no%type := to_number(&v_empno);
v_sal employee.emp_sal%type;

begin

select emp_sal into v_sal from employees where emp_no = v_empno;


FOR counter IN 1..(round(v_sal/1000)) LOOP
v_asterisk := v_asterisk || '*';
END LOOP;

UPDATE employees set asterisk=v_asterisk where emp_no = v_empno;


commit;
END;

U & P U Patel Department of Computer Engineering


7
CE246-DBMS 18CE078

Select * from employees;

Conclusion : We understood the concept of select into and %type attribute.

Sign: Grade:

U & P U Patel Department of Computer Engineering


7

You might also like