You are on page 1of 41

ASSIGNMENT OF RDBMS

(Relational Database
Management System)

SUBMITTED TO: Mr. DINESH KUMAR


SUBMITTED BY: Deepa k REG.N O. 10803590 SECTI ON B6801A20 BATC H: 3

ASSIGNMENT 1: TO UNDERSTAND ER DIAGRAM. QUES 1) Draw an ER Diagram for the following scenario: 1) There are multiple banks and each bank has many branches. Each branch has multiple customers 2) Customers have various types of accounts 3) Some Customers also had taken different types of loans from these bank branches 4) One customer can have multiple accounts and Loans ANS: 1) Identify the Entities i) ii) iii) iv) v) Bank Branches Customers Accounts Loans

2) Find the Relationships i) Bank has many branches. The Cardinality between Bank and Branch is one to many Bank 1 Has ii) Each branch has multiple customers M Branches

The Cardinality between Bank and Branch is one to many Branch 1 Has M Customers

iii) One customer can have multiple accounts The Cardinality between Bank and Branch is one to many Customer 1 Has M Accounts

iv) One customer can have multiple and Loans The Cardinality between Bank and Branch is one to many Customer 1 Has M Loans

3) Identify the Key Attributes Bank Name is the key attribute of Entity Bank which uniquely identifies the Bank Location is the key attribute of Entity Branch which uniquely identifies the Branch Customer# (Customer Id) is the key attribute of Entity Customer which uniquely Identifies the Customer Loan Term is the key attribute of Entity Loan Account Title is the key attribute of Entity Account

4) Identify the Other Key Attributes

For Bank Entity, the relevant attribute is Location For Branch Entity, the relevant attribute is Name For Customer Entity, the relevant attribute is Name For Loan Entity, the relevant attribute is Loan amount For Account Entity, the relevant attribute is Account type

5) Draw the E R Diagram Locatio n

Name

Location

Name

1 Bank

H as

M Branches 1

H as

M Customers

C-Id

Name 1 H as Term Amoun t M Loans 1 H as M Accounts Type Title

ASSIGNMENT 2: TO CONVERT ER DIAGRAM TO RELATIONAL SCHEMA

ANS: The relational schema is: BOOK(ISBN,title,price,year) AUTHOR(name,address) PUBLISHER(emailid,name,telephone#,address) WAREHOUSE (code, phone, and address) AUTHOR E-MAILID TELEPHONE# NAME BOOK ISBN PRICE ADDRESS YEAR

PUBLISHER

WAREHOUSE

CODE

PHONE

ADDRESS

ASSIGNMENT 3: NORMALIZATION QUES 1: Let us find out the Primary Key and highest normal form for this relation: R{a,b,c,d,e,} a,e c, d e, a,eb, a,ed ANS: The Primary Key is ( a,e ) All other attributes are functionally dependent on both a and e. All the attributes are atomic in nature therefore Relation R is in 1NF. All non-key attributes (b, c, d) are fully functionally dependent on the primary key (a, e). Therefore the relation in is 2NF. All the non-key attributes (b, c, d) are non-transitively depending on primary key (a, e). Therefore the relation is in 3NF. QUES 2: Find the primary key and the highest normal form of following relations
a. R{a,b,c} a,bc and ca b. R{a,b,c,d} ab, ad and bc c. R{a,b,c,d,e} a,bc, a,bd and de

ANS: a) R {a, b, c} a, bc and ca The Primary Key is (a,b) All the attributes are in atomic level or are atomic in nature therefore Relation R is in 1NF.

All non-key attributes are fully functionally dependent on the primary key. Therefore the relation in is 2NF. The relation is also in 3NF b) R {a, b, c, d} ab, ad and bc The Primary Key is a All the attributes are in atomic level or are atomic in nature therefore Relation R is in 1NF. All non-key attributes are fully functionally dependent on the primary key . Therefore the relation in is 2NF. The relation is also in 3NF because all non key attributes are not transitively dependent on key attribute. c) R {a, b, c, d, e} a, bc, a, bd and de The Primary Key is a,b All the attributes are in atomic level or are atomic in nature therefore Relation R is in 1NF. All non-key attributes are fully functionally dependent on the primary key. Therefore the relation in is 2NF. The relation is also in 3NF because all non key attributes are not transitively dependent on key attribute.

ASSIGNMENT 4: NORMALIZATION QUES 1: Convert following table to 3NF table. Please add appropriate columns in normalized tables to make 3NF compliant. Note: Consider address column as simple attribute Student Id 1001 1002 1003 1004 1005 Student Name Address Course Id 101 102 103 104 105 Course Name CHSSC PF RDBMS UNIX IWT Date Of Exam 06-Mar-08 15-Mar-08 22-Mar-08 02-Apr-08 10-Apr-08 Marks Grade 83 72 89 63 95 A B A C A

21, Lion Blvd, Lawrence St Peter 29, Tiger Blvd, Jagan Burbank 213,Parliament Bobby Blvd, Petas 401,Gazipura Ramsey Blvd,Gulbarga Chang 101,Marshal Hu Blvd,Kingston

ANS: All the attributes are in atomic level or are atomic in nature. Therefore the above table is in 1NF For 2NF, we have to remove the partial dependencies. Functional Dependencies of the above table. Let us suppose the table name is Student details Studentdetails (Student Id, Course Id, StudentName, Address, CourseName, Date of Exam, Marks, Grade) Student Id -> StudentName, Address, Marks, Grade {Partially dependent on key attribute}

Course Id -> CourseName, Date of Exam {Partially dependent on key attribute} Student Id, Course Id -> Marks, Grade {Fully dependent on key attribute} Marks -> Grade {No dependency on key attribute}

After removing the partially dependency, we get the following tables which are in 2NF Student Info Student Id 1001 1002 Jagan 1003 Bobby 1004 Ramsey 1005 Chang Hu Student Name Lawrence Address 21, Lion Blvd, St Peter 29, Tiger Blvd, Burbank 213,Parliament Blvd, Petas 401,Gazipura Blvd,Gulbarga 101,Marshal Blvd,Kingston

Course Info Course Id 101 102 103 104 105 Course Name CHSSC PF RDBMS UNIX IWT Date of Exam 06-Mar-08 15-Mar-08 22-Mar-08 02-Apr-08 10-Apr-08

Marks Info Student Id 1001 1002 1003 1004 1005 Course Id 101 102 103 104 105 Marks 83 72 89 63 95 Grade A B A C A

For 3NF, Non key attributes are not transitively dependent on key attribute. Course Id -> Marks -> Grade The above dependency is a transitive dependency. After removing the transitive dependency, we get the following tables which are in 3NF Marks Info Student Id 1001 1002 1003 1004 1005 Course Id 101 102 103 104 105 Marks 83 72 89 63 95

Marks Grade Info Marks 83 72 89 63 Student Id 1001 95 1002 Jagan 1003 Bobby 1004 Ramsey 1005 Chang Hu Grade A B A C Student Name Lawrence A Student Info Address 21, Lion Blvd, St Peter 29, Tiger Blvd, Burbank 213,Parliament Blvd, Petas 401,Gazipura Blvd,Gulbarga 101,Marshal Blvd,Kingston

Course Info Course Id 101 102 103 104 105 Course Name CHSSC PF RDBMS UNIX IWT Date of Exam 06-Mar-08 15-Mar-08 22-Mar-08 02-Apr-08 10-Apr-08

ASSIGNMENT 5: SQL QUERIES QUES 1: Create the following tables with appropriate constraints and insert the given sample data. i) Applicant Table Column name Data type Description Unique Id generated for an applicant when he or she applies for admission in the college. Name of the applicant email Id of the applicant Complete address of Applicant Constraints

ApplicantId ApplicantName EmailId Address City

Varchar2(4) Varchar2(30) Varchar2(30) Varchar2(50) Varchar2(15)

Primary Key Not null Should be Unique

ANS: The query for creation of this table is: CREATE TABLE Applicant (ApplicantId varchar2 (4) CONSTRAINT applicant_pk PRIMARY KEY, ApplicantName varchar2 (30) CONSTRAINT applicant_applicantname_notnull NOT NULL, EmailId varchar2 (30) CONSTRAINT applicant_emailid_uq UNIQUE, Address varchar2 (50), city varchar2 (15));

ii) Branch Table Column name BranchId BranchName Data type Varchar2(2) Varchar2(30) Description Unique Id generated for a branch Name of the branch Constraints Primary Key, Should start with B Not Null

ANS: The query for creation of this table is: CREATE TABLE Branch (BranchId varchar2 (2) CONSTRAINT branch_pk PRIMARY KEY CONSTRAINT branch_bid CHECK (BranchId like b %), BranchName varchar2 (30) CONSTRAINT branch_branchName_notnull NOTNULL);

iii) Course Table Column name CourseId CourseName Semester BranchId Elective ProjectMarks AssignmentMarks InternalMarks Data type Description Varchar2(4) Unique Id given to each course varchar2(30) Name of the course Name of the semester in which the Number(1) course is taught Id of the branch where the course is Varchar2(2) taught char(1) Number(3) Number(3) Number(3) Whether it is elective paper or not Marks of the project in the course (if any) Assignment Marks for the course (if any) Internal Marks for the course (if any) Marks of the semester exam of the course Constraints Primary Key Not Null

Foreign key to Branch Table Its value can be 'Y' or 'N' only

SemesterExamMarks Number(3)

ANS: The query for creation of this table is: CREATE TABLE Course (CourseId varchar2 (4) CONSTRAINT course_pk PRIMARY KEY, CourseName varchar2 (30) CONSTRAINT course_coursename_notnull NOT NULL, Semester number (1),

BranchId varchar2 (2) CONSTRAINT course_fk REFERENCES Branch (BranchId), Elective char (1) IN (Y,N), ProjectMarks number (3), AssignmentMarks number (3), Column name StudentId ApplicantId CurrentSemester UserId Password Data type Varchar2(4) Varchar2(4) number(1) Varchar2(15) Varchar2(15) Description Unique id generated for each student when he or she gets admitted in to the college Applicant Id when the student applied for the admission in the college Semester in which the student is currently studying Decided at the time of selection in the college Decided at the time of selection in the college Constraints Primary Key Foreign key to Applicant table It should be unique Not Null

InternalMarks number (3), SemesterExamMarks number (3)); iv) Student Table

ANS: The query for creation of this table is: CREATE TABLE Student (StudentId varchar2 (4) CONSTRAINT student_pk PRIMARY KEY, ApplicantId varchar2 (4) CONSTRAINT student_fk REFERENCES Applicant (ApplicantId), CurrentSemester number (1), UserId varchar2 (15) CONSTRAINT student_userid_uq UNIQUE, Password varchar2 (15) CONSTRAINT student_password_notnull NOT NULL); v) Registration Table Column name Data type Description Constraints

StudentId CourseId DateOfExam ProjectMarks AssignmentMarks InternalMarks SemesterMarks Grade

Student Id of the student who has taken Varchar2(4) up the course Course Id of the course in which the Varchar2(4) student has appeared Date on which the semester exam was Date conducted Marks obtained by student in the Number(3) project of the course (if any) Assignment Marks obtained by the Number(3) student for the course (if any) Internal Marks obtained by the student Number(3) for the course (if any) Marks of the semester exam obtained Number(3) by the student for the course Over all grade of the student in the Char(1) course

Foreign key to Student table Foreign key to Course table

ANS: The query for creation of this table is: CREATE TABLE Registration (StudentId varchar2 (4) CONSTRAINT registration_student_fk REFERENCES Student (StudentId), CourseId varchar2 (4) CONSTRAINT registration_course_fk REFERENCES Course (CourseId), DateOfExam date, ProjectMarks number (3), AssignmentMarks number (3), InternalMarks number (3), SemesterMarks number (3), Grade char (1)); vi) Department Table Column name DepartmentId DepartmentName Data type Description Unique Id generated for Each Number(2) Department Varchar2(30) Name of the Department Constraints Primary Key Not Null

Id of Instructor who is managing HeadOfDepartment Varchar2(4) the department ANS: The query for creation of this table is: CREATE TABLE Department

(DepartmentId number (2) CONSTRAINT department_pk PRIMARY KEY, Departmentname varchar2 (30) CONSTRAINT department_departmentname-notnull NOT NULL, HeadOfDepartment varchar2 (4));

vii) Instructor Table Column name InstructorId InstructorName DateOfJoining Data type Description Constraints Primary Key, Should start with I Not Null Foreign key to Department table

Varchar2(4) Unique Id generated for Instructors Varchar2(30) Name of the instructor Date when instructor joins the Date department

Department id of the department DepartmentId Number(2) where Instructor works ANS: The query for creation of this table is: CREATE TABLE Instructor

(InstructorId varchar2 (4) CONSTRAINT instructor_pk PRIMARY KEY CONSTRAINT instructor_iid CHECK (InstructorId like I %), InstructorName varchar2 (30) CONSTRAINT instructor_instructorname_notnull NOT NULL, DateOfJoining date, DepartmentId number (2) CONSTRAINT instructor_fk REFERENCES Department (DepartmentId)); viii) Course Allocation Table Column name AllocationId CourseId InstructorId Data type Description Unique Id generated whenever ever Number(4) a course is allocated to a instructor Course id of the course for which Varchar2(4) allocation is done Instructor Id of the instructor who Varchar2(4) has been allocated the course Start date when a course is taken up by the instructor( A course can be allocated to the instructor in the Date middle of the semester also) Date till which the course is allocated to the instructor( the instructor can be de allocated from the course in the middle of the Date semester) Constraints Primary key Foreign key to Course table Foreign key to Instructor table

StartDate

EndDate

Can't be less than Start Date

ANS: The query for creation of this table is: CREATE TABLE CourseAllocation (AllocationId number (4) CONSTRAINT allocation_pk PRIMARY KEY, CourseId varchar2 (4) CONSTRAINT coursealloction_course_fk REFERENCES Course (CourseId), InstructorId varchar2 (4) CONSTRAINT courseallocation_instructor_fk REFERENCES Instructor (InstructorId), StartDate date, EndDate date CONSTRAINT coursealloction_enddate CHECK (NOT EndDate<StartDate)); ix) Hotel Table Column name HostelId Roomno StudentId HostelFee Datatype Varchar2(10) Number(3) Varchar2(4) Number(6) Description Unique Id Given to each hostel in the college Unique no given to each room in a particular hostel Student id of the student who reside in a particular hostel and in a particular room Monthly fee of the hostel Constraints Composite Primary Key Foreign key to Student Table Greater than 0

ANS: The query for creation of this table is: CREATE TABLE Hostel (HostelId varchar2 (10), Roomno number (3), StudentId varchar2 (4) hotel_fk REFERENCES Student (StudentId), HostelFee number (6) CONSTRAINT hotel_fee CHECK (HostelFee>0), CONSTRAINT hotel_pk PRIMARY KEY (HostelId, Roomno)); x) Attendance Table Colum name Description Student id of the student whos StudentId Varchar2(4) attendance is recorded Course Id of the course in which CourseId Varchar2(4) attendance is recorded TotalLecturedays Number(3) Total no of attendance in the course No of classes the student has attended NoOfDaysPresent Number(3) in the course Datatype Constraints Foreign key to Student table Foreign key to Course table Greater than 0

ANS: The query for creation of this table is: CREATE TABLE Attendance (StudentId varchar2 (4) CONSTRAINT attendance_student_fk REFERENCES Student (StudentId), CourseId varchar2 (4) CONSTRAINT attendance_course_fk REFERENCES Course (CourseId), TotalLecturedays number (3) CONSTRAINT totallecture_days CHECK (TotalLecturedays>0), NoOfDaysPresent number (3));

a) Applicant Data ApplicantId A001 Vadi A002 Sam A003 A004 A005 A006 A007 A008 A009 A010 A011 Suraj Lakshmi Sandra Vivek Vikas Bipin Gopi Sandy gopi@abc.c om sandy@abc. com sandra@abc. com Vivek_kuma r@abc.com vikas@abc.c om Sam_Sundar @abc.com suraj@abc.c om Applicant Name Raj EmailId raj@abc.co m vadi@abc.c om Address 2nd Street,Hebbal 4th Street,Vijayan gar 5th Street,Edappal li 1st Main,Rabindr a Nagar 2nd Main,Allipura m 3rd Corss,RS Puram 5th Main,Coimbe d 2nd Main,Jayalakh mipuram 2nd Cross,Siddart h Nagar 2nd Street,Green Market 9th Cross,Seshadr City Mysore Bangalore Cochin Kolkata Vizag Coimbatore Chennai Bangalore Mysore Delhi Bangalore

ipuram A012 A013 A014 A015 A016 Lilly Rose Megha Henry Joel lilly@abc.co 9th m Main,Banjara Hills 11th Cross,Kadava nthara megha@abc 10th .com Lane,Vashi henry@abc. 2nd street,Silk com Board jeol@abc.co 4th m street,Vijayan agar Hyderabad Cochin Mumbai Bangalore Mysore

ANS: INSERT INTO Applicant (ApplicantId, ApplicantName, EmailId, Address, City) VALUES ('A001','Raj','raj@abc.com','2nd Street,Hebbal','Mysore'), ('A002','Vadi','vadi@abc.com','4th Street,Vijaynagar','Bangalore'), ('A003','Sam','sam_sundar@abc.com','5th Street,Edappalli','Cochin'), ('A004','Suraj','suraj@abc.com','1st Main,Rabindra nagar','Kolkata'), ('A005','Lakshmi','2nd main,Allipuram','Vizag'), ('A006','Sandra','Sandra@abc.com''3rd Cross,RS Puram','Coimbatore'), ('A007','Vivek','vivek_kumar@abc.com''5th main,Coimbed','Chennai'), ('A008','Vikas','vikas@abc.com''2nd Main,Jayalakhmipuram','Bangalore'), ('A009','bipin','2nd Cross,Siddarth nagar',city='Mysore'), ('A010','Gopi','gopi@abc.com''2nd Street Green Market','delhi'), ('A011','Sandy','sandy@abc.com''9th Cross,Seshadripuram','Bangalore'), ('A012','Lilly','lilly@abc.com''9th Main,Banjara Hills','Hyderabad'), ('A013','Rose','11th Cross,kadavanthara','Cochin'), ('A014','Megha','megha@abc.com''10th Lane,Vashi','Mumbai'),

('A015',='Henry',='henry@abc.com''2nd Street,Silk Road','Bangalore'), ('A016','Joel','jeol@abc.com''4th Street,Vijayanagar','Mysore'); b) Branch Data BranchId B1 B2 B3 B4 B5 B6 BranchName Information Science Computer Science Electronics Electrical Mechanical Civil

ANS: INSERT INTO Branch (BranchId,Branchname) VALUES ('B1','Information Science'), ('B2','computer Science'), ('B3','Electronics'), ('B4','Electrical'), ('B5','Mechanical'), ('B6','Civil');

c) Course Data Assignment Marks Semester Internal Exam Marks Marks 10 10 60 10 10 10 10 10 10 10 10 60 60 60 60

Course Id C001 C002 C003 C004 C005

Course Name Programming Fundamentals Data Structures Basics of RDBMS System Software Computer Hardware

Branch Semester Id Elective 1 B1 N 2 B1 2 B1 3 B1 3 B2 N N N N

Project Marks 20 20 20 20 20

C006 C007 C008 C010 C009

File Structures Network Computing Data Warehousing Analysis of Algorithms Neural Networks ANS: INSERT INTO

4 B1 4 B3 5 B1 5 B1 6 B2

N N Y N Y

60 20 20 20

10 10 10 10 10

10 10 10 10 10

20 60 60 60 80

Course VALUES (&CourseId,&CourseName, &Semester,&BranchId,&Elective, &ProjectMarks, &AssignmentMarks, &InternalMarks, &SemesterExamMarks); d) Student Data Current Semester

StudentId ApplicantId UserId Password A001 2 raj Tiger S001 A003 3 sam Tiger S002 A004 5 suraj Tiger S003 A006 3 sandra Tiger S004 S005 A007 4 vivek Tiger S006 A008 7 vikas Tiger S007 A009 8 bipin Tiger S008 A010 3 gopi Tiger S009 A012 1 lilly Tiger S010 A013 4 rose Tiger S011 A014 7 megha Tiger S012 A015 6 henry Tiger S013 A016 2 jeol Tiger ANS: INSERT INTO Student VALUES (&StudentId,&ApplicantId, &CurrentSemester,&UserId,&Password); e) Registration Data

StudentI d S001 S002 S003

CourseI d C001 C001 C002

Project DateOf Marks Exam 5-Jun-08 24-Aug-08 15-Mar-08

Semester Assignment Internal Exam Marks Marks Marks 20 5 5 50 15 10 8 32 20 9 9 55

Grade A B A

S004 S005 S006 S007 S010 S009 S011 S012 S013

C003 C004 C005 C006 C004 C003 C007 C008 C006

13-Aug-08 8-Sep-08 22-Nov-08 6-Feb-08 8-Sep-08 29-Dec-08 13-Sep-08 16-Jan-08 6-Feb-08

20 15 10 45 20 18 20 35 47

10 10 5 5 10 8 7 5 8

10 10 5 5 10 10 8 5 7

60 50 30 15 40 46 55 20 18

A A D B A A A B A

ANS: INSERT INTO Registration VALUES (&StudentId,&CourseId, &DateOfExam, &ProjectMarks, &AssignmentMarks, &InternalMarks, &SemesterExamMarks,&Grade); f) Department Data DepartmentId 10 20 30 40 50 60 DepartmentName Information Science Computer Science Electronics Electrical Mechanical Civil HeadOfDepartment I105 I102 I104 I107 I109 I106

ANS: INSERT INTO department VALUES (&DepartmentId, &DepartmentName,&HeadOfDepartment);

g) Instructor Table InstructorId I101 I102 I103 I104 I105 I106 I107 I108 I109 InstructorName Bob Hockins Suguru Zikovich Ritivoi David Field Emillie Norton Ron Hardman Scott Urman Daisy Samson Ford Bravo DateOfJoining DepartmentId 12-Jan-00 10 21-Feb-01 20 13-Jan-03 10 30-Mar-99 30 4-Jun-98 30 1-Jan-05 60 8-Apr-08 40 9-Dec-04 50 10-May-08 50

I110 I111 I112 I113 I114 I115 I116

Rebecca Brown Antario Samuel Justin Nissar Avinash Naren Micheal Rose

2-Jan-09 23-Jun-04 25-Jan-02 14-Apr-03 16-Mar-09 27-May-07 5-Jan-08

10 10 60 40 10 20 40

ANS: INSERT INTO instructor VALUES (&InstructorId,&InstructorName, &DateOfJoining, &DepartmentId); h) Course Allocation Table

AllocationId 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013

CourseId C001 C002 C001 C001 C003 C004 C003 C002 C005 C006 C007 C005 C008

InstructorId I101 I102 I103 I101 I104 I103 I104 I107 I105 I106 I108 I109 I110

StartDate EndDate 1-Jun-08 4-Jun-08 7-Mar-08 14-Mar-08 20-Aug-08 23-Aug-08 1-Sep-08 4-Sep-08 10-Aug-08 12-Aug-08 4-Sep-08 7-Sep-08 26-Aug-08 28-Aug-08 21-Jul-08 27-Jul-08 12-Nov-08 21-Nov-08 1-Feb-08 5-Feb-08 7-Sep-08 12-Sep-08 12-Nov-08 21-Nov-08 15-Jan-08 15-Jan-08

ANS: INSERT INTO CoursAllocation VALUES (&AllocationId,&CourseId,&InstructorId, &StartDate, &EndDate); i) Attendance Data StudentId S001 S002 CourseId C001 C001 TotalLectureDays 4 4 NoOfDaysPresent 3 4

S003 S004 S005 S006 S007 S010 S009 S011 S012

C002 C003 C004 C005 C006 C004 C003 C007 C008

8 3 4 10 5 4 3 6 1

5 3 2 9 5 4 1 5 1

ANS: INSERT INTO Attendance VALUES (&StudentId,&CourseId, &TotalLectureDays, &NoOfDaysPresent); j) Hotel Data HostelId Violet Violet Violet Indigo Indigo Indigo Blue Blue Blue RoomNo 101 201 301 100 200 300 102 202 302 StudentId S001 S003 S004 S002 S005 S006 S007 S008 S009 HostelFee 3000 4000 5000 3000 4000 5000 3000 4000 5000

ANS: INSERT INTO Hostel VALUES (&HostelId,&Roomno,&StudentId,&FostelFee);

The format of inserting the values is same for all the cases as explained in part a and part b.

ASSIGNMENT 6: SQL QUERIES

1) List the name of different cities from where applicants belong to ANS: SELECT ApplicantName,City FROM Applicant 2) List the different semesters in which students have enrolled ANS: SELECT Semester FROM Student 3) List the distinct InstructorId and CourseId from CourseAllocation table. ANS: SELECT DISTINCT InstructorId, CourseId FROM CourseAllocation 4) List the name of course which are not electives ANS: SELECT CourseName FROM Course WHERE Elective=N. 5) List the name of instructor who has joined before 01-jan-2002 ANS: SELECT InstructorName FROM Instructor WHERE DateOfJoining < System date 6) List Id of courses whose ending date is before the current system date. ANS: SELECT CourseId FROM CourseAllocation WHERE EndDate < System date 7) List the name of applicant from Mysore and Bangalore. ANS: SELECT ApplicantName FROM Applicant WHERE City IN(Mysore,Bangalore); 8) List the name of applicants who do not belong to Mysore. ANS: SELECT ApplicantName FROM Applicant WHERE City NOT(Mysore); 9) List the name of semester 1 and semester 2 courses which belongs to branch B1. ANS: SELECT CourseName FROM Course WHERE Semester IN (1,2) AND BranchId = B1;

10) List the name of courses which are elective and project marks is equal to 20. ANS: SELECT CourseName FROM Course WHERE Elective =Y AND ProjectMarks = 20;

11) List the name of the course in which project marks is greater than the semester marks. ANS: SELECT CourseName FROM Course WHERE ProjectMarks > SemesterMarks; 12) List the details of all the applicants who belong to Bangalore, Mysore or Hyderabad. ANS: SELECT * FROM Applicant WHERE City IN(bangalore,mysore,hyderabad); 13) List the details of courses which are taught in semester 1, 2 or 3. ANS: SELECT * FROM Course WHERE Semester IN(1,2,3); 14) List the studentId who have secured marks between 80 to 100. ANS: SELECT StudentId FROM Student WHERE SemesterExam BETWEEN 80 AND 100; 15) List the student details in ascending order of their applicantId. ANS: SELECT * FROM Student ORDER BY ApplicantId; 16) List the details of instructor on ascending order of their date of joining. ANS: SELECT * FROM Instructor ORDER BY DateOf Joining; 17) List the details of instructor on descending order of department no and ascending order of joining date. ANS:SELECT * FROM Instructor ORDER BY DepartmentName DESC,ORDER BY DateOf Joining; 18) List the name of instructors starting with R and ending with n ANS: SELECT InstructorName FROM Instructor WHERE InstructorName (LIKE r%n);

19) List the details of instructors whose date of joining is in the month of March. ANS: SELECT * FROM Instructor WHERE DateOf Joining BETWEEN 1-March AND 31-March; 20) List the details of applicant whose email id contains a character _

ANS: SELECT * FROM Applicant WHERE EmailId (LIKE %_%); 21) List the applicant who do not have email id in their applicant detail information. ANS: SELECT EmailId FROM Applicant WHERE EmailId IS NULL; 22) List the course Id which has project Marks in its course detail information. ANS: SELECT CourseId FROM Course WHERE ProjectMarks(CourseAllocation);

ASSIGNMENT 7: SQL QUERIES QUES: To understand a simple query

Let us try to understand how the following query works: List employees not belonging to department 30, 40, or 10 Assume that the Emp, Dept tables contains the following description: Emp (EmpId, EmpName, sal, job, hiredate, deptno) Dept (Deptno, DeptName)

ANS: SELECT ename FROM emp WHERE deptno NOT IN (30, 40, 10); Working of Query 1) The query filters those tuples which do not belong to deptno 30, 40 or 50, because of the operator NOT IN, from the emp table. 2) Finally it displays only the employee names of the filtered tuples because we specify only ename in the SELECT clause.

ASSIGNMENT 8: SQL QUERIES QUES: Let us try to understand how the following query works:

List the total salary, maximum and minimum salary and average salary of the employees job wise, for department 20 and display only those rows having an average salary > 1000. Assume that the Emp table contains the following description: Emp (EmpId, EmpName, sal, job, hiredate, deptno) ANS: SELECT job, avg (sal) FROM emp WHERE deptno=20 GROUP BY job HAVING avg (sal) > 1000 ORDER BY job; Working of Query 1) First thing first we have to select (filter) data only for dept 20. For this we have to put WHERE deptno=20 2) Now, we have to find average salary of the employees job wise. Therefore we have to group our selected (filtered) data using group by clause. GROUP BY job

3) Now, we have to display only those rows having an average salary > 1000. Therefore we have to put HAVING avg (sal) > 1000 4) For getting the final output in ascending order of job we have to put order by clause. ORDER BY job

ASSIGNMENT 9: SQL QUERIES Perform the following queries:

1) List the Branch Id and the number of courses offered by each Branch ANS: SELECT BranchId, COUNT (NoOfCourses) FROM Branch Data GROUP BY BranchId; 2) List the number of courses which do not have Project Marks ANS: SELECT COUNT (no of courses) FROM Course Data WHERE project marks IS NULL; 3) List the Branch Id and the number of electives offered in each branch ANS: SELECT BranchId, COUNT (no of electives) FROM Branch Data GROUP BY BranchId; 4) List the Course Id, average total marks obtained in each Course. Hint: Total marks= project marks + assignment marks + internal marks + semester marks ANS: SELECT CourseId, AVG (total marks) FROM Course GROUP BY CourseId; 5) List the number of courses for which exam was conducted between 1-Jan-2008 and 30-May-2008 ANS: SELECT COUNT (no of courses) FROM tablename WHERE ExamDate BETWEEN 1-Jan-2008 AND 30-May-2008 6) List the Instructor Id, number of different Courses offered by an Instructor ANS: SELECT InstructorId, COUNT (Courses) FROM tablename GROUP BY InstructorId; 7) List the Course Id which is offered by more than one instructor ANS: SELECT CourseId, COUNT (Courses) FROM tablename GROUP BY CourseId HAVING COUNT (Instructor)>1; 8) List the Instructor Ids who have finished handling at least two courses during the year 2008 ANS: SELECT InstuctorId FROM Instuctor WHERE COUNT (Courses) >=2; 9) List the total strength of students staying in each Hostel

ANS: SELECT Hostel, COUNT (Strength) FROM tablename GROUP BY Hostel; 10) List the total marks in each Semester of a Branch Hint: total marks= project marks + assignment marks + internal marks + semester marks ANS: SELECT Semester, SUM (total marks) FROM tablename GROUPBY Semester; 11) List the Student Id, Student Name if the student has an Email Id ANS: SELECT StudentId, StudentName FROM table GROUP BY StudentId, and StudentName HAVING EmailId IS NOT NULL; 12) List the names of students currently studying in 5th semester ANS: SELECT StudentName FROM tablenameWHERE Semester=5; 13) List the names of students along with the course names for which they have registered ANS: SELECT StudentName, CourseName FROM tablename GROUP BY StudentName; 14) List the name of the course and the number of A grades obtained in that course ANS: SELECT CourseName, COUNT (Grade) FROM Tablename GROUP BY CourseName HAVING Grade=A; 15) List the name of department along with the instructor name who is heading that department if he has minimum two years of experience in the College. ANS: SELECT DepartmentName, InstructorName FROM tablename GROUP BY DepartmentName HAVING Experience>2; 16) List the name of course, name of instructor, start date, end date if that course is allotted to the instructor ANS: SELECT CourseName, InstructorName, StartDate, EndDate FROM table GROUP BY CourseName; 17) List the student id and hostel id if they are staying in the hostel. Also display the student ids who are not staying in the hostel

ANS: SELECT StudentId, HostelId FROM tablename WHERE regd IS NOT NULL UNION SELECT StudentId, HostelId FROM tablename WHERE regd IS NULL; 18) List the names of applicants and their User Id and Password. Display NULL for User Id and Password if they dont get admission ANS: SELECT Name, UserId, Password FROM tablename WHERE Admission IS NOT NULL UNION UPDATE Tablename SET UserId=NULL, Password=NULL WHERE Admission IS NULL; 19) List the instructor name, branch name, number of courses taught by the instructor in that branch ANS: SELECT InstructorName, BranchName, COUNT (Courses) FROM table GROUP BY InstructorName, BranchName; 20) List the student name and course name in which they have scored D grade ANS: SELECT StudentName, CourseName FROM table GROUP BY StudentName HAVING Grade=D; 21) List the course names which have duration less than or equal to 4 days ANS: .SELECT CourseName FROM table WHERE Duration<=4; 22) List the name of the instructor and the course names they are teaching on 27-Aug2008 ANS: SELECT InstructorName, CourseName FROM tablename GROUP BY InstructorName, CourseName HAVING date=27-aug-2008; 23) List the names of students who are not staying in Hostel ANS: SELECT StudentName FROM tablename WHERE Hostel IS NOT NULL; 24) List the student name, course name and the number of days present for each course conducted ANS: SELECT StudentName, CourseName, COUNT (NoOfDays) FROM tablename GROUP BY StudentName, CourseName;

25) List the names of courses handled by the instructors who are also head of departments ANS: SELECT CourseName, InstructorName FROM tablename WHERE Instructor=HeadOfDepartment; 26) List the names of applicants who got admission into Computer Science branch ANS: SELECT ApplicantName, CSEBranch FROM tablename GROUP BY CSEBranch; 27) List the names of instructors who are handling elective courses ANS: SELECT InstructorName FROM Tablename WHERE Courses =Electives;

ASSIGNMENT 10: SQL QUERIES QUES: Consider the following Suppliers table

Suppliers Table SNo S1 S2 S3 S4 S5 SName Smith Jones Blake Clark Adams Status 20 10 30 20 30 City London Paris Paris London Athens

Let us consider the following query: Get supplier numbers for suppliers with a status lower than that of supplier S1

ANS: SELECT SNo FROM Suppliers WHERE Status < (SELECT Status FROM Suppliers WHERE SNo=S1); Working of Query 1) The inner query is executed first which selects the status of supplier S1 from suppliers table. 2) Now the query looks like this: SELECT SNo FROM Suppliers WHERE Status < 20 3) Above query is again executed as a simple query i.e. tuples are selected for those suppliers whose status is less than 20 and finally there SNo is being displayed.

ASSIGNMENT 11: SQL QUERIES QUES: Assume that User_A has created two tables: EMPLOYEE and DEPARTMENT. He/She is granting/ revoking the privileges to other users on these two tables. Different data control mechanisms have been addressed. 1) User_A grants Select, Insert and Update privileges to User_B on EMPLOYEE table with grant option. GRANT SELECT, INSERT, UPDATE ON Employee TO User_B WITH GRANT OPTION; 2) User_A grants Select privilege to User_C on DEPARTMENT table GRANT SELECT ON Department TO User_C WITH GRANT OPTION; 3) User_B grants Select, Update privileges to User_D on EMPLOYEE table GRANT SELECT, UPDATE ON Employee TO User_D; 4) User_C grants Select privilege to User_E on DEPARTMENT table GRANT SELECT ON Department TO User_E; Check the following:
1) Whether User_B able to Update the DEPARTMENT table

ANS: No, User_B not able to UPDATE the Department Table, because User_B has no GRANT Permission to update the Department Table

2) Whether User_D able to Update the EMPLOYEE table ANS: Yes, User_D is able to UPDATE the Employee Table, because User_B has GRANT Permission of UPDATE to User_D and User_B also has GRANT permission from User_A 3) Whether User_C able to Select the data from DEPARTMENT table ANS: Yes, User_C able to SELECT the data from DEPARTMENT table, because User_C has GRANT permission from User_A

4) Whether User_E able to Select the data from EMPLOYEE table ANS: No, User_E cant able to SELECT the data from EMPLOYEE table 5) Whether User_D able to grant Select, Update privileges to any User ANS: No, User_D cant able to grant Select, Update privileges to any User 6) Whether User_D able to Select data from EMPLOYEE table after privileges are revoked by User_A as below: REVOKE SELECT, INSERT, UPDATE ON Employee FROM User_B ANS: NO, User_D cant able to SELECT data from EMPLOYEE

ASSIGNMENT 12: SQL QUERIES QUES: Perform the following steps and understand the possibilities and limitations in updating views. 1) Create EMPLOYEE table with the following description. EMPLOYEE EmpId EmpName Number(4) Varchar2(15 ) Salary Number(7,2) DateOfJoin Date Primary Key Cannot be NULL

2) Insert the below data into EMPLOYEE table EmpI d 1001 1002 1003 EmpName Salary DateOfJoin Suresh Kumar Vijay 50000 40000 70000 01-Jun-02 25-Mar-03 15-Aug-01

3) Create a View EMPLOYEE_VIEW as below: CREATE VIEW EMPLOYEE_VIEW AS SELECT * FROM EMPLOYEE 4) Execute the below query and see the data in view: SELECT * FROM EMPLOYEE_VIEW

Check the following: 1) Whether data insertion is possible into the view without a column having PRIMARY KEY constraint INSERT INTO EMPLOYEE_VIEW (EmpName, Salary, DateOfJoin) VALUES ('Kiran', 2500,'01-Jan-2002'); ANS: Yes, we can insert the data into the view having PRIMARY KEY 2) Whether data insertion is possible into the view without a column having NOT NULL constraint INSERT INTO EMPLOYEE_VIEW (EmpId, Salary, DateOfJoin) VALUES (1004, 60000,'01-Jan-2004'); ANS: Yes, we can insert into the view without a column having NOT NULL constraint 3) Whether data insertion is possible into the view without a column having any constraint on it. INSERT INTO EMPLOYEE_VIEW (EmpId, EmpName, DateOfJoin) VALUES (1004,Arun,'01-Jan-2004'); ANS: Yes, data insertion is possible into the view without a column having any constraint on it. 4) Drop table EMPLOYEE and select the data from view. DROP TABLE EMPLOYEE; SELECT * FROM EMPLOYEE_VIEW; ANS: Nothing is displayed because if Table is DROP then view is also dropped

5) Recreate the table EMPLOYEE with different names for all columns of it, insert few rows and select data from the view SELECT * FROM EMPLOYEE_VIEW; ANS: 1) Create Table: CREATE TABLE EMPLOYEE (Emp_contactnum number (10), Emp_DOB date) 2) Insertion INSERT INTO EMPLOYEE VALUES (&Emp_contactnum, &Emp_DOB); 3) View CREATE VIEW EMPLOYEE_VIEW AS SELECT * FROM EMPLOYEE; INSERT INTO EMPLOYEE_VIEW VALUES (9465012362, 06-April-90); SELECT * FROM EMPLOYEE_VIEW; Emp_Contactnum 9465012362 Emp_DOB 06-April-90

6) Drop table EMPLOYEE, Recreate the it with same old names for all columns, insert few rows and select data from the view SELECT * FROM EMPLOYEE_VIEW; ANS: CREATE TABLE EMPLOYEE (Empid number (4) CONSTRAINT Employee_pk PRIMARY KEY, EmpName varchar2 (15) CONSTRAINT EmployeeName_notnull NOT NULL, Salary number (7, 2) DateOfJoin date); INSERT INTO EMPLOYEE VALUES (&Empid, &EmpName, & Salary, &DateOfJoin);

You might also like