You are on page 1of 9

Q3.

Create following two tables including integrity constraints using SQL Command:
Table Name : Student
Field Name
Data Type
Roll No
Number
Name
Text
Address
Text

Field Size
8
20
50

Constraints
Primary Key
Not Null

Table Name : Enrollment


Field Name
Data Type
Roll No
Number

Field Size
8

Course
Grade

20
1

Constraints
Foreign Key,
Primary Key
Primary Key
A,B,C,D,E

Text
Text

Perform the following operations using SQL Commands


(i)
(ii)

List all the Students enrolled for BBA and have grade A
Find the number of students enrolled in each course.

Ans:create table student2


(rollno number(8) ,
name varchar2(20) NOT NULL,
address varchar2(50));
Output:-

create table enrollment


(rollnumber number(8) ,
course varchar2(20),
grade varchar2(1) CHECK(grade IN('A','B','C','D','E')));
Output:-

alter table student2 add constraint pk1_primarykey PRIMARY KEY(rollno);


Output:-

alter table enrollment add constraint pk2_primarykey PRIMARY KEY(rollnumber,course);


Output:-

alter table enrollment add constraint fk1_rollnumber FOREIGN KEY(rollnumber)


REFERENCES student2(rollno);
Output:-

select * from student2,enrollment where course='bba' and grade='A' and rollno=rollnumber;


Output:-

select count(course) from enrollment GROUP by course;

Q4. Consider EMP table and perform the following using SQL Commands
(i)
(ii)
(iii)
(iv)
(v)

Add a new column called total sal to the table


Update the total sal column with sal + comm.
Find the employees having salary greater than average salary of the employees
working in dept 10
List employee name and yearly salary and arrange the output on the basis of
yearly salary in descending order.
Retrieve the names of departments in ascending order and their employees in
descending order.

Ans:create table emp


(empno number(8),
dept varchar2(20),
name varchar2(20),
salary number(20),
comm number(8));
select * from emp;

alter table emp add totalsal number(20);

update emp
set totalsal=salary+comm;

select name,salary from emp where salary >(select AVG(salary) from emp);

select name,salary from emp order by salary desc;

select name,dept from emp order by dept asc,name desc;

You might also like