Final DBMS Lab Manual by Savitha
Final DBMS Lab Manual by Savitha
KARNATKA
LABORATORY MANUAL
ON
By
Smt. SAVITHA R ME
Head of Department of Information Science and Engineering
GOVERNMENT POLYTECHNIC-109
Kalaburagi
CONTENT
SL.NO EXPERIMENTS PAGE NO
A. Install and setup DBMS software (MYSQL and Oracle 21c Express
1
1 Edition)
B. Learn the interface and explore the features of installed DBMS 3
Identify ER Model elements and Draw ER diagram for the given
2 5
specification using tools (MYSQL)
3 Map ER Model to relational model. 10
Experiment 1A: Install and setup DBMS software (MYSQL and Oracle
21c Express Edition)
1. Phypmyadmin
Pypmyadmin is a free and open source administration tool for MYSQL and MariaDB. As a
portable web application written primarily in PHP, it has become one of the most popular
MYSQL administration tools, especially for web hosting services.
XAMPP contains one of the most popular Relational data base management systems in the world
in combination with the web server apache and scripting languages PHP/Perl.
XAMPP is the title used for a compilation of free software. The name is an acronym of
Step 4: Deactivate UAC (User Account Control) during installation process( because it affects
installation of XAMPP)
Download the Oracle Database 21c Express Edition installer. It can be found
at https://www.oracle.com/database/technologies/oracle21c-windows-downloads.html you will
need to select the Windows download and accept the license agreement before the download will
commence.
Experiment 1b: Learn the interface and explore the features of installed
DBMS
Controls for the individual components of your test server can be reached through the XAMPP
control panel. The clear user interface logs are all actions and allow you to start and stop
individual module with a single window.
localhost/phpmyadmin/
Open the XAMPP control panel and start the Apache and MYSQL server.
Open Command Prompt and navigate to the bin location of MySQL Server.
MySQL Server x.0\bin contains mysql.exe. The executable can accept username and the mention
of password as optional arguments.
Run the following command, in the command prompt with ‘root’ as default administrator
username
Now, you have to enter the password. The password is not echoed back to the prompt, but stars.
This is for security and protecting your password from social engineering.
Press enter key after typing your password. In this case password is null so just press enter key
If your login and password do not match with the users in the MySQL server, following message
is displayed in the Command Prompt.
1. Library consists of many branches and each branch has many borrower which maintains borrower
ID,branch_id, Borrower_name, address, phone etc
2. Publishers publish books. And many copy of the book is maintained in each library branch.
library maintains the no_of_copies available of each book
3. Borrower borrows books from the library branch which records date_borrowed and due_date to
return the book for each borrower.
4. Publisher details such as publisher_id, publisher_name, address and phone are stored
5. Book details such as book_id, Title, author , publisher and price are maintained
6. Library brach details such as branch_id, name and address are maintained.
Step 1: The first step is to identify the entity sets. These are things that have more than one
instance of being existent. The entity sets for a library management system are as follows:
Library_branch
Book
Publisher
Borrower
Step 2: The second step is to associate attributes to the entity sets. This is important because
entity sets are recurring, such as several books, members, etc. The attributes are needed to
differentiate between entities. The respective attributes are as follows:
Step 3: The third step is to find the key attributes of each entity. The key attribute is unique to
every single entity component. The key attributes are as follows:
Library_Branch – Branch_ID
Book – Book_ID
Publisher –Publisher_ID
Borrower – Library_card_no
Step 4: The fourth step is to identify the relationship between the different entities present. This
is necessary to analyze the database clearly and make a better library management system.
A single member can borrow multiple books if they want. And at the same time same copy of the
book is borrowed by multiple members. This kind of relationship is known as a many-to-many
relationship.
In this relation, a single library branch can have multiple books present over there. And the same
time a single copy of the book is maintained over multiple branch Making this many-to-many
relationship as well.
The ONE LIBRARY BRANCH HAS MANY BORROWERS. There is only one BRANCH and
multiple BORROWES; thus, this is a one-to-many relationship.
A single publisher can publish several books and supply them to the library. This is another one-
to-many relationship.
Step 5: The fifth step is to compile all these steps. Join the entities that go together and form an
ER diagram.
Step 2: Select the database for which you want to generate ER diagram (Create a Library
database with tables using GUI)
Step 3: Select the diagram from last menu item of more section
Step 4: You will land on page with different options to create ER diagram
3.1. Rule 1: Convert each entities in to a Relation with its simple attributes as
relation’s fields
Library ER Diagram consists of 4 entities. Hence each entity can be converted into a relation as
follows
If you have a multi-valued attribute, take the attribute and turn it into a new entity or table of its
own. Then make a 1:N relationship between the new entity and the existing one. In simple
words. 1. Create a table for the attribute. 2. Add the primary (id) column of the parent entity as a
foreign key within the new table.
We can ignore derived attributes because it can be derived from stored attributes
1:1 Relationship
In 1:1 relationship 2 tables must be created and the relationship attributes are added
either in the left side entity/table of the relationship or in the right side entity/table.
In the above ER diagram we donot have any 1:1 relationship.
1:N Relationship
In 1:N relationship 2 tables must be created and the relationship attributes (if any)
are added in to the N side entity of the relationship along with the primary key of
the 1 side entity.
In the above 1:N relation N side entity is book hence primary key of publisher that is
publisher_id must be included in book relation/table. The final book relation becomes
In the above 1:N relation many side entity is borrower hence primary key of library_branch that
is library_branch_id must be included in borrowr relation/table, so the final borrower relation
becomes
M:M Relationship
For M:N relationship 3 tables must be created (entity1 to table 1, entity 2 to table 2 and relation1
as table 3) as follows
3. Borrow the third table consists of the primary key of table 1 and 2 along with relationship
attributes date_of_issue and due_date. Hence final Borrow relation will become as follows
3. Copies the third table consists of the primary key of table 1 and 2 along with relationship
attribute no_of_copies. Hence final Copies relation will become as follows
The relational schema for the above ER Diagram would be as shown below:
Following are some of the most commonly used constraints available in SQL.
NOT NULL Constraint − Ensures that a column cannot have NULL value.
DEFAULT Constraint − Provides a default value for a column when none is specified.
UNIQUE Constraint − Ensures that all values in a column are different. A table can have many
numbers of unique keys
PRIMARY Key − uniquely identifies each row/record in a database table.A table can have only
one primary key
CHECK Constraint − The CHECK constraint ensures that all the values in a column satisfies
certain conditions.
INDEX − Used to create and retrieve data from the database very quickly.
Constarint Attributes
Primary Key 1.Book(Book_id)
2.Library_branch(Branch _id)
3.Publisher(publisher_id)
4.Borrower(library_card_no)
Not Null Amost all the attributes
Default Address may have default value ‘Kalaburgi’
Unique Publisher name in publisher may be unique since we donot
want the names to be repeated
Reference key 1. Book_id in all other tables except book table
Usually primary key is used to identify the attribute values hence the library database has
following functional dependencies in relation 1,2,3 and 4. Whereas Relation 5 and 6 does not
have primary key but the combination of 2 reference keys can be used to identify each attribute
value uniquely.
4.2 Normalization
Normalization is a set of rule we should follow when designing the database to reduce the
redundancy (duplication) and to improve data integrity (correctness)
First Normal Form: A given relation is called in First Normal Form (1NF) if each cell of the
table contains only an atomic value.
Example: Consider the below table of order(sales), here a single order can contain multiple
itemsIDs, which is a multivalued attribute.
To remove the multivalued attribute we can create the above table as follows
But then the data of the table may contain more null values as shown below.
In order to remove storage of null values (to utilize memory) we have to decompose this table to
satisfy first normal form as shown below
Second Normal Form: A given relation is called in Second Normal Form (2NF) if and only if-
Example: Consider the below table order (sale) where we have included firstname, lastname and
address of the customer.
OrderID{CustomerID, orderDate,OrderTotal}
CustomerID{ Firstname,Lastname,Address}
But according to 2NF non-key attributes (columns) must depends on the primary key only.
Hence to satisfy the 2NF we have to decompose this table as shown below
Now the functional dependencies of each relation (table) will become as follows
Relation1: Customer
FD: Customer {Firstname, Lastname, Address}
Relation2: Order
FD: OrderId {CustomerID, OrderDate, OrderTotal}
Relation3: OrderItems
FD: OrderItemId {OrderID, ItemID, Quantity}
Now in all the three relations, all the non-key attributes depends on the primary key only. This
makes the relations to satisfy 2NF.
Third Normal Form: A given relation is called in Third Normal Form (3NF) if and only if-
Now these relations eliminate transitive dependencies and hence satisfy 3NF.
The final relations for this Shopping database would be as flows which satisfies 1NF, 2NF and
3NF.
According to 1 NF each attribute must have atomic values. We are not having any multivalued
attributes here hence all the relation satisfies 1NF
According to 2 NF there should not be any partial dependencies on primary key. There is no such
partial dependencies in the above relations hence all the relation satisfies 2NF
According to 3NF there should not be any transitive dependencies. There is no transitive
dependencies in the above relations hence all the relation satisfies 3NF.
Since each relation is having only one functional dependency and all the attributes are dependent
on the primary key only we can clearly say each relation satisfies 1NF, 2NF AND 3NF.
In the Library Database the following data types and Constraints are identified validated.
CREATE: Create command is used to create DB objects or structures such as database, tables,
views, user, function, procedure, packages and triggers
1. CREATING TABLES FOR LIBRARY DATABASE
TABLE 1
CREATE TABLE PUBLISHER(
PUBLISHER_ID VARCHAR(10)PRIMARY KEY,
PUBLISHER_NAME VARCHAR(30) UNIQUE,
ADDRESS VARCHAR(30) DEFAULT ‘KALABURAGI’,
PHONE BIGINT(10));
TABLE 1
CREATE TABLE BOOK(
BOOK_ID VARCHAR(10)PRIMARY KEY,
TITLE VARCHAR(20) NOT NULL,
AUTHOR VARCHAR(20) NOT NULL,
PUBLISHER VARCHAR(10)REFERENCES PUBLISHER(ID) ,
PRICE BIGINT(10) NOT NULL);
TABLE 3
CREATE TABLE LIBRARY_BRANCH(
BRANCH_ID VARCHAR(10)PRIMARY KEY,
BRANCH_NAME VARCHAR(20) NOT NULL,
ADDRESS VARCHAR(30)NOT NULL,
PHONE BIGINT(10));
TABLE 4
CREATE TABLE BOOK_COPIES(
TABLE 5
CREATE TABLE BORROWER(
LIBRARY_CARD_NO VARCHAR(10)PRIMARY KEY,
BRANCH_ID VARCHAR(10) REFERENCES library_branch(BRANCH_ID),
NAME VARCHAR(20) NOT NULL,
ADDRESS VARCHAR(30) DEFAULT ‘KALABURAGI’,
PHONE BIGINT(10));
TABLE 6
CREATE TABLE BOOK_BORROWED(
BOOK_ID VARCHAR(10)REFERENCES book(BOOK_ID),
LIBRARY_CARD_NO VARCHAR(10)REFERENCES borrower(LIBRARY_CARD_NO),
DATE_BORROWED DATE,
DUE_DATE DATE);
LIBRARY ER DIAGRAM
TABLE 1
CREATE TABLE STUDENT (
NAME VARCHAR(20),
STUDENT_NUMBER VARCHAR(10)PRIMARY KEY,
CLASS VARCHAR(10),
MAJOR VARCHAR(10));
TABLE 2
CREATE TABLE COURSE(
COURSE_NAME VARCHAR(20),
COURSE_NUMBER VARCHAR(10)PRIMARY KEY,
CREDIT_HOURS INTEGER(2),
DEPARTMENT VARCHAR(10));
TABLE 3
CREATE TABLE SECTION(
SECTION_ID VARCHAR(10)PRIMARY KEY,
COURSE_NUMBER VARCHAR(10)REFERENCES COURSE(COURSE_NUMBER),
SEMESTER INTEGER(1),
ACADEMIC_YEAR YEAR(4),
INSTRUCTOR VARCHAR(20));
TABLE 4
CREATE TABLE GRADE_REPORT(
STUDENT_NUMBER VARCHAR(10)REFERENCES STUDENT(STUDENT_NUMBER),
SECTION_ID VARCHAR(10)REFERENCES SECTION(SECTION_ID),
GRADE VARCHAR(1));
TABLE 5
CREATE TABLE PREREQUISITE(
COURSE_NO VARCHAR(10) REFERENCES COURSE(COURSE_NUMBER),
PREREQUISITE_NUMBER VARCHAR(20) REFERENCES COURSE(COURSE_NUMBER));
GRADEBOOK ER DIAGRAM
TABLE 1
CREATE TABLE DEPARTMENT(
DNAME VARCHAR(20),
DNUMBER VARCHAR(10)PRIMARY KEY,
MGR_START_DATE DATE);
TABLE 2
CREATE TABLE DEPT_LOCATION(
DNUMBER VARCHAR(10)REFERENCES DEPARTMENT(DNUMBER),
DLOCATION VARCHAR(20));
TABLE 3
CREATE TABLE EMPLOYEE(
FNAME VARCHAR(20),
MINIT VARCHAR(20),
LNAME VARCHAR(20),
SSN VARCHAR(10)PRIMARY KEY,
BDATE DATE,
ADDRESS VARCHAR(20),
SEX VARCHAR(6),
SALARY INTEGER(10),
SUPER_SSN VARCHAR(10)REFERENCES EMPLOYEE(SSN),
DNO VARCHAR(10)REFERENCES DEPARTMENT(DNUMBER));
TABLE 4
TABLE 5
CREATE TABLE WORKS_ON(
ESSN VARCHAR(10)REFERENCES EMPLOYEE(SSN),
PNO VARCHAR(10)REFERENCES PROJECT(PNUMBER),
HOURS INTEGER(2));
TABLE 6
CREATE TABLE DEPENDENT(
ESSN VARCHAR(10)REFERENCES EMPLOYEE(SSN),
DEPENDENT_NAME VARCHAR(20),
SEX VARCHAR(6),
BDATE DATE,
RELATIONSHIP VARCHAR(10));
COMPANY ER DIAGRAM
Table altered.
ADDRESS VARCHAR2(20)
Table altered.
Table altered.
Table created.
Table altered.
Table altered.
Table dropped.
7.1 INSERT: Insert is used for inserting record values (instances) into a table
Table created.
Table created.
1 row created.
1 row created.
1 row created.
SQL> /
Enter value for eno: e2
Enter value for dno: 1
Enter value for ename: jhon
Enter value for sal: 15000
old 1: insert into emp values('&eno',&dno,'&ename',&sal)
new 1: insert into emp values('e2',1,'jhon',15000)
1 row created.
SQL> /
Enter value for eno: e3
Enter value for dno: 1
Enter value for ename: santhosh
Enter value for sal: 20000
old 1: insert into emp values('&eno',&dno,'&ename',&sal)
new 1: insert into emp values('e3',1,'santhosh',20000)
1 row created.
SQL> /
1 row created.
SQL> /
Enter value for eno: e5
Enter value for dno: 2
Enter value for ename: fayaz khan
Enter value for sal: 30000
old 1: insert into emp values('&eno',&dno,'&ename',&sal)
new 1: insert into emp values('e5',2,'fayaz khan',30000)
1 row created.
SQL> commit;
Commit complete.
7.2 UPDATE: Update is used to modify the existing record (instances) value in a table
6 rows selected.
1 row updated.
6 rows selected.
6 rows updated.
7.3 DELETE: delete is used to delete the existing record (instances) from the table
7.3.1 Below query deletes the records of employee whose salary is 10000
1 row deleted.
7.3.1 Below query deletes the records of employee whose eno is ‘e5’
1 row deleted.
4 rows deleted.
no rows selected
7.4 SELECT : select is used to retrieve the records (instances) from the database tables
7.4.1 Below query retrieves all the attributes and instances from the table ‘emp’( * is used to
retrieve all the attributes)
7.4.1 Below query retrives only ‘eno’ attribute and all the instances from the table ‘emp’
ENO
---
e1
e2
e3
e4
e5
7.4.2 Below query retrives only ‘ename’ attribute and all the instances from the table ‘emp’
ENAME
--------------------
smith
jhon
santhosh
raju
fayaz khan
Alias are used to display specific given names for the existing attributes. Below query display
ename as Employee_Name and sal as Salary from the emp table
Employee_Name Salary
-------------------- ----------
smith 10000
jhon 15000
santhosh 20000
raju 25000
fayaz khan 30000
7.6 ORDER BY clause: it is used to display the instances in a sorted order of the given
attribute. Asc is used for descending order. Desc is used for descending order. The default order
is Asc.
e1 1 smith 10000
e2 1 jhon 15000
e3 1 santhosh 20000
e4 2 raju 25000
e5 2 fayaz khan 30000
7.6.1 Below query displays emp table instances (data/records) in the default asceding order of
their names
6 rows selected.
7.6.2. Below query displays emp table instances (data/records) in the asceding order of their
names
e6 1 kartik
e4 2 raju 25000
e3 1 santhosh 20000
e1 1 smith 10000
6 rows selected.
7.6.3 Below query displays emp table instances (data/records) in the descending order of their
names
6 rows selected.
7.6.4 Below query displays emp table instances (data/records) in the default ascedning order of
their dno first and if two records has same dno then ename is used to sort.
e3 1 santhosh 20000
e1 1 smith 10000
e5 2 fayaz khan 30000
e4 2 raju 25000
6 rows selected.
7.6.4 Below query displays emp table instances (data/records) in the default ascedning order of
their ename first and if two records has same ename then dno is used to sort.
6 rows selected.
8.1 WHERE CLAUSE USING RELATIONAL OPERATOR (<, <=, >, >=,< >,
= , !=):
8.1.1 Below query retrieves all the records of emp whose salary is greater than 20000
8.1.2 Below query retrieves all the records of emp whose salary is greater than or equal to
20000
8.1.3 Below query retrieves all the records of emp whose salary is less than 20000
8.1.4 Below query retrieves all the records of emp whose salary is less than or equal to
20000
8.1.5 Below query retrieves all the records of emp whose salary not equal to 20000 using <>
8.1.6 Below query retrieves all the records of emp whose salary not equal to 20000 using !=
8.1.7 Below query retrieves all the records of emp whose salary is equal to 20000 using =
8.1.8 Below query retrieves all the records of emp whose name is smith
1 row created.
SQL> commit;
Commit complete.
6 rows selected.
8.2.1 Below query retrieves all the records of emp whose salary is null
e6 1 kartik
8.2.2 Below query retrieves all the records of emp whose salary is not null
8.3.1 Below query retrieves all the records of emp whose working for deptno 1 and alos having
salary greater than 10000
8.3.2 Below query retrieves all the records of emp whose working for deptno 1 or whose salary
is greater than 10000
8.3.3 Below query retrieves all the records of emp whose working for deptno 1 using IN operator
8.3.4 Below query retrieves all the records of emp whose working for deptno 1 or 2 using IN
operator
8.3.5 Below query retrieves all the records of emp who is not working for deptno 1 or 2 using
NOT IN operator
no rows selected
8.3.6 Below query retrieves all the records of emp who is not working for deptno 1 using NOT
IN operator
8.3.7 Below query retrieves all the records of emp whose salary lies between 15000 and 25000
SQL> select * from emp where sal BETWEEN 15000 AND 25000;
ENO DNO ENAME SAL
8.3.8 Below query retrieves all the records of emp whose name is smith
8.3.9 Below query retrieves all the records of emp whose name starts with s
8.3.10 Below query retrieves all the records of emp whose name ends with h
8.3.11 Below query retrieves all the records of emp whose name contains ‘h’
8.3.12 Below query retrieves all the records of emp whose name’s second letter is a
8.3.13 Below query retrieves all the records of emp whose name’s fourth letter is a
8.3.14 Below query retrieves all the records of emp whose name’s second letter from the last is s
SQL> select * from emp where ename LIKE ' % s _ ‘ ;
8.3.16 Below query retrieves all the records of emp whose name contains ‘a’
Table created.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL> commit;
Commit complete.
1. CARTESION JOIN
6 rows selected.
Table created.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
ID NAME
---------- --------------------
1 smith
2 john
ID NAME
---------- --------------------
2 john
3 wilson
UNION: Used to retrieve all the records from subquery1 and subqury2;
Synatx: Subquery-1 UNION Subqury-2
ID NAME
---------- --------------------
1 smith
2 john
3 wilson
INTERSECT: Used to retrieve all the records which are common in both subquery1 and
subqury2;
Synatx: Subquery-1 INTERSECT Subqury-2
ID NAME
---------- --------------------
2 john
MINUS: Used to retrieve all the records which EXISTS IN subquery1 and NOT EXISTS IN
subqury2;
Synatx: Subquery-1 MINUS Subqury-2
ID NAME
---------- --------------------
1 smith
ID NAME
---------- --------------------
3 wilson
9b.1 Query to retrieve total number of employees in emp table(instead of * we can also use key
attribute, in this case Eno)
COUNT(*)
----------
6
SQL> select count(*) AS no_of_employees from emp;
NO_OF_EMPLOYEES
---------------
6
9b.2 Query to retrieve total number of employees getting salary in emp table
----------
5
9b.3 Query to retrieve sum of salary of all the employees in emp table
SQL> select sum(sal) AS total_salary_paid from emp;
TOTAL_SALARY_PAID
-----------------
100000
HIGHEST_SALARY
--------------
30000
LOWEST_SALARY
-------------
10000
AVG(SAL)
----------
20000
DNO
----------
1
2
DNO NO_OF_EMPLOYEE
---------- --------------
1 4
2 2
DNO COUNT(ENO)
---------- ----------
1 4
2 2
DNO COUNT(ENO)
---------- ----------
1 4
no rows selected
DNO COUNT(ENO)
---------- ----------
2 2
DNO SUM(SAL)
---------- ----------
1 45000
2 55000
DNO SUM(SAL)
---------- ----------
2 55000
DNO MAX(SAL)
---------- ----------
1 20000
2 30000
6 rows selected.
12 PY 1 BANG
13 BENEFITS 1 BANG
14 PZ 2 BIJAPUR
15 COMPUTER 2 GULBARGA
8 rows selected.
QUERY 1: WRITE A NESTED QUERY TO SELECT THE SSN OF ALL EMPLOYEE WHO
WORKS ON THE SAME PROJECT THAT THE EMPLOYEE 'AHMED' WORKS ON
SQL> select distinct essn from works_on where pno in(select pno from works_on where essn
in(select ssn from employee where ename='AHMED'));
ESSN
----------
9876
9998
6668
SQL> select distinct pno from project where pno in(select pno from project, department,
employee where project.dno=department.dnumber and mgrssn=ssn and ename='ANAND');
PNO
----------
11
13
12
SQL> select distinct pno from project where pno in (select pno from works_on,employee where
essn=ssn and ename='ANAND');
PNO
----------
12
SQL> select distinct pno from project where pno in(select pno from project , department,
employee where project.dno=department.dnumber and mgrssn=ssn and ename='ANAND') or
pno in(select pno from works_on,employee where essn=ssn and ename='ANAND');
PNO
----------
11
13
12
SQL> select * from employee where salary between 20000 and 40000 and dno=1;
QUERY 6: WRITE A QUERY TO SELECT THE EMPLOYEE NAME WHO HAS ATLEAT
ONE DEPENDENT.
ENAME
--------------------
AHMED
ANAND
RAJA
QUERY 7: WRITE A QUERY TO SELECT THE MANAGER WHO HAS ATLEAT ONE
DEPENDENT.
SQL> select distinct ename from employee, dependent,department where ssn=essn and
ssn=mgrssn;
ENAME
--------------------
ANAND
(OR)
SQL> select ename from employee where ssn in(select essn from dependent where essn in(select
mgrssn from department));
ENAME
--------------------
ANAND
QUERY 8: WRITE A QUERY TO SELECT THE EMPLOYEE NAME WHO HAS
DEPENDENT WITH THE SAME SEX.
SQL> select distinct ename from employee,dependent where ssn=essn and employee.sex =
dependent.sex;
ENAME
--------------------
RAJA
QUERY 9: WRITE A QUERY TO SELECT THE EMPLOYEE NAME AND ALSO THE
DEPENDENT NAME WHO HAS DEPENDENT WITH THE SAME SEX.
ENAME DEP_NAME
-------------------- ----------
RAJA KIRAN
QUERY 10: WRITE A QUERY TO SELECT THE EMPLOYEE NAME WHO DOES NOT
HAVE DEPENDENT.
SQL> select ename from employee where ssn not in (select ssn from dependent where ssn =
essn);
ENAME
--------------------
FRANKLIN
JAMES
RAMESH
QUERY 11: WRITE A QUERY TO SELECT THE EMPLOYEE NAME AND HIS
DEPARTMENT NAME AND ALSO THE PROJECT NAMES ON WHICH THE EMPLOYEE
IS WORKING ON.
8 rows selected.
QUERY 12: WRITE A QUERY TO SELECT THE EMPLOYEE NAME WHOSE SALARY IS
GREATER THAN THE EMPLOYEE'S MAXIMUM SALARY OF DEPARTMENT NUMBER
2.
SQL> select ename from employee where salary>(select max(salary) from employee where
dno=2);
ENAME
--------------------
ANAND
10.1 ANY, ALL, SOME, EXIST and NOT EXIST in nested queries
A 25 2000
B 33 4000
C 23 8500
D 27 6000
E 24 4150
ANY: Any is used when 1 or more records match the sub condition and the result is true for
those records.
SQL> SELECT * FROM customer WHERE Age > ANY( SELECT Age FROM customer
WHERE Salary > 5000);
A 25 2000
B 33 4000
D 27 6000
E 24 4150
ALL: All is used when all the records match the sub condition then the output to be true else
false.
SQL> SELECT * FROM customer WHERE Age > ALL( SELECT Age FROM customer
WHERE Salary > 5000);
B 33 4000
SOME: It is used to must match at least one row in the subquery and must be preceded by
comparison operators. Suppose using greater than ( >) with SOME means greater than at least
one value.
SQL> SELECT * FROM customer WHERE Age > SOME( SELECT Age FROM customer
WHERE Salary > 5000);
A 25 2000
B 33 4000
D 27 6000
E 24 4150
Below sub query returns true, because in customer table we are having records with salary<5000.
When the sub query returns true main query returns its records
SQL> SELECT * FROM customer WHERE exists( SELECT Age FROM customer WHERE
Salary < 5000);
A 25 2000
B 33 4000
C 23 8500
D 27 6000
E 24 4150
Below sub query returns false, because in customer we are having records with salary<5000.
When the sub query returns false main query returns no records
SQL> SELECT * FROM customer WHERE NOT exists( SELECT Age FROM customer
WHERE Salary < 5000);
no rows selected
View created.
View created.
1 row updated.
e5 2 bhavesh 55000
View dropped.
View dropped.
SQL>Connect system/gpt109
User created.
Grant succeeded.
SQL> disconnect;
Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
Connected.
ERROR at line 1:
SQL> disconnect;
Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
Connected.
Grant succeeded.
Connected.
Table created.
ERROR at line 1:
SQL> disconnect;
Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
Connected.
Grant succeeded.
SQL> disconnect;
Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
Connected.
1 row created.
ID
----------
1
SQL> commit;
Commit complete.
SQL> disconnect;
Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
SQL> disconnect;
Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
Connected.
Revoke succeeded.
Connected.
ERROR at line 1:
SQL> CREATE TABLE TEACHER (ID NUMBER (10) PRIMARY KEY, NAME VARCHAR (20),
ADDRESS VARCHAR (30));
Table created.
2 IS
3 BEGIN
5 END;
6 /
Procedure created.
SQL> BEGIN
2 INSERTTEACHER(1,'SAVITHA','GULBARGA');
4 END;
5 /
ID NAME ADDRESS
1 SAVITHA GULBARGA
ID NAME ADDRESS
1 SAVITHA GULBARGA
2 SHANTHA GULBARGA
2 RETURN NUMBER
3 IS
4 N3 NUMBER(10);
5 BEGIN
6 N3:=N1+N2;
7 RETURN N3;
8 END;
9 /
Function created.
SQL> DECLARE
2 N3 NUMBER(10);
3 BEGIN
4 N3:=ADDITION(11,22);
6 END;
7 /
ADDITION RESULT IS 33
SQL> DECLARE
2 A NUMBER;
3 B NUMBER;
4 C NUMBER;
5 BEGIN
6 A:=&FIRSTNUMBER;
7 B:=&SECONDNUMBER;
9 END;
10 /
old 6: A:=&FIRSTNUMBER;
new 6: A:=50;
old 7: B:=&SECONDNUMBER;
new 7: B:=60;
RESULT IS30
2 RETURN NUMBER IS
3 TOTNUM NUMBER(2):=0;
4 BEGIN
6 RETURN TOTNUM;
7 END;
8 /
Function created.
SQL> DECLARE
2 TOTALSTUDENT NUMBER(2);
3 BEGIN
4 S:=TOTALSTUDENTS();
6 END;
7 /
REGNO SNAME CO
---------- -------------------- --
109IS20001 AKASH IS
109IS20002 AYAN IS
109IS20003 NAGESH IS
109CS20001 RAGHAV CS
1 row created.
1 row created.
SQL> COMMIT;
Commit complete.
---------- -------------------- --
109IS20001 AKASH IS
109IS20002 AYAN IS
1 DECLARE
2 rollno student.regno%type;
3 snm student.sname%type;
4 cr student.course%type;
5 BEGIN
6 rollno := '&RegisterNumer';
7 snm := '&StudentName';
8 cr := '&Course';
11 COMMIT;
12 -- adding savepoint
13 SAVEPOINT mysavepoint;
15 rollno := '&RegisterNumer';
16 snm := '&StudentName';
17 cr := '&Course';
20 ROLLBACK TO mysavepoint;
21* END;
SQL> /
old 8: cr := '&Course';
new 8: cr := 'IS';
REGNO SNAME CO
---------- -------------------- --
109IS20001 AKASH IS
109IS20002 AYAN IS
109IS20003 NAGESH IS
3 IS
4 BEGIN
6 DBMS_OUTPUT.PUT_LINE('RECORD INSERTED');
7* END;
8 /
Procedure created.
1 DECLARE
2 rollno student.regno%type;
3 snm student.sname%type;
4 cr student.course%type;
5 BEGIN
6 rollno := '&RegisterNumer';
7 snm := '&StudentName';
8 cr := '&Course';
9 INSERTDATA(rollno,snm,cr);
10 COMMIT;
11 -- adding savepoint
12 SAVEPOINT mysavepoint;
14 rollno := '&RegisterNumer';
15 snm := '&StudentName';
16 cr := '&Course';
17 INSERTDATA(rollno,snm,cr);
18 ROLLBACK TO mysavepoint;
19* END;
20 /
old 8: cr := '&Course';
new 8: cr := 'CS';
RECORD INSERTED
RECORD INSERTED
---------- -------------------- --
109IS20001 AKASH IS
109IS20002 AYAN IS
109IS20003 NAGESH IS
109CS20001 RAGHAV CS