0% found this document useful (0 votes)
27 views60 pages

RECORD_RDBMS 10

Rdbms

Uploaded by

Bharath Raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views60 pages

RECORD_RDBMS 10

Rdbms

Uploaded by

Bharath Raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

DEPARTMENT OF COMPUTER APPLICATIONS

RDBMS LABORATORY
(HBCA21AL2)

I YEAR BCA AI &DS-A

2023 -2024

STUDENT NAME :

REGISTER NUMBER:
DEPARTMENT OF COMPUTER APPLICATIONS

BONAFIDE CERTIFICATE

Certified that this is the Bonafide Record of work done by


………………………………………………………………… of
Reg.no: (…………………………..), BCA(AI&DS)-A section in

theRDBMS LABORATORY(HBCA21AL2) during the year

of 2023-2024.

Signature of Lab-in-Charge Signature of HOD

Submitted for the Practical Examination held on ……………….……

Internal Examiner External Examiner


RDBMS LABORATORY
CONTENTS
Sl. PAGE STAFF
DATE PROGRAM SIGNATURE
No. No.

SQL BASICS:
1.DDL-
1. CREATE,ALTER,DROP

2.DML-
INSERT, UPDATE, DELETE
3.DRL-
SELECT & ITS OPERATORS

2. VIEWS

INTEGRITY CONSTRAINTS-
3. NAMING CONSTRAINTS
SUB QUERIES :
4. A. NESTED QUERIES
B.COMPLEX QUERIES

5. SQL BUILT IN FUNCTIONS


SET OPERATIONS
6.

7. FIND FACTORIAL USING


PL/SQL

FIBONACCI SERIES USING


8. PL/SQL
EX.NO: 1 .1 SQL BASICS
DATE: 1.DDL – CREATE,ALTER,DROP

Aim:

Procedure:
DDL COMMANDS

1. CREATE : CREATE command is used to create objects in the database.

2. DROP : DROP command is used to delete the object from the database.

3. ALTER : ALTER command is used to alter the structure of database

CREATING TABLE:

Syntax

CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN NAME.1 <DATATYPE> (SIZE),


COLUMN NAME2 <DATATYPE> (SIZE) ............................................. )

1) Write a query to create EMP table.

CREATE TABLE EMP (EMPNO NUMBER (4), ENAME VARCHAR2 (10), DESIGNATION
VARCHAR2 (10), SALARY NUMBER (8,2))

Table created.

DESCRIBE TABLE:

Syntax:

DESC <TABLE NAME>

2) Write a query to display the column name and data type of the table employee.

DESC EMP
Table Column Data Length Precision Scale Primary Nullable Default Comment
Type Key
EMP EMPNO Number - 4 0 - - -
ENAME Varchar2 10 - - - - -
DESIGNATION Varchar2 10 - - - - -
SALARY Number - 8 2 - - -

1-4
CREATE NEW TABLE FROM AN EXISTING TABLE WITH ALL FIELDS:

Syntax:

CREATE TABLE <TRAGET TABLE NAME> SELECT * FROM <SOURCE TABLE NAME>

3) Write a query for create EMP1 table from an existing table EMP with all the fields

CREATE TABLE EMP1 AS SELECT * FROM EMP

Table created.

DESC EMP1

Table Column Data Length Precision Scale Primary Nullable Default Comment
Type Key
EMP1 EMPNO Number - 4 0 - - -
ENAME Varchar2 10 - - - - -
DESIGNATION Varchar2 10 - - - - -
SALARY Number - 8 2 - - -
1-4

CREATE NEW TABLE FROM AN EXISTING TABLE WITH SELECTED FIELDS

Syntax

CREATE TABLE <TRAGET TABLE NAME> AS SELECT EMPNO, ENAME

FROM <SOURCE TABLE NAME>

4) Write a query for create a from an existing table with selected fields

CREATE TABLE EMP2 AS SELECT EMPNO, ENAME FROM EMP

Table created.

DESC EMP2
Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment
EMP2 EMPNO Number - 4 0 - - -
ENAME Varchar2 10 - - - - -
1-2
ALTER SINGLE COLUMN

Syntax :

ALTER <TABLE NAME> MODIFY <COLUMN NAME> <DATATYPE> (SIZE)

5) Write a Query to Alter the column EMPNO NUMBER (4) TO EMPNO NUMBER

ALTER TABLE EMP MODIFY EMPNO NUMBER (6)

Table altered.

DESC EMP

Table Column Data Length Precision Scale Primary Nullable Default Comment
Type Key
EMP EMPNO Number - 6 0 - - -
ENAME Varchar2 10 - - - - -
DESIGNATION Varchar2 10 - - - - -
SALARY Number - 8 2 - - -
1-4
ALTER TABLE WITH MULTIPLE COLUMN:

Syntax:

ALTER <TABLE NAME> MODIFY <COLUMN NAME1> <DATATYPE> (SIZE), MODIFY


<COLUMN NAME2> <DATATYPE> (SIZE ......................................................... )

6) Write a Query to alter the table emp with multiple columns (EMPNO, ENAME)

ALTER TABLE EMP MODIFY (EMPNO NUMBER (7), ENAME VARCHAR2(12));

Table altered.

DESC EMP

Table Column Data Length Precision Scale Primary Nullable Default Comment
Type Key
EMP EMPNO Number - 7 0 - - -
ENAME Varchar2 12 - - - - -
DESIGNATION Varchar2 10 - - - - -
SALARY Number - 8 2 - - -
1-4
ADD NEW COLUMN
Syntax :

ALTER TABLE <TABLE NAME> ADD (<COLUMN NAME> <DATA TYPE> <SIZE>
7) Write a query to add a new column into emp Table

ALTER TABLE EMP ADD QUALIFICATION VARCHAR2(6)

Table altered.
DESC EMP

Tabl Column Data Lengt Precisio Scal Primar Nullabl Defaul Commen
e Type h n e y Key e t t
EMP EMPNO Number - 7 0 - - -
ENAME Varchar 12 - - - - -
2
DESIGNATION Varchar 10 - - - - -
2
SALARY Number - 8 2 - - -
QUALIFICATIO Varchar 6 - - - - -
N 2
1-5
ADD MULTIPLE COLUMNS INTO TABLE
Syntax

ALTER TABLE <TABLE NAME> ADD (<COLUMN NAME1> <DATA TYPE>


<SIZE>,(<COLUMN NAME2> <DATA TYPE><SIZE>,…..)

8) Write a query to add multiple columns into emp table

ALTER TABLE EMP ADD (DOB DATE, DOJ DATE);

Table altered.
DESC EMP
Tabl Column Data Lengt Precisio Scal Primar Nullabl Defaul Commen
e Type h n e y Key e t t
EMP EMPNO Number - 7 0 - - -
ENAME Varchar 12 - - - - -
2
DESIGNATION Varchar 10 - - - - -
2
SALARY Number - 8 2 - - -
QUALIFICATIO Varchar 6 - - - - -
N 2
DOB Date 7 - - - - -
DOJ Date 7 - - - - -
1-7
REMOVE / DROP

DROP A COLUMN FROM AN EXISTING TABLE

Syntax

ALTER TABLE <TABLE NAME> DROP COLUMN <COLUMN NAME>

9) Write a query to drop a column from an existing table emp

ALTER TABLE EMP DROP COLUMN DOJ

Table altered.

DESC EMP

Tabl Column Data Lengt Precisio Scal Primar Nullabl Defaul Commen
e Type h n e y Key e t t
EMP EMPNO Number - 7 0 - - -
ENAME Varchar 12 - - - - -
2
DESIGNATION Varchar 10 - - - - -
2
SALARY Number - 8 2 - - -
QUALIFICATIO Varchar 6 - - - - -
N 2
DOB Date 7 - - - - -
1-6
DROP MULTIPLE COLUMNS

Syntax

ALTER TABLE <TABLE NAME> DROP <COLUMN NAME1>,<COLUMN NAME2>

10) Write a query to drop multiple columns from emp table

ALTER TABLE EMP DROP (DOB, QUALIFICATION)

Table altered.

DESC EMP

Table Column Data Length Precision Scale Primary Nullable Default Comment
Type Key
EMP EMPNO Number - 7 0 - - -
ENAME Varchar2 12 - - - - -
DESIGNATION Varchar2 10 - - - - -
SALARY Number - 8 2 - - -
1-4
Result:
EX.NO: 1 .2 SQL BASICS 2.IMPLEMENTATION ON DML IN RDBMS
DATE:

Aim:

Procedure:
DML COMMANDS

1. INSERT : INSERT command is used to insert data to the table.

2. UPDATE : UPDATE command is used to UPDATE records in the table

3.DELETE : DELETE command is used to DELETE Records form the table

INSERT DATA INTO THE TABLE

Syntax

INSERT INTO <TABLE NAME> VALUES (VAL1, ‘VAL2’,…..);

1) Write a query to insert records in to emp table.

INSERT INTO EMP VALUES(101,'NAGARAJAN','LECTURER',15000)

1 row created.

SELECT * FROM EMP;

EMPNO ENAME DESIGNATION SALARY


101 NAGARAJAN LECTURER 15000

UPDATE SINGLE COLUMN

Syntax :

UPDATE <TABLE NAME> SET <COLUMNAME>=<VALUE> WHERE

<COLUMN NAME>=<VALUE>

2) Write a query to update salary is 16000 in emp table where empno is 101

UPDATE EMP SET SALARY=16000 WHERE EMPNO=101

1 row updated.

SELECT * FROM EMP

EMPNO ENAME DESIGNATION SALARY


101 NAGARAJAN LECTURER 16000
UPDATE MULTIPLE COLUMNS

Syntax:

UPDATE <TABLE NAME> SET <COLUMNANE>=<VALUE> WHERE

<COLUMN NAME>=<VALUE>;

3) Write a query to update multiple COLUMNS from emp TABLE.

UPDATE EMP SET SALARY = 17000, DESIGNATION='ASST. PROF' WHERE

EMPNO=101;

1 row updated.

SELECT * FROM EMP

MPNO ENAME DESIGNATION SALARY


101 NAGARAJAN ASST. PROF 17000

DELETE DATA FROM TABLE

Syntax :

DELETE <TABLE NAME> WHERE <COLUMN NAME>=<VALUE>;

4) Write a query to delete records from emp TABLE.

DELETE EMP WHERE EMPNO=101

1 row deleted.

SELECT * FROM EMP

NO DATA FOUND
Result:
EX.NO: 1 .3 SQL BASICS
DATE: 3. DRL DATA RETRIEVAL IMPLEMENTING ON SELECT
COMMANDS

Aim:

Procedure:
EMP Table having the following records:

EMPNO ENAME DESIGNATION SALARY


103 KALA PROF 50000
104 BALA INSTRUCTOR 5000
101 NAGARAJAN LECTURER 15000
102 MALA ASS PROF 25000
105 SUMA TECH ASST 10000

SELECT STATEMENT

The SELECT statement is used to select data from a database. The data returned is stored in a
result table, called the result-set.

Syntax

SELECT column1, column2, ... FROM table_name;

1) Write a query to Fetch EMPNO, EName and Salary fields of the customers available in
EMP table.

SELECT EMPNO, ENAME, SALARY FROM EMP

EMPNO ENAME SALARY


103 KALA 50000
104 BALA 5000
101 NAGARAJAN 15000
102 MALA 25000
105 SUMA 10000

2) Write a query to fetch all the fields of EMP table.

SELECT * FROM EMP

EMPNO ENAME DESIGNATION SALARY


103 KALA PROF 50000
104 BALA INSTRUCTOR 5000
101 NAGARAJAN LECTURER 15000
102 MALA ASS PROF 25000
105 SUMA TECH ASST 10000
SELECT STATEMENT WITH WHERE CLAUSE

Syntax:
SELECT column1, column2, column FROM table_name WHERE [condition]

3) Write a query to fetch EMPNO, ENAME, And Salary fields from the EMP table where
salary is greater than 20000
SELECT EMPNO, ENAME, SALARY FROM EMP WHERE SALARY > 20000

EMPNO ENAME SALARY


103 KALA 50000
102 MALA 25000

4) Write a query to fetch empno, eName and Salary fields from the emp table for an
employee with name KALA.

SELECT EMPNO, ENAME, SALARY FROM EMP WHERE ename='KALA'

EMPNO ENAME SALARY


103 KALA 50000

SELECT STATEMENT WITH AND OPERATOR

The AND operator allows the existence of multiple conditions in an SQL statement's WHERE
clause.

Syntax:
SELECT column1, column2, columnN FROM table_name WHERE [condition1] AND
[condition2]...AND [conditionN];

5) Write a query to fetch empno, eName and Salary fields from the emp table where
salary is greater than 20000 AND designation=’PROF’

SELECT EMPNO, ENAME, SALARY FROM EMP WHERE salary>20000 and


designation='PROF'

EMPNO ENAME SALARY


103 KALA 50000
SELECT STATEMENT WITH OR OPERATOR:

The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause.

Syntax:
SELECT column1, column2, columnN FROM table_name WHERE [condition1] OR
[condition2]...OR [conditionN]

6) Write a query to fetch empno, eName ,desination, Salary fields from the emp table
where salary is greater than 20000 or designation=’PROF’

SELECT * FROM EMP WHERE salary>20000 or designation='PROF'

EMPNO ENAME DESIGNATION SALARY


103 KALA PROF 50000
102 MALA ASS PROF 25000

SELECT STATEMENT WITH LIKE CLASUSE

The SQL LIKE clause is used to compare a value to similar values using wildcard operators.
There are two wildcards used in conjunction with the LIKE operator:

• The percent sign (%)


• The underscore (_)

The percent sign represents zero, one, or multiple characters. The underscore represents a single
number or character. The symbols can be used in combinations.

Syntax:

SELECT FROM table_name WHERE column LIKE 'XXXX%'

SELECT FROM table_name WHERE column LIKE 'XXXX_'

7) Write a query to display all the records from emp table where SALARY starts with
5

SELECT*FROM empWHERE SALARY LIKE ‘5%’

EMPNO ENAME DESIGNATION SALARY


103 KALA PROF 50000
104 BALA INSTRUCTOR 5000
8) Write aquery to display all the records from emp table where SALARY starts with
5(only 4 digits)

SELECT * FROM emp WHERE SALARY LIKE '5_ _ _'

EMPNO ENAME DESIGNATION SALARY


104 BALA INSTRUCTOR 5000

SELECT STATEMENT WITH ORDER BY CLASUSE

The SQL ORDER BY clause is used to sort the data in ascending or descending order, based on
one or more columns.

9) Write a query to sort the result in ascending order by ENAME.

SELECT * FROM EMP ORDER BY ENAME

EMPNO ENAME DESIGNATION SALARY


104 BALA INSTRUCTOR 5000
103 KALA PROF 50000
102 MALA ASS PROF 25000
101 NAGARAJAN LECTURER 15000
105 SUMA TECH ASST 10000

Write a query to sort the result in descending order by NAME.

SELECT * FROM EMP ORDER BY ENAME DESC

EMPNO ENAME DESIGNATION SALARY


105 SUMA TECH ASST 10000
101 NAGARAJAN LECTURER 15000
102 MALA ASS PROF 25000
103 KALA PROF 50000
104 BALA INSTRUCTOR 5000
Result:
EX.NO: 2 2.VIEWS
DATE:

Aim:

Procedure:
SQL COMMANDS

1. COMMAND NAME: CREATE VIEW

COMMAND DESCRIPTION: CREATE VIEW command is used to define a view.

2. COMMAND NAME: INSERT IN VIEW

COMMAND DESCRIPTION: INSERT command is used to insert a new row into the view.

3. COMMAND NAME: DELETE IN VIEW

COMMAND DESCRIPTION: DELETE command is used to delete a row from the view.

4. COMMAND NAME: UPDATE OF VIEW

COMMAND DESCRIPTION: UPDATE command is used to change a value in a tuple

without changing all values in the tuple.

5. COMMAND NAME: DROP OF VIEW

COMMAND DESCRIPTION: DROP command is used to drop the view table

COMMANDS EXECUTION

CREATION OF TABLE

CREATE TABLE EMP77 (Ename VARCHAR2(10), EMPno NUMBER(8), DEPTname


VARCHAR2(10), DEPTno NUMBER (5),Doj DATE)

Table created.

TABLE DESCRIPTION

DESC EMP77

Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment

EMP77 ENAME Varchar2 10 - - - - -


EMPNO Number - 8 0 - - -
DEPTNAME Varchar2 10 - - - - -
DEPTNO Number - 5 0 - - -
DOJ Date 7 - - - - -
CREATION OF VIEW

1) CREATE VIEW EMPVIEW AS SELECT ENAME, EmpNO, DEPTNAME, DEPTNO,


doj FROM EMP77

VIEW CREATED.

DESCRIPTION OF VIEW

2) DESC EMPVIEW

Data Primary
Table Column Length Precision Scale Nullable Default Comment
Type Key
EMPVIEWENAME Varchar2 10 - - - - -
EMPNO Number - 8 0 - - -
DEPTNAME Varchar2 10 - - - - -
DEPTNO Number - 5 0 - - -
DOJ Date 7 - - - - -

INSERTION INTO VIEW

3) insert into empview values('suma',12,'mca',4,'15-nov-22')

1 ROW INSERTED

4) SELECT * FROM EMPVIEW

ENAME EMPNO DEPTNAME DEPTNO DOJ


balu 20 mech 5 20-JAN-17
suja 18 bca 2 11-FEB-15
kala 180 mba 3 12-JAN-17
anu 19 mca 4 13-JAN-17

5) SELECT * FROM EMP77

ENAME EMPNO DEPTNAME DEPTNO DOJ


balu 20 mech 5 20-JAN-17
suja 18 bca 2 11-FEB-15
kala 180 mba 3 12-JAN-17
anu 19 mca 4 13-JAN-17
DELETION OF VIEW:

6) DELETE EMPVIEW WHERE empno=18

1 ROW DELETED.

SELECT * FROM EMPVIEW

ENAME EMPNO DEPTNAME DEPTNO DOJ


balu 20 mech 5 20-JAN-17
kala 180 mba 3 12-JAN-17
anu 19 mca 4 13-JAN-17

UPDATE STATEMENT:

7) UPDATE EMPVIEW SET ENAME='KAVI' WHERE Ename='anu'

1 row(s) updated.

8) SELECT * FROM EMPVIEW

ENAME EMPNO DEPTNAME DEPTNO DOJ


balu 20 mech 5 20-JAN-17
kala 180 mba 3 12-JAN-17
KAVI 19 mca 4 13-JAN-17

DROP A VIEW:

9) DROP VIEW EMPVIEW

VIEW DROPED

CREATE A VIEW WITH SELECTED FIELDS:

10) CREATE OR REPLACE VIEW EMPVIEW2 AS SELECT EMPNO, ENAME,doj FROM


EMPVIEW
View created.
11) select * from empview2

EMPNO ENAME DOJ


20 balu 20-JAN-17
180 kala 12-JAN-17
19 KAVI 13-JAN-17
CHANGING THE COLUMN’S NAME IN THE VIEW USING AS SELECT

STATEMENT:

Type 1
12) CREATE OR REPLACE VIEW EMPview3(EID,NAME,date_of_joining) AS SELECT
EMPNO,ENAME,doj FROM EMP77

View created.

13) select * from empview3

EID NAME DATE_OF_JOINING


20 balu 20-JAN-17
180 kala 12-JAN-17
19 KAVI 13-JAN-17

TYPE 2

14) CREATE OR REPLACE VIEW EMPview4 AS SELECT EMPNO "EID",ENAME


"EMPLOYEE_NAME",DOJ "JOINING_DATE" FROM EMP77
View created.
15) select * from empview4

EID EMPLOYEE_NAME JOINING_DATE


20 balu 20-JAN-17
180 kala 12-JAN-17
19 KAVI 13-JAN-17
Result:
EX.NO: 3 3. INTEGRITY CONSTRAINTS- NAMING
DATE: CONSTRAINTS

Aim:

Procedure:
I) Primary key Constraint

1. Write a query to create primary key constraint with column level

CREATE TABLE EMPLOYEE10(EMPNO NUMBER(4) PRIMARY KEY, ENAME


VARCHAR2(10), JOB VARCHAR2(6), SAL NUMBER(5), DEPTNO NUMBER(7))
Table created
insert into employee10 values(101,'selva', 'clerk',1000,1)

1 row(s) inserted.
insert into employee10 values(101,'dhanu', 'clerk',1000,1)
ORA-00001: unique constraint (SYSTEM.SYS_C004101) violated

2. Write a query to create primary constraints in column level with naming convention

CREATE TABLE EMPLOYEE11(EMPNO NUMBER(4) CONSTRAINT EMP_EMPNO_PK


PRIMARY KEY, ENAME VARCHAR2(10), JOB VARCHAR2(6), SAL NUMBER(5), DEPTNO
NUMBER(7))
Table created.
insert into employee11 values(105,'dhanu', 'clerk',1000,1)
1 row(s) inserted
insert into employee11 values(105,'manju', 'AP',1000,1)
ORA-00001: unique constraint (SYSTEM.EMP_PRK) violated

II) Reference /foreign key constraint

Write a query to create foreign key constraints with column level

Parent Table:

CREATE TABLE DEPT(DEPTNO NUMBER(2) PRIMARY KEY, DNAME VARCHAR2(20),


LOCATION VARCHAR2(15))

Child Table:

CREATE TABLE EMP4 (EMPNO NUMBER(3), DEPTNO NUMBER(2) REFERENCES


DEPT(DEPTNO), DESIGN VARCHAR2(10))
III) Check constraint

3. Write a query to create Check constraints with column level

CREATE TABLE nila80(EMPNO NUMBER(3), ENAME VARCHAR2(20), DESIGN


VARCHAR2(15), SAL NUMBER(5) CONSTRAINT EMP7_SAL_CK CHECK(SAL>500 AND
SAL<10001), DEPTNO NUMBER(2))

insert into nila80 values(101, 'nirmala','ap',1000, 2)


1 row(s) inserted.

insert into nila80 values(101, 'mala','ap',400, 2)


ORA-02290: check constraint (SYSTEM.EMP7_SAL_CK) violated
insert into nila80 values(101, 'kalai','ap',10002, 2)
ORA-02290: check constraint (SYSTEM.EMP7_SAL_CK) violated

IV) Unique Constraint

4. Write a query to create unique constraints with column level

CREATE TABLE EMP10(EMPNO NUMBER(3), ENAME VARCHAR2(20), DESGIN


VARCHAR2(15) CONSTRAINT EMP10_DESIGN_UK UNIQUE, SAL NUMBER(5))

insert into emp10 values(11, 'raja','Manager',1000)


1 row(s) inserted.

insert into emp10 values(12, 'mala','Manager',1000)


ORA-00001: unique constraint (SYSTEM.EMP10_DESIGN_UK) violated
V) Not Null Constraint

6) Write a query to create Not Null constraints with column level

CREATE TABLE EMP13 (EMPNO NUMBER(4), ENAME VARCHAR2(20) CONSTRAINT


EMP13_ENAME_NN NOT NULL, DESIGN VARCHAR2(20), SAL NUMBER(3))

insert into emp13(empno,ename,design,sal) values(101,'balu','ap',800)


1 row(s) inserted.
insert into emp13(empno,design,sal) values(101,'ap',800)
ORA-01400: cannot insert NULL into ("SYSTEM"."EMP13"."ENAME")
VI) Null Constraint

Write a query to create Null constraints with column level

CREATE TABLE EMP14 (EMPNO NUMBER(4), ENAME VARCHAR2(20) CONSTRAINT


EMP13_ENAME_NN NULL, DESIGN VARCHAR2(20), SAL NUMBER(3));

insert into emp14(empno,ename,design,sal) values(101,'balu','ap',800)


1 row(s) inserted.
insert into emp14(empno,design,sal) values(102,'ap',800)
1 row(s) inserted.
VII) Default Constraint

CREATE TABLE emp25(ID number(4),Name varchar(25), Age number(2), City varchar(25)


DEFAULT 'Sandnes')

insert into emp25(id,name,age) values(101,'joe',34)


1 row(s) inserted.
insert into emp25(id,name,age) values(102,'suma',32)
1 row(s) inserted.
select * from emp25

ID NAME AGE CITY


101 joe 34 Sandnes
102 suma 32 Sandnes
Result:
EX.NO: 4.1 SUB QUERIES : A.NESTED
DATE: QUERIES

Aim:

Procedure:
Student Table

STUDENTID NAME
v002 Abhay
v003 Acelin
v004 Adephos
v001 Abe

Marks Table

STUDENTID TOTALMARKS
v001 95
v002 80
v003 74
v004 81

1.Write a query to identify all students who get better marks than that of the student who's StudentID is
'v002', but we do not know the marks of 'v002'

SELECT a.studentid, a.name, b.totalmarks FROM student a, marks b WHERE a.studentid =


b.studentid AND b.totalmarks > (SELECT totalmarks FROM marks WHERE studentid = 'v002')

STUDENTID NAME TOTALMARKS


v004 Adephos 81
v001 Abe 95
Result:
EX.NO: 4.2 SUB QUERIES :
DATE: B.COMPLEX QUERIES

Aim:

Procedure:
Emp table

EMPNO ENAME DESIGNATION SALARY


103 KALA PROF 50000
104 BALA INSTRUCTOR 5000
101 NAGARAJAN LECTURER 15000
102 MALA ASS PROF 25000
105 SUMA TECH ASST 10000
105 SUMA TECH ASST 20000
101 NAGARAJAN LECTURER 12000

1) Write a
query to select all record from emp where designation not in LECTURER or
ASS PROF.

select * from emp where DESIGNATION not in ('LECTURER','ASS PROF')

EMPNO ENAME DESIGNATION SALARY


103 KALA PROF 50000
104 BALA INSTRUCTOR 5000
105 SUMA TECH ASST 10000
105 SUMA TECH ASST 20000

2)Write a query to select all record from emp where ename in ‘KALA','BALA' and
'SUMA'.

select * from emp where ename in('KALA','BALA','SUMA')

EMPNO ENAME DESIGNATION SALARY


103 KALA PROF 50000
104 BALA INSTRUCTOR 5000
105 SUMA TECH ASST 10000
105 SUMA TECH ASST 20000

3) Write a query to select all record from emp where ename NOT in ‘KALA','BALA' and
'SUMA'

select * from emp where ename NOT in('KALA','BALA','SUMA')

EMPNO ENAME DESIGNATION SALARY


101 NAGARAJAN LECTURER 15000
102 MALA ASS PROF 25000
101 NAGARAJAN LECTURER 12000
Result:
EX.NO: 5 SQL BUILT IN FUNCTIONS
DATE:

Aim:

Procedure:
Table name : employees

SELECT * FROM employees;

ID NAME WORK_DATE DAILY_TYPING_PAGES


101 kala 12-JAN-17 100
102 mala 12-JAN-17 200
103 kumar 12-JAN-17 600
104 raja 12-JAN-17 500
105 sugi 12-JAN-17 300
101 kala 13-JAN-17 670
103 kumar 13-JAN-17 650
102 mala 14-JAN-17 60
101 kala 15-JAN-17 690
102 mala 15-JAN-17 490

Group functions(Aggregate Functions)

COUNT()

Returns the number of rows.

Syntax

SELECT COUNT(*) FROM table_name;

1. Write a query to count total number of rows in this table.

SELECT COUNT(*) FROM employees

COUNT(*)
10
2. Write a query to count the number of records for id is 101.

SELECT COUNT(*) FROM employees WHERE id= 101

COUNT(*)
3
MAX function

SQL MAX function is used to find out the record with maximum value among a record set.

3) Write a query to fetch maximum value of daily_typing_pages.

SELECT MAX(daily_typing_pages)FROM employees

MAX(DAILY_TYPING_PAGES)

690
MIN function

SQL MIN function is used to find out the record with minimum value among a record set.

4) Write a Query to fetch minimum value of daily_typing_pages.

SELECT MIN(daily_typing_pages) FROM employee

MAX(DAILY_TYPING_PAGES)

60
5) Write a query use MIN Function along with MAX function to find out minimum value.

SELECT MIN(daily_typing_pages) least, MAX(daily_typing_pages) max FROM employees

LEAST MAX
60 690
AVG function

SQL AVG function is used to find out the average of a field in various records.

6) Write a query to calculate average of all the dialy_typing_pages.

SELECT AVG(daily_typing_pages) FROM employees

AVG(DAILY_TYPING_PAGES)
426
Sum function

SQL SUM function is used to find out the sum of a field in various records.

7) Write a query to calculate total of all the dialy_typing_pages.

SELECT SUM(daily_typing_pages) FROM employees

SUM(DAILY_TYPING_PAGES)
4260

8) Write a query to find the sum up all the records related to a single person and total typed
pages by every person.

SELECT name, SUM(daily_typing_pages) FROM employees GROUP BY name

NAME SUM(DAILY_TYPING_PAGES)
raja 500
kala 1460
kumar 1250
sugi 300
mala 750

SQL - Numeric Functions

SQRT function

9) Write a query to find out the square root of any number.

select SQRT(16) from dual

SQRT(16)
4
ABS(X)

The ABS() function returns the absolute value of X.

10)Write a query to find the absolute value of -2

SELECT ABS(-2) from dual

ABS(-2)
2

COS(X)

This function returns the cosine of X. The value of X is given in radians.

11) Write a query to find the cos value of 90.

SELECT COS(90) from dual

COS(90)
-.44807361612917015236547731439963950742
EXP(X)

This function returns the value of e (the base of the natural logarithm) raised to the power of X.

12) Write a query to find the exponential value of 3.

SELECT EXP(3) from dual

EXP(3)
20.0855369231876677409285296545817178971

GREATEST(n1,n2,n3, ......... )

The GREATEST() function returns the greatest value in the set of input parameters (n1, n2, n3, a
nd so on).

13) Write a query to return the largest number from a set of numeric values.

SELECT GREATEST(3,5,1,8,33,99,34,55,67,43) from dual

GREATEST(3,5,1,8,33,99,34,55,67,43)
99
LEAST(N1,N2,N3,N4, ..... )

The LEAST() function is the opposite of the GREATEST() function.

14)Write a query to return the least-valued item from the value list

SELECT LEAST(3,5,1,8,33,99,34,55,67,43) from dual

LEAST(3,5,1,8,33,99,34,55,67,43)
1

MOD(N,M)

This function returns the remainder of N divided by M.

15)Write a query to return the remainder of 29 divided by 3.

SELECT MOD(29,3) from dual

MOD(29,3)
2

POWER(X,Y)

These two functions return the value of X raised to the power of Y.

16)Write a query to return the value of 3 raised to the power of 3.

SELECT POWER(3,3) from dual

POWER(3,3)
27

SQL string functions

ASCII(str)

Returns the numeric value of the leftmost character of the string str. Returns 0 if str is the empty
string. Returns NULL if str is NULL. ASCII() works for characters with numeric values from 0 to
255.
17) Write a SQL statement to find the ASCII value of 2.

SELECT ASCII('2') from dual

ASCII('2')
50

INSTR(str,substr)

Returns the position of the first occurrence of substring substr in string str.

18) Write a SQL statement to return the position of the first occurance of substring ‘bar’.

SELECT INSTR('foobarbar', 'bar') from dual

INSTR('FOOBARBAR','BAR')
4
LENGTH(str)

Returns the length of the string str, measured in bytes.

19) Write a SQL statement to find the length of the string ‘text’.

SELECT LENGTH('text') from dual

LENGTH('TEXT')
4

LOWER(str)

Returns the string str with all characters changed to lowercase according to the current character
set mapping.

20) Write a query to change the given string into lowercase letters

SELECT LOWER('COMPUTER SCIENCE') from dual

LOWER('COMPUTERSCIENCE')
computer science
REPLACE(str,from_str,to_str)

Returns the string str with all occurrences of the string from_str replaced by the string to_str.

21) Write a query to replace ‘w’ with ‘Ww’

SELECT REPLACE('www.mysql.com', 'w', 'Ww') FROM DUAL

REPLACE('WWW.MYSQL.COM','W','WW')
WwWwWw.mysql.com

UPPER(str)

Returns the string str with all characters changed to uppercase according to the current character
set mapping.

Write a SQL statement to convert the given string to uppercase letters.

22) SELECT UPPER('computer applications') from dual

UPPER('computer applications')
COMPUTER APPLICATIONS

CONCAT function

SQL CONCAT function is used to concatenate two strings to form a single string.

DATE functions:

Add_months :

It adds the month in specified date with the function

Syntax:

Add_months(Date,m) where date refers to date and m is number value.


23) MONTHS_BETWEEN :

It computes and returns the number of months between 2 dates.

Syntax:

MONTHS_BETWEEN(date1, date2)

24) Write a SQL query to find number of months between 2 specified dates.

Select months_between('01-mar-06','01-dec-05') from dual

MONTHS_BETWEEN('01-MAR-06','01-DEC-05')
3

Next_day:

It returns the date of first day of the week specified in a string after the beginning date.

Syntax: NEXT_DAT(date,ch)

where ch refers to character value

25) Write a query to find the next Sunday of date 13/march/2005

Select next_day('13-mar-05','Sunday') from dual

NEXT_DAY('13-MAR-05','SUNDAY')
20-MAR-05
Result :
EX.NO: 6 SET OPERATIONS
DATE:

Aim:

Procedure:
Set Operation in SQL

SQL supports few Set operations to be performed on table data. These are used to get meaningful
results from data, under different special conditions.

The student1 table,

REGNO NAME COURSE SECTION CLGNAME


101 suma bca a mgr university
102 kannan bca a mgr university
103 sumesh bsc b mgr university
104 akash bca a mgr university
105 zara bca a mgr university
106 mala bsc b mgr university
108 vino bsc c mgr university
109 arun bsc c mgr university
The student2 table,
REGNO NAME COURSE SECTION CLGNAME
102 kannan bca a mgr university

105 zara bca a mgr university

106 mala bsc b mgr university

107 kishore bsc c mgr university

109 arun bsc c mgr university

Union

UNION is used to combine the results of two or more Select statements. However it will eliminate
duplicate rows from its result set. In case of union, number of columns and datatype must be same
in both the tables.
select * from student1 union select * from student2

Result table

REGNO NAME COURSE SECTION CLGNAME


101 suma bca a mgr university
102 kannan bca a mgr university
103 sumesh bsc b mgr university
104 akash bca a mgr university
105 zara bca a mgr university
106 mala bsc b mgr university
107 kishore bsc c mgr university
108 vino bsc c mgr university
109 arun bsc c mgr university
Union All

This operation is similar to Union. But it also shows the duplicate rows.

select * from student1 union all select * from student2

Result table

REGNO NAME COURSE SECTION CLGNAME


101 suma bca a mgr university
102 kannan bca a mgr university
103 sumesh bsc b mgr university
104 akash bca a mgr university
105 zara bca a mgr university
106 mala bsc b mgr university
108 vino bsc c mgr university
109 arun bsc c mgr university
102 kannan bca a mgr university
105 zara bca a mgr university
106 mala bsc b mgr university
107 kishore bsc c mgr university
109 arun bsc c mgr university
Intersect

Intersect operation is used to combine two SELECT statements, but it only retuns the records
which are common from both SELECT statements. In case of Intersect the number of columns
and datatype must be same.

select * from student1 intersect select * from student2

Result table

REGNO NAME COURSE SECTION CLGNAME


102 kannan bca a mgr university
105 zara bca a mgr university
106 mala bsc b mgr university
109 arun bsc c mgr university
Minus

Minus operation combines result of two Select statements and return only those result which
belongs to first set of result.

select * from student1 minus select * from student2

Result table

REGNO NAME COURSE SECTION CLGNAME


101 suma bca a mgr university
103 sumesh bsc b mgr university
104 akash bca a mgr university
108 vino bsc c mgr university
Result:
EX.NO: 7 FIND FACTORIAL
DATE:

Aim:

Procedure
SOURCE CODE

create function fnew(n number)


return number is
b number;
begin
b:=1;
for i in 1..n
loop
b:=b*i;
end loop;
return b;
end;
declare
n number:=5;
y number;
begin
y:=fnew(n);
dbms_output.put_line(y);
end;
OUTPUT:

120
Result :
EX.NO:8 FIBONACCI SERIES
DATE:

Aim:

Procedure:
SOURCE CODE

declare

f1 number(3);

f2 number(3);

f3 number(3);

num number(3);

begin

f1:=0;

f2:=1; f3:=0;

num:=1;

while num<=10

loop

dbms_output.put_line(f3);

f1:=f2;

f2:=f3;

f3:=f1+f2;

num:=num+1;

end loop; end;


OUTPUT:

13

21

34
Result :

You might also like