0% found this document useful (0 votes)
29 views125 pages

DBMS Full File

Uploaded by

saxenapansul
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)
29 views125 pages

DBMS Full File

Uploaded by

saxenapansul
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/ 125

ASSIGNMENT 1

Q1. What is SQL? What are the various features of SQL?

Solution:

Structured Query Language

 SQL was developed by IBM in 1970s for use in System R, it is an ISO and ANSI standard.
 Structured Query Language (SQL) is a language that provides an interface to relational database
systems.
 SQL is a computer language for storing, manipulating and retrieving data stored in relational database.
 All relational database management systems like MySQL, Oracle, Sybase, Informix, Postgres and SQL.
Server use SQL as standard database language.

Features of SQL

 SQL can be used by a range of users, including those with little or no programming experience.
 It is a non procedural language.
 It reduces the amount of time for creating and maintaining database systems.
 It is English like language.

Q2. Describe various Data types used in SQL.

Solution:

SQL data types

 DATE()
The date is displayed in YYYY-MM-DD format. This format is default for the DATE data type. The
DATE data type can store values from 0001-01-01 through 9999-12-31.
 CHAR(size)
CHAR is a fixed length string data type, so any remaining space in the field is padded with blanks.
CHAR takes up 1 byte per character. So, a CHAR(100) field (or variable) takes up 100 bytes in disk,
regardless of the string it holds.
 VARCHAR(size)
VARCHAR takes up 1 byte length data type. So it holds only the characters you assign to it.
VARCHAR takes up 1 byte per character, +2 bytes to hold length information. For example, if you set a
VARCHAR(100) data type=’Jen’, then it would take up 3 bytes (for J, E and N) plus 2 bytes, or 5 bytes
in all.
 INTEGER
A 32-bit signed integer value. The range of INTEGER is -2147483648 to 2147483647. The int data type
is the primary integer data type in SQL server. The bigint data type is intended for use when integer
values might exceed the range that is supported by the int data type.
 NUMBER(p,s)
Number allows a decimal component whereas integer doesn’t. If you try to store 3.43 in an integer, it
will just store 3. Number allows for much larger values than integer does. p is a precision value; s is a
scale value. For e.g, number(6,2) is a number that has 4 digits before the decimal and 2 digits after the
decimal.

Q3. Create table student with fields: Roll No. , Name, Course and Date of Birth and display its structure.

Solution:

Create table stud_data_095 (rollno number(10), name varchar(20), course varchar(30), dob date);
Desc stud_data_095;

Q4. Modify the structure of student table as follows(display structure after every modification):

1) Add two new columns named Section and Contact No.

Solution:

Alter table stud_data_095 add (section varchar(10), contactno number(10));

Desc stud_data_095;
Desc stud_data_095;

2) Change the size of course column by 2 points.

Solution:

Alter table stud_data_095 modify(course varchar(32));


Desc stud_data_095;

3) Remove the column Date of Birth.

Solution:

Alter table stud_data_095 drop column dob;

Or

Alter table stud_data_095 drop(dob);


Desc stud_data_095;

Q5. Create table Employee with fields: EmpID, EmpName, Designation and Date of Joining

Solution:

Create table employee095 (eid number(10), ename varchar(20), designation varchar(30), doj date);
Desc employee095;

Q6. Modify the structure of Employee table as follows (display structure after every modification):

1. Add a new column Salary.

Solution:

Alter table employee095 add(salary number(10));


Desc emp095;

2. Change the size of Salary by 5 points and Name by 10 Points.

Solution:

Alter table employee095 modify(salary number(15));


Alter table employee095 modify(ename varchar(30));

Desc employee095;

3. Remove the fields Designation and Date of Joining.

Solution:
Alter table employee095 drop(designation, doj);

Desc employee095;
ASSIGNMENT 2
Q1. Create table customer with fields: CID, Fname, Lname, City, State, pin.

Solution:

Create table customer_095 (CID number(10), Fname varchar(10), Lname varchar(10), City varchar(10),
State varchar(10), pin number(8));

Desc customer_095;
Q2. Insert 5 rows each using all the three , methods (All fields, Selected Fields and User interactive).

Solution:

1. All fields
Insert into customer_095 values ( 98, 'Neha', 'Sharma', 'Faridabad', 'Haryana', 121002);

2. Selected fields
Insert into customer_095 (CID, Fname, Lname, City, State)values ( 100, 'Rajesh', 'Kapoor', 'Bangalore',
'Karnataka');
3. User interactive
Insert into customer_095 values (&CID, '&Fname', '&Lname', '&City', '&State', '&pin');

Q3. Display the contents of Customer table.

Solution:

select * from customer_095;


Q4. Display CID and F name of customers.

Solution:

select CID, Fname from customer_095;

Q5. Display Fname, Lname, and State of all the customers.

Solution:

select Fname, Lname, State from customer_095;


Q6. Display all records from the customer table where state is Delhi.

Solution:

select * from customer_095 where State='Delhi';

Q7. Display fname, lname of customers concatenated with state and name it as “Customer and their
Location”.

Solution:

Select Fname||' '||Lname||' '||State as "Customer and their location" from customer_095;
Q8. Display the records of those customers whose pin is not entered.

Solution:

Select * from customer_095 where Pin is null;

Q9. Display the states to which all the customer belongs.

Solution:

Select distinct State from customer_095;


Q10. Select records of customer from Delhi having name Rajiv.

Solution:

Select * from customer_095 where State = ‘Delhi’ and Fname = ‘Rajiv’;

Q11. Display records of customer from state Delhi or Karnataka.

Solution:

Select * from customer_095 where State = ‘Delhi’ or State = ‘Karnataka’;


Q12. Display details of Customer with CID=98.

Solution:

Select * from customer_095 where CID = 98;

Q13. Display details of Customer except the customer with CID=98.

Solution:

Select * from customer_095 where CID <> 98;


Q14. Use Alias Name to display CID as Customer Number.

Solution:

Select CID as “Customer_number” from customer_095;

Q15. Retrieve all rows where CID is between 98 and 100(both numbers included).

Solution:

Select * from customer_095 where CID between 98 and 100;


Q16. Display those rows where state name begins with ‘D’.

Solution:

Select * from customer_095 where State like ‘D%’;

Q17. Retrieve all rows where first name contains the word’RAJ’.

Solution:

Select Fname from customer_095 where Fname like ‘%Raj%’;


Q18. Retrieve all rows where name field contains the word’RA ESH’.

Solution:

Select * from customer_095 where Fname like ‘Ra_esh’;

Q19. Retrieve all rows where state is Delhi or Karnataka.

Solution:

Select * from customer_095 where State in (‘Delhi’,’Karnataka’);


Q20. Rename the table customer to cust.

Solution:

Rename customer_095 to cust095;

Q21. Delete all those rows of customers who stay in Bangalore.

Solution:

Delete from cust095 where City=’Bangalore’;


Q22. Delete those customers who do not have Pin Code.

Solution:

Delete from cust095 where pin is null;

Q23. Rename column city to address.

Solution:

Alter table cust095 rename column City to Address;


Q24. Delete the customers who do not belong to Bangalore.

Solution:

Delete from cust095 where not Address=’Bangalore’;


ASSIGNMENT 3
Q1. Create table employeewith following attributes:

empid, fname, lname, bdate, address, gender, salary,dept_no.

Solution:

Create table emp_info_095 (empid number(10), fname varchar(20), lname varchar(20), bdate date,
gender varchar(8), salary number(8,2), dept_no number(2), address varchar(20));

Q2. Insert 5 records in employee table.

Solution:

Insert into emp_info_095 values (101,'Ram', 'Kapoor','28-feb-1996', 'Male', 25000, 1, 'Noida');

Insert into emp_info_095 values (102,'Namita', 'Gupta','02-Nov-1996', 'Female', 30000, 2, 'Faridabad');

Insert into emp_info_095 values (103,'Rohan', 'Khanna','02-oct-1995', 'Male', 20000, 5, 'Janakpuri');

Insert into emp_info_095 values (104,'Varun', 'Mehta','04-Apr-1997', 'Male', 50000, 2, 'Janakpuri');

Insert into emp_info_095 values (105,'Monika', 'Sharma','17-Aug-1997', 'Female', 60000, 5, 'Vikaspuri');


Q3. List name of all employees who work in Dept No. 5.

Solution:

Select fname, lname from emp_info_095 where dept_no = 5;


Q4. Find names and salary of all the employees sorted according to their salary (ascending as well as
descending).

Solution:

1. ASCENDING

Select fname, lname, salary from emp_info_095 order by salary;

2. DESCENDING

Select fname, lname, salary from emp_info_095 order by salary desc;


Q5. List names of employees having salary between 30,000 and 50,000 using logical operators.

Solution:

Select fname, lname from emp_info_095 where salary>=30000 and salary<=50000;

Q6. List name of employees who live in Janakpuri.

Solution:

Select fname, lname from emp_info_095 where address = ‘Janakpuri’;


Q7. List name of all female employees.

Solution:

Select fname, lname from emp_info_095 where gender =’Female’;

Q8. List name of those employees whose bdate is before 2 Nov. 1996.

Solution:

Select fname, lname from emp_info_095 where bdate<’02-nov-1996’;


Q9. List all those employees whose first name is “Ram” and last Name is “Kapoor”.

Solution:

Select * from emp_info_095 where fname=’Ram’ and lname=’Kapoor’;

Q10. Rename the column address to city.

Solution:

Alter table emp_info_095 rename column address to city;


Q11. Sort the employees in descending order of city.

Solution:

Select * from emp_info_095 order by city desc;

Q12. Update the person named “Ram” to “Vikas”.

Solution:

Update emp_info_095 set fname=’Vikas’ where fname=’Ram’;


Q13. Update city of empid 101 to Jaipur.

Solution:

Update emp_info_095 set city=’Jaipur’ where empid=102;

Q14. Delete record of empid 102.

Solution:

Delete from emp_info_095 where empid=102;


Q15. Display those employees whose salary is not equal to 20,000.

Solution:
Select * from emp_info_095 where salary<>20000;
ASSIGNMENT 4
Q1. Create table employee____ with following attributes:

Eno, Ename (Not Null), Salary(Not Null), Commission, Job, Deptno.

Solution:

Create table emp095 (eno number(10), ename varchar(20) not null, salary number(10) not null,
commission number(10), job varchar(20), dno number(5));

Desc emp095;

Q2. Insert 6 records in employee table.


Solution:

Insert into employee values (101,'Rishab',20000,3000,'Clerk',5);

Insert into employee values (101,'Rishab',20000,3000,'Clerk',5);

Insert into employee values (102,'Avinash',70000,5000, 'Manager',2);

Insert into employee values (103,'Rohit',45000,4000,' Manager ',3);

Insert into employee values (104,'Gagan',25000,3500,'Executive',5);

Insert into employee values (105,'Pratham',50000,4500,' Manager ',2);

Insert into emp095 values (103,'Rohit',45000,4000,' Manager ',3);

Insert into emp095 values (104,'Gagan',25000,3500,'Executive',5);

Insert into emp095 values (105,'Pratham',50000,4500,' Manager ',2);

Insert into emp095 values (106,'Abhishek',30000,3800,'Executive',1);

select * from emp095;


Q3. Find average and total salaries of all employees.

Solution:

Select avg(salary), sum(salary) from emp095;

Q4. Find minimum salary of Manager.

Solution:

Select min(salary) from emp095 where job=’Manager’;


Q5. Find the maximum salary of the Manager.

Solution:

Select max(salary) from emp095 where job=’Manager’;

Q6. How many employees are Managers.

Solution:

Select count(eno) from emp095 where job=’Manager’;


Q7. Add 50% salary to salary column and name it HRA.

Solution:

Select eno, ename, salary+(0.5*salary) as “hra” from emp095;

Q8. Count the total no. employees.

Solution:

Select count(eno) from emp095;


Q9. Count the no. of departments available.

Solution:

Select count(distinct(dno)) from emp095;

Q10. Count the no. of employees in each department.

Solution:

Select count(distinct(dno)) from emp095 group by dno;


Q11. Update the commission to 10%of salary for all the employees having salary greater than 50000.

Solution:

Update emp095 set commission=(0.1*salary) where salary>50000;

Q12. Find the salary of lowest paid employee for each department.

Solution:

Select min(salary), dno from emp095 group by dno;


Q13. Update the salaries of all employees in department no 2 by hiking it by 15%.

Solution:

Update emp095 set salary=salary+(salary*0.15) where dno=2;

Q14. Find out the difference between highest and lowest salaries for each department.

Solution:

Select max(salary)-min(salary), dno from emp095 group by dno;


Q15. List average salary of each job excluding managers

Solution:

Select job, avg(salary) from emp095 where job<>‘Manager’ group by job;


ASSIGNMENT 5
Q1.

a) Create table course with fields: Coursecode (primary key), Coursename (not null), Section
b) Create table student with following attributes Rollno, First name, Last name, Address(unique),
Coursecode(foreign key)

SOLUTION:

a) Create table course_095(coursecode number(10), cname varchar(10) not null, section varchar(10), constraint
codepk primary key(coursecode));

Desc course_095;
b) create table student_095(rollno number(10), fname varchar(10), lname varchar(10), address varchar(20),
coursecode number(10), constraint addch unique(address), constraint cdfk foreign key(coursecode)
References course_095(coursecode));

Desc student_095;
Add the following constraints to the above tables:

1. Roll no. should be primary key using alter statement


2. Add constraint on firstname to not allow blank entries
3. Add a check constraint on section i.e. it should be ‘m’ or ‘e’
4. Drop constraint primary key of student table using alter command.

SOLUTION:

1. Alter table student_095 add constraint roll_pk primary key(rollno);

2. Alter table student_095 modify fname not null;

3. Alter table course_095 add constraint sec_ck check(section in ('m','e'));


4. Alter table student_095 drop primary key;

Q2.

Person table:
P_id Firstname Lastname Address city

1 Hari Om Tilaknagar 10 Delhi

2 Shyam Tomar Base 23 Bangalore

3 Petter Joseph Staff 20 Chandigarh

Orders table:
o_id orderno p_id

1 77895 3

2 44678 3

3 22456 2

4 24562 1

Specify the following constraints while creating table:-

1. p_id (person table)-primary key


2. first name-not null constraint
3. order no –unique constraint,not null
4. p_id(order table)-foreign key
5. o_id-primary key
SOLUTION:

CREATE PERSON095:
Create table person095 (pid number(5), fname varchar(10) not null, lname varchar(10), address varchar(15),
city varchar(15), constraint pid_pk primary key(pid));

desc person095;

INSERT VALUE INTO PERSON095:


insert into person095 values(1,'Hari','Om','Tilak Nagar 10','Delhi');
insert into person095 values(2,'Shyam','Tomar','Base 25','Bangalore');
insert into person095 values(3,'Petter','Joseph','Staff 20','Chandigarh');

Select * from person095;

CREATE ORDER_095:
create table order_095(oid number(5), orderno number(10) not null, pid number(5), constraint oid__pk
primary key(oid), constraint orderno_uk unique(orderno), constraint pid_fk foreign key(pid) references
person095(pid));

desc order_095;

INSERT VALUE INTO ORDER_095:


insert into order_095 values(1,77895,3);
insert into order_095 values(2,44678,3);
insert into order_095 values(3,22456,2);
insert into order_095 values(4,24562,1);

select * from order_095;

Specify the following constraints while alter table:-


1. Add a default constraint on city as delhi.
2. Add check constraint on p_id>0.
3. Drop unique constraint order no from order table.
4. Try to delete a record from person table and find out the result.
5. Try to delete a record from order table and find out the result.
6. Note down your observation from these two queries.

SOLUTION:
1. alter table person095 modify City default 'Delhi';

Select * from person095;

2. Alter table person095 add constraint pid_chk check(pid>0);


3. Alter table order_095 drop constraint orderno_uk;

4. Delete from person095 where city='Delhi';


5. Delete from order_095 where orderno=77895;

6. We cannot delete a record from the table in which a primary key is present which is referred by a foreign
key of another table.
So, we cannot delete a record from person table because PID is a primary key in person table which is
referred by a foreign key PID in order table.
ASSIGNMENT 6
Q1. Give Syntax and Queries to demonstrate the use of following Numeric functions in System Defined
Table:
1. Absolute
2. Ceil
3. Floor
4. Round
5. Mod
6. Power
7. Tan
8. Cos
9. Sin
10. Log
11. Trunc
12. Greatest
13. Least
14. Sqrt
SOLUTION:
1. Select abs(-18.7) from dual;
2. Select ceil(18.3) from dual;

3. Select floor(18.1) from dual;


4. Select round(18.6) from dual;

5. Select mod(11,2) from dual;


6. Select power(11,2) from dual;

7. Select tan(60) from dual;


8. Select cos(45) from dual;
9. Select sin(90) from dual;

10. Select log(11,2) from dual;

11. Select trunc(11.98765,2) from dual;


12. Select greatest(11,2,17,14) from dual;
13. Select least(11,2,17,14) from dual;

14. Select sqrt(169) from dual;


Q2. Create the following table and perform the below mentioned Queries
A B C
-12.5 2 3
-13.5 4 5
0.5 6 7
1.0 8 9
2.0 10 11

1. Find the absolute value of Column A and name it as “Absolute A”


2. Find the upper and lower values of Column A and column B respectively.
3. Find the Power value using Absolute A as base and column C as its power.
4. Find the square root of Column A
5. Find the value of A mod B
6. Find the sum of column A and Column B

SOLUTION:

Create table number_sonam095(A number(4,2), B number(4,2), C number (4,2));


Desc number_sonam095;

Insert into number_sonam095 values(-12.5,2,3);


Insert into number_sonam095 values(-13.5,4,5);
Insert into number_sonam095 values(0.5,6,7);
Insert into number_sonam095 values(1.0,8,9);
Insert into number_sonam095 values(2.0,10,11);
Select * from number_sonam095;

1. Select abs(A) as “Absolute A” from number_sonam095;


2. Select ceil(A),floor(B) from number_sonam095;

3. Select power(abs(A),C) from number_sonam095;


4. Select sqrt(abs(A)) from number_sonam095;

5. Select mod(A,B) from number_sonam095;


6. Select A+B as “Sum” from number_sonam095;
Q3. Give Syntax and Queries to demonstrate the use of following Character functions in System Defined
Table
1. Initcap
2. Length
3. Substr
4. Instr
5. Trim
6. Ltrim
7. Rtrim
8. Lpad
9. Rpad
10. Upper
11. Lower
12. Replace
SOLUTION:
1. Select initcap(‘ram’) from dual;
2. Select length(‘ram’) from dual;

3. Select substr(‘university’,3,4) from dual;


4. Select instr(‘university’,’niv’) from dual;

5. Select trim(‘r’ from ‘rrrmmmrrmmmrr’) from dual;


6. Select ltrim(‘rrrmmmrrmmmrr’,’r’) from dual;

7. Select rtrim(‘rrrmmmrrmmmrr’,’r’) from dual;


8. Select lpad(‘bcom’,10,’*’) from dual;

9. Select rpad(‘bcom’,10,’*’) from dual;


10. Select upper(‘abcd’) from dual;

11. Select lower(‘ABCD’) from dual;


12. Select replace(‘amit’,’m’,’nk’) from dual;

Q4. Create the following table and perform the below mentioned Queries
Eid Ename Designation Salary City
101 Rajiv Manager 10000 Delhi
102 Sita Clerk 5000 Mumbai
103 Ram Manager 10000 Chennai
104 Ramesh Assistant 8000 Hyderabad
105 Shyam Clerk 5000 Delhi
1. Convert the name of employees into uppercase
2. Capitalize the first letter of the designation of Employees
3. Convert the city column into lowercase letters
4. Trim zeroes from salary column
5. Pad the salary to 10 digits using Zeroes on right side
6. Remove 1 from eid column
7. Prefix 2 to eid column
8. Find the location of word “el” in city column
9. Find the first two digits of city column
10. Find the length of designation column
SOLUTION:
Create table employee_sonam095 (eid number(10),ename varchar(15),designation varchar(15),salary
number(10),city varchar(15));

Desc employee_sonam095;
insert into employee_sonam095 values(101,'Rajiv','Manager',10000,'Delhi');
insert into employee_sonam095 values(102,'Sita','Clerk',5000,'Mumbai');
insert into employee_sonam095 values(103,'Ram','Manager',10000,'Chennai');
insert into employee_sonam095 values(104,'Ramesh','Assistant',8000,'Hyderabad');
insert into employee_sonam095 values(105,'Shyam','Clerk',5000,'Delhi');

Select * from employee_sonam095;


1. select upper(ename) from employee_sonam095;

2. select initcap(designation) from employee_sonam095;


3. select lower(city) from employee_sonam095;

4. select rtrim(salary,'0') from employee_sonam095;


5. select rpad(salary,10,'0') from employee_sonam095;

6. select ltrim(eid,'1') from employee_sonam095;


7. select lpad(eid,4,'2') from employee_sonam095;

8. select instr(city,'el') from employee_sonam095;


9. select substr(city,1,2) from employee_sonam095;
ASSIGNMENT 7
Q1. Create table Department with fields:

Dno(Primary Key), Dname(Unique) , Dlocation

SOLUTION:

Create table department_sonam095(dno number(5), dname varchar(15), dlocation varchar(15), constraint dnopk
primary key(dno), constraint dname_uk unique(dname));

Desc department_sonam095;
Q2. Create table Employee with fields:

Eno(Primary Key), Ename(Not Null), Salary, Designation, Dno(Foreign Key)

SOLUTION:

create table employee__sonam095(eno number(5), ename varchar(20) not null, salary number(10), Designation
varchar(20), Dno number(5), constraint eno_pk primary key(eno), constraint dno_fk foreign key(dno) references
department_sonam095(dno));

Desc employee__sonam095;
Q3. Insert 5 records in both tables

Department

Dno Dname Dlocation

10 Sales Noida

20 Finance Gurgaon

30 HR Delhi

40 Marketing Delhi

50 IT Gurgaon

Employee

Eno Ename Salary Designation Dno

101 Akash 40000 Manager 30

102 Neha 30000 Executive 50

103 Kunal 25000 Executive 10


104 Saksham 60000 Manager 50

105 Dheeraj 50000 Team Leader 20

SOLUTION:

DEPARTMENT TABLE:

insert into department_sonam095 values(10,'Sales','Noida');

insert into department_sonam095 values(20,'Finance','Gurgaon');

insert into department_sonam095 values(30,'Hr','Delhi');

insert into department_sonam095 values(40,'Marketing','Delhi');

insert into department_sonam095 values(50,'It','Gurgaon');

select * from department_sonam095;


EMPLOYEE:

insert into employee__sonam095 values(101,'Akash', 40000, 'Manager', 30);

insert into employee__sonam095 values(102,'Neha', 30000, 'Executive', 50);

insert into employee__sonam095 values(103,'Kunal', 25000, 'Executive', 10);

insert into employee__sonam095 values(104,'Shaksham', 60000, 'Manager', 50);

insert into employee__sonam095 values(105,'Dheeraj', 50000, 'Team leader', 20);


select * from employee__sonam095;

Q4. Display the details of the employee who draws maximum salary.

SOLUTION:
select * from employee__sonam095 where salary=(select max(salary)from employee__sonam095);

Q5. Display the details of those employees whose salary is greater than the average salary in the organization.

SOLUTION:

select * from employee__sonam095 where salary>(select avg(salary)from employee__sonam095);

Q6. Display the details of the employee whose Department location is Noida.

SOLUTION:
Select * from employee__sonam095 where dno =(select dno from department_sonam095 where dlocation='Noida');

Q7. Display the Department Locations to which employees belong.

SOLUTION:

Select distinct(dlocation) from department_sonam095;

Q8. Increase the Salary of Employees by 1000Rs whose Designation is Manager.

SOLUTION:
update employee__sonam095 set salary=salary+1000 where designation='Manager';

Q9. Change the column name Designation to Job.

SOLUTION:

alter table employee__sonam095 rename column designation to job;

Q10. Delete the column Dlocation.

SOLUTION:
alter table department_sonam095 drop column dlocation;
ASSIGNMENT 8
1. Create a table employee with following attributes eno(Primary Key) ,ename ,ecity, salary, deptno.

SOLUTION:
Create table employee__095(eno number(10) primary key,ename char(20),ecity char(10),salary
number(30),deptno number(20));

Desc employee__095;
2. Insert 5 records.

SOLUTION:
Insert into employee__095 values(101,'John','Jaipur',20000,10);
Insert into employee__095 values(102,'Derek','Dehradun',50000,20);
Insert into employee__095 values(103,'Sam','Shillong',12000,10);
Insert into employee__095 values(104,'Ashley','Ahemdabad',23000,20);
Insert into employee__095 values(105,'Ash','Amritsar',12000,20);

Select * from employee__095;

3. Create a view having ename and ecity.


SOLUTION:
Create view v1 as select ename,ecity from employee__095;

select * from v1;

4. In the above view update the ecity to ‘Delhi’ where ename is ‘John’.
SOLUTION:
Update v1 set ecity='Delhi' where ename='John';

5. Insert a row in the view and see if it is visible in table created.

SOLUTION:
Insert into v1 values('Amit','Hyderabad');

If the table contains the primary key then we cannot insert values through a view.
6. Create view containing ename,city,deptno and salary and update the view by increasing the salary
of all employees of department no 10 by Rs.1000.

SOLUTION:
Create view v2 as select ename,ecity,deptno,salary from employee__095;

Update v2 set salary=salary+1000 where deptno=10;

Select * from v2;


7. Create view having details of employee working in deptno=10.

SOLUTION:
Create view v_3 as select * from employee__095 where deptno=10;

Select * from v_3;


8. Create a view having grouping functions like max(sal)and min(sal).

SOLUTION:
Create view v4 as select max(salary) as maximum, min(salary) as minimum from employee__095;

Select * from v4;


9. Delete the view created.

SOLUTION:
Drop view v4;

ASSIGNMENT 9
Q1. Give Syntax and Queries to demonstrate the use of following Date functions in System Defined Table

a. Add_months(d,n)
b. Last_day(d)
c. Months between(d2,d1)
d. Next_day(d,day)

SOLUTION:

1. Select add_months('04-oct-12',3) from dual;

2. Select last_day('04-oct-12') from dual;


3. Select months_between('04-oct-12','03-aug-12') from dual;

OR

Select round(months_between('04-oct-12','03-aug-12')) from dual;


4. Select next_day('04-oct-12','Thursday') from dual;

Q2. Create table task and perform the following operations


Task_name Start_date End_Date

Planning 01-april-1995 23-april-1995

Analysis 24-april-1995 14-may-1995

Design 15-may-1995 30-may-1995

Coding 01-june-1995 30-june-1995

Testing 01-july-1995 02-aug-1995

a. Increase End date by 3 months and name the column as new end date
b. Find the last working day of every end date
c. Calculate months between start date and end date.
d. Find the next working day of every start date.

SOLUTION:

Create table task095(taskname varchar(10),startdate date,enddate date);

Desc task095;
Insert into task095 values('Planning','01-april-1995','23-april-1995');
Insert into task095 values('Analysis','24-april-1995','14-may-1995');
Insert into task095 values('Design','15-may-1995','30-may-1995');
Insert into task095 values('Coding','01-june-1995','30-june-1995');
Insert into task095 values('Testing','01-july-1995','02-august-1995');
Select * from task095;

1. Select add_months(enddate,3) as "new enddate" from task095;

2. Select last_day(enddate) from task095;


3. Select months_between(enddate,startdate) from task095;

4. Select next_day(startdate,'Monday') from task095;


Q3. Give Syntax and Queries to demonstrate the use of following Date functions in System Defined Table

a. To_Char()
b. To_Date()
c. To_number()

SOLUTION:

Select to_char('123.176','$999.9') from dual;

Select to_date('070903','mmddyy') from dual;


Select to_number('15')+3 from dual;
ASSIGNMENT 10
Q1. Create the Department table with fields: Dno(Primary Key), Dname, Dloc

Insert the following values

Dno Dname Dloc

10 Accounts New York

20 Research Dallas

30 Sales Chicago

40 Operation Boston

50 Payroll Dallas

SOLUTION:

Create table department_sonam(dno number(10) primary key, dname varchar(15), dloc varchar(15));
Desc department_sonam;

Insert into department_sonam values(10,'Accounts','New York');


Insert into department_sonam values(20,'Research','Dallas');
Insert into department_sonam values(30,'Sales','Chicago');
Insert into department_sonam values(40,'Operation','Boston');
Insert into department_sonam values(50,'Payroll','Dallas');

Select * from department_sonam;

Q2. Create the Employee table with fields: Empno(Primary Key), Ename, Salary, Commission,
Deptno(Foreign Key),Mgrid
Insert the following values

Empno Ename Salary Commission Deptno Mgrid

75 Jones 20000 20

76 Martin 30000 30 75

77 Blake 40000 1400 30 75

78 Ford 10000 20

79 Tummes 20000 5000 10 78

SOLUTION:

Create table employee_sonam(empno number(10) primary key, ename varchar(15), salary number(10),
commission number(10), deptno number(10), mgrid number(10), foreign key(deptno) References
department_sonam (dno));
Desc employee_sonam;

Insert into employee_sonam (empno,ename,salary,deptno) values(75,'Jones',20000,20);


Insert into employee_sonam (empno,ename,salary,deptno,mgrid) values(76,'Martin',30000,30,75);
Insert into employee_sonam values(77,'Blake',40000,1400,30,75);
Insert into employee_sonam (empno,ename,salary,deptno) values(77,'Ford',10000,20);
Insert employee_sonam values(79,'Tummes',20000,5000,10,78);

Select * from employee_sonam;

Q3. Create the SalGrade table with fields: Grade, Lowsal, Highsal

Insert the following values

Grade Lowsal Highsal


A 10000 19000

B 20000 29000

C 30000 50000

SOLUTION:

Create table salgrade_sonam(grade varchar(10), lowsal number(15), highsal number(15));


Desc salgrade_sonam;

Insert into salgrade_sonam values('A', 10000,19000);


Insert into salgrade_sonam values('B', 20000,29000);
Insert into salgrade_sonam values('C', 30000,50000);
Select * from salgrade_sonam;

Q4. Display Empno, Salary,Deptname of employees

SOLUTION:

Select empno,salary,deptno from employee_sonam,department_sonam where deptno=dno;


Q5. Use table Alias & solve the above query again

SOLUTION:

Select empno,salary,deptno from employee_sonam e,department_sonam d where e.deptno=d.dno;

Q6. Display Empno., ename,Grade and salary that should be greater than lowsal and less than highsal

SOLUTION:

Select empno,ename,grade,salary from employee_sonam e,salgrade_sonam s where salary>=lowsal and


salary<=highsal;
Q7. Retrieve name of employees where employee is also a manager

SOLUTION:

Select e.ename from employee_sonam e,employee_sonam m where e.empno=m.mgrid;

Q8. Display Empno, Ename, Dname ,Dloc of all the employees using outer join

SOLUTION:

Select empno,ename,dname,dloc from employee_sonam e left join department_sonam d on


e.deptno=d.dno;
Q9. Display Empno, Ename, Dname ,Dloc of all the departments using outer join

SOLUTION:

Select empno,ename,dname,dloc from employee_sonam e right join department_sonam d on


e.deptno=d.dno;

Q10. Retrieve employee no, employee name of all the employees along with their manager no and
manager name if any

SOLUTION:

Select e.empno,e.ename,m.mgrid,m.ename from employee_sonam e,employee_sonam m where


e.empno=m.mgrid;
ASSIGNMENT 11

Q1. Create table course with fields: ccode(PK), cname(Unique),fee

SOLUTION:

Create table course095(ccode number(10) primary key,cname varchar(10) unique,fee number(15));


desc course095;

Q2. Create table student with following fields: RollNo(PK), Sname, DOB(not Null), Coursecode(FK),

Year of admission

SOLUTION:
Create table student095_sonam(rollno number(10) primary key,sname varchar(10),dob date not null,
coursecode number(10) ,year_of_addmission number(10),foreign key (coursecode) references
course095(ccode));
desc student095_sonam;

Q3. Insert 5 records in each table

SOLUTION:

Select * from course095;


select * from student095_sonam;

Q4. Find the age of the students

SOLUTION:

Select sname,round(months_between('31-dec-2022',dob)) from student095_sonam;

Q5. Find the roll no, Name, course name of every student
SOLUTION:

select rollno,sname,cname from student095_sonam,course095 where ccode=coursecode;

Q6. Find the details of the students who are in BBACAM course

SOLUTION:

Select * from student095_sonam where coursecode=(select ccode from course095 where


cname='BBACAM');

Q7. Find the list of students who have taken admission in the same year

SOLUTION:
Select * from student095_sonam order by year_of_addmission;

Q8. Find the number of students who have taken admission year wise

SOLUTION:

Select year_of_addmission,count(rollno) from student095_sonam group by year_of_addmission;

Q9. Find the details of the students who are in the course in which student with roll no 101 studies

SOLUTION:
Select * from student095_sonam where coursecode=(select coursecode from student095_sonam where
rollno=101);

Q10. Find the details of the students who have paid the fees for the costliest course

SOLUTION:

Select * from student095_sonam where coursecode=(select ccode from course095 where fee=(select
max(fee) from course095));

You might also like