Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

GOVERNMENT POLYTECHNIC

PORBANDAR

DIPLOMA ENGINEERING (SEMESTER-3)

DATABASE MANAGEMENT SYSTEM (3330703)

LABORATORY MANUAL

ENROLLMENT NO : _____________________________________

NAME : _____________________________________

DIVISION : _____________________________________

ADDRESS : _____________________________________

_____________________________________

_____________________________________
GOVERNMENT POLYTECHNIC
PORBANDAR

CERTIFICATE

This is to certify that,

Mr./Misss.__________________________________________________________________

Enrollment No._______________________ of 3rd Semester Diploma course in Computer

Engineering has satisfactorily completed his/her term work in DATABASE MANAGEMENT

SYSTEM (3330703) from _____________________ to__________________ within four

walls of Government Polytechnic, Porbandar.

Date of Submission____________________________

Staff in charge _____________________ Head of Department _____________________


INDEX

SR.
PRACTICAL PAGE DATE SIGN
NO.

PERFORM QUERIES FOR


1 DDL(CREATE) and DML(INSERT)
COMMANDS.
PERFORM QUERIES FOR
2 RETRIVAL OF RECORDS FROM A
TABLE.
PERFORM QUERIES FOR DML
3 (UPDATE and DELETE)
COMMANDS.
PERFORM QUERIES FOR DML
4 (ALTER, TRUNCATE, DROP and
RENAME) COMMANDS.

PERFORM QUERIES USING


5
OPERATORS.

DEMONSTRATE VARIOUS DATE


6
FUNCTIONS.

DEMONSTRATE VARIOUS
7
NUMERIC FUNCTIONS.

DEMONSTRATE VARIOUS
8
CHARACTER FUNCTIONS.

DEMONSTRATE VARIOUS
9
CONVERSION FUNCTIONS.

DEMONSTRATE VARIOUS GROUP


10
FUNCTIONS.

PERFORM QUERIES USING


11 GROUP BY, HAVING AND ORDER
BY CLAUSE.
DATABASE MANAGEMENT SYSTEM (3330703)

PRACTICAL: 1
AIM: PERFORM QUERIES FOR DDL(CREATE) and DML(INSERT)
COMMANDS.

SQL Data Types:-


 DATATYPE
 CHAR(size)
 VARCHAR(size)/VARCHAR2(size)
 DATE
 NUMBER(P,S)
 LONG
 RAW/LONG RAW

 CHAR(SIZE)
 This data type is used to store character strings values of fixed length.
 The size represents length of character should be stored
 This data can store maximum 255 characters.
 Example: Name CHAR(60)

 VARCHAR(size)/VARCHAR2(size)
 This data type used store variable length alphanumeric data.
 The maximum size of this data type is 4000 characters.

 DATE
 This data type is used to store date and time.
 The standard format is DD-MON-YY for example 21-JUN-09.
 Date time stores time in the 24-hours format.
 Range of this data type is January 1, 4712 B.C. to December 31, 4712 A.D.

 NUMBER(P,S)
 The NUMBER data type is used store numbers (both Integer and floating point
numbers).
 We can store number up to 38 characters long.
 The precision (P), determines the maximum length of number, and scale (s),
determines the point sign (.) right from a number(P).

 LONG
 This data type is used to store variable length character strings up to 2 GB.
 LONG data can be used to store binary data in ASCII format.

 RAW/LONG RAW
 The RAW/LONG RAW data type are used to store binary data, such as digitized
picture or image.
 RAW data type can have a maximum length of 255 bytes.
 LONG RAW data type can contain up to 2 GB.

GOVERNMENT POLYTECHNIC, PORBANDAR 1


DATABASE MANAGEMENT SYSTEM (3330703)

SQL Commands:-
 CREATE
 Create command used to create a table in database.

 Syntax:
SQL> Create table <table name>
(
<columnname1> <datatype> (<size>),
<columnname2> <datatype> (<size>)
);

 Example:
SQL> Create table client_master
(
client_no varchar2(6),
name varchar2(20),
city varchar2(15),
pincode number(8),
state varchar2(15)
);

 INSERT
 Used to insert data into a table or create a new row in table:
 We have two methods to insert data in a table
a) By value method
b) By address method

a) USING VALUE METHOD


 Syntax:
insert into <table_name> values (value1,value2, value3 …. Valuen);

 Example:
SQL> insert into client_master
values(‘C00001’,’RAHUL’,’Mumbai’,400054,’Maharashtra’);
SQL> insert into client_master
values (‘C00002’,’MANISH’,’Bangalore’,560001,’Karnataka’);

To insert a new record again you have to type entire insert command, if there
are lot of records this will be difficult. This will be avoided by using address
method.

b) USING ADDRESS METHOD


 Syntax:
insert into <table_name> values (&col1, &col2, &col3 …. &coln);

This will prompt you for the values but for every insert you have to use
forward slash.

 Example:

GOVERNMENT POLYTECHNIC, PORBANDAR 2


DATABASE MANAGEMENT SYSTEM (3330703)

SQL> insert into client_master


values (‘&client_no’, '&name', ‘&city’,&pincode,’&state’);

Enter value for client_no: C0001


Enter value for name: Rahul
Enter value for city: Mumbai
Enter value for pincode: 400054
Enter value for state: Maharashtra

Enter value for client_no: C0002


Enter value for name: Manish
Enter value for city: Bangalore
Enter value for pincode: 560001
Enter value for state: Karnataka

GOVERNMENT POLYTECHNIC, PORBANDAR 3


DATABASE MANAGEMENT SYSTEM (3330703)

EXERCISE:-
Create the tables described below:-

Table Name : Client_master

Column Name Data Type Size


Client_no Varchar2 6
Name Varchar2 20
City Varchar2 15
Pincode Number 8
State Varchar2 15

Insert data for Client_master table:-

Client_no Name City Pincode State


C0001 Ivan Bayross Bombay 4000054 Maharastra
C0002 Vandana Madras 7800001 Tamilnadu
C0003 Pramala Bombay 4000057 Maharastra
C0004 Chhaya Bangalore 5600051 Karnataka
C0005 Hansel Bombay 4500060 Maharashtra
C0006 Deepak Mangalore 5600550 Karnataka

GOVERNMENT POLYTECHNIC, PORBANDAR 4


DATABASE MANAGEMENT SYSTEM (3330703)

GOVERNMENT POLYTECHNIC, PORBANDAR 5


DATABASE MANAGEMENT SYSTEM (3330703)

Table Name : Product_master

Column Name Data Type Size


Product_no Varchar2 6
Description Varchar2 20
Quantity Number 8
Sell_price Number 8,2
Cost_price Number 8,2

Insert data for Product_master table:-

Product_no Description Quantity Sell_price Cost_price


P00001 1.44 floppies 100 525 500
P00002 Monitor 10 12000 11280
P00003 Mouse 20 1050 1000
P00004 Jeans 50 750 500
P07885 Trousers 60 850 550
P07975 Skirts 50 450 300

GOVERNMENT POLYTECHNIC, PORBANDAR 6


DATABASE MANAGEMENT SYSTEM (3330703)

GOVERNMENT POLYTECHNIC, PORBANDAR 7


DATABASE MANAGEMENT SYSTEM (3330703)

Table Name : Salesman_master

Column Name Data Type Size


Salesman_no Varchar2 6
Salesman_name Varchar2 20
City Varchar2 20
Pincode Number 8
State Varchar2 20
Salary Number 8
Remarks Varchar2 20

Insert data for Salesman_master table:-

Salesman_no Salesman_name City Pincode State Salary Remarks


S00001 Kiran Bombay 400002 Maharashtra 35000 Good
S00002 Manish Bombay 400001 Maharashtra 5000 Good
S00003 Ravi Bombay 400003 Maharashtra 4000 Average
S00004 Prem Bombay 400002 Maharashtra 8000 Good
S00005 Vishal Bombay 400001 Maharashtra 12000 Good
S00006 Rahul Bombay 400003 Maharashtra 25000 Good

GOVERNMENT POLYTECHNIC, PORBANDAR 8


DATABASE MANAGEMENT SYSTEM (3330703)

GOVERNMENT POLYTECHNIC, PORBANDAR 9


DATABASE MANAGEMENT SYSTEM (3330703)

PRACTICAL: 2
AIM: PERFORM QUERIES FOR RETRIVAL OF RECORDS FROM A
TABLE.

 SELECT
 The SELECT command is used to retrieve selected rows from one or more tables.

 Syntax:
1) SELECT * FROM <TableName>;
2) SELECT <ColumnName1>,<ColumnName2> From <TableName>;
3) SELECT * FROM <TableName> WHERE <Condition>;

 Example:
1) SELECT * FROM client_master;
2) SELECT name, city FROM client_master;
3) SELECT * FROM client_master WHERE city=’Bombay’;

 DESCRIBE
 The DESCRIBE command is used to display information of table structure.

 Syntax:
DESCRIBE <TableName>;

 Example:
DESCRIBE client_master;

Name Null? Type


----------------------------------------------------------------------------
Client_no Varchar2(6)
Name Varchar2(20)
City Varchar2(15)
Pincode Number(8)
State Varchar2(2)

GOVERNMENT POLYTECHNIC, PORBANDAR 10


DATABASE MANAGEMENT SYSTEM (3330703)

EXERCISE:-
Exercise on retrieving records from a table.

1) Retrieve entire contents of Client_master table.

2) Find out the name of all the clients.

3) Retrieve the list of name, city and state of all the clients.

GOVERNMENT POLYTECHNIC, PORBANDAR 11


DATABASE MANAGEMENT SYSTEM (3330703)

4) List the various products available from the Product_master table.

5) List all the clients who are located in Bombay.

6) List all the products whose sell_price is >= 1000 rupees.

GOVERNMENT POLYTECHNIC, PORBANDAR 12


DATABASE MANAGEMENT SYSTEM (3330703)

7) Find the names of salesman who have a salary greater than 10000 rupees.

8) Display the structure of product_master, salesman_master tables.

9) Display list of all tables

GOVERNMENT POLYTECHNIC, PORBANDAR 13


DATABASE MANAGEMENT SYSTEM (3330703)

PRACTICAL: 3
AIM: PERFORM QUERIES FOR DML (UPDATE and DELETE)
COMMANDS.

 UPDATE
 The UPDATE command is used to change or modify data of a table
 Update command used to update
a) Selected rows from table
b) All the rows from table

 Syntax:
Update <table name>
Set <columnname1> = <expression1>,
<columnname2> = <expression2>;
Where condition;

 Example:
Update client_master
Set city=’Bombay’
Where client_no=’C00002’;

 DELETE
 Delete command used to delete data or rows from a table
 Delete command used to delete
a) Selected rows from table
b) All the rows from table

 Syntax:
1) Delete from <table name>;
2) Delete from <table name> Where <Condition>;

 Example:
1) Delete from client_master;
2) Delete from client_master Where client_no=’C00001’;

GOVERNMENT POLYTECHNIC, PORBANDAR 14


DATABASE MANAGEMENT SYSTEM (3330703)

EXERCISE:-
Exercise on updating records in a table:-

1) Change the city of client_no ‘C00005’ to ‘Bangalore’ in Client_master table.

2) Change the cost_price of ‘Trousers’ to 999.50 rupees.

3) Change the salary of ‘S00002’ to 6500 rupees.

GOVERNMENT POLYTECHNIC, PORBANDAR 15


DATABASE MANAGEMENT SYSTEM (3330703)

Exercise on deleting record in a table:-

1) Delete all salesman from salesman_master whose salaries are greater than 5000
rupees.

2) Delete all products from Product_master where the quantity is equal to 50.

3) Delete from client_master where the column state holds the value ‘Tamilnadu’.

GOVERNMENT POLYTECHNIC, PORBANDAR 16


DATABASE MANAGEMENT SYSTEM (3330703)

PRACTICAL: 4
AIM: PERFORM QUERIES FOR DML (ALTER, TRUNCATE, DROP
and RENAME) COMMANDS.

 ALTER TABLE
 Alter command used to modify structures of a table.
 Alter command can be used to add ,modify, or drop columns in a table.

 Syntax:

 Adding New Columns


SQL> Alter table TableName
Add (NewColumnName Datatype(size),
NewColumnName Datatype(size)….);

 Modifying Existing Columns


SQL> Alter table TableName
Modify (ColumnName NewDatatype(NewSize));

 Dropping(Deleting) Existing Columns


SQL> Alter table TableName
Drop column columname;

 Example:
1) alter table client_master add(telephone_no number(10));
2) alter table product_master modify(sell_price number(10,2));
3) alter table emp drop column dept;

 TRUNCATE TABLE
 TRUNCATE TABLE used to delete all data from a table
 Logically, this is equivalent to DELETE statement that deletes all rows
 TRUNCATE command is faster than DELETE command
 The number of deleted rows are not returned

 Syntax:
TRUNCATE TABLE <TableName>

 Example:
SQL> TRUNCATE TABLE client_master;

 DROP TABLE
 DROP TABLE command is used to delete or destroy table from a database

 Syntax:
DROP TABLE <TableName>

GOVERNMENT POLYTECHNIC, PORBANDAR 17


DATABASE MANAGEMENT SYSTEM (3330703)

 Example:
SQL> DROP TABLE client_master;

 RENAME TABLE
 RENAME TABLE command is used to change the name of existing table.

 Syntax:
RENAME <TableName> TO <NewTableName>

 Example:
SQL> RENAME Client_master TO Client;

GOVERNMENT POLYTECHNIC, PORBANDAR 18


DATABASE MANAGEMENT SYSTEM (3330703)

EXERCISE:-
Exercise on altering table structure, deleting table structure and renaming a table:-

Add a column called ‘Telephone’ of data type number and size=10 to the Client_master
table.

Change the size of Sell_price column in product_master to (10,2)

Change the size of salary column in salesman_master to 10.

GOVERNMENT POLYTECHNIC, PORBANDAR 19


DATABASE MANAGEMENT SYSTEM (3330703)

Destroy the table Client_master along with its data.

Change the name of the Salesman_master table to Sman_master.

GOVERNMENT POLYTECHNIC, PORBANDAR 20


DATABASE MANAGEMENT SYSTEM (3330703)

PRACTICAL: 5
AIM: PERFORM QUERIES USING OPERATORS.
 Arithmetic Operators
 Oracle allows arithmetic operators to used while viewing records from table or
while performing data manipulation operations such as Insert, Update and Delete.
 These are:
+ Addition * Multiplication
- Subtraction / Division
** Exponentiation () Enclosed Operation
 Example:
Select product_no, description, sell_price * 0.05 from product_master;

 Logical Operators
 The AND Operator
 The OR Operator
 Combining the AND and OR Operator
 The NOT Operator

 The AND Operator


 The AND Operator allows creating an SQL statement based on two or more
conditions being met.
 It can be used in any valid SQL statement such as select, insert, or delete.
 Example: Select product_no, description, sell_price * 0.05 from
product_master where sell_price between 500 and 1100;

 The OR Operator
 The OR condition allows creating an SQL statement where records are
returned when any one of the conditions are met
 It can be used in any valid SQL statement such as select, insert, or delete.
 Example: select client_no, name, city, pincode from client_master where
pincode=4000054 or pincode=4000057;

 Combining the AND and OR Operator


 The AND and OR conditions can be combined in a single SQL statement.
 It can be used in any valid SQL statement such as select, insert, or delete.

 The NOT Operator


 The Oracle engine will process all rows in a table and display only those
records that do not satisfy the condition specified.
 Example: select * from client_master where not ( city=’Bombay’ or
city=’Delhi’ );

 Comparison Operators
 Comparison operators are used in condition to compare one expression with other.
The comparison operators are =, <, >, >=, <=, !=, between, like, is null and in
operators.

GOVERNMENT POLYTECHNIC, PORBANDAR 21


DATABASE MANAGEMENT SYSTEM (3330703)

 Between operator:
 Between operator is used to check between two values or specific range.
 Example:
select * from salesman_master where salary between 5000 and 8000;
 The above select statement will display only those rows where salary of
salesman is between 5000 and 8000.

 In operator:
 The IN operator can be used to select rows that match one of the values in a
list.
 Example:
select * from client_master where client_no in(C00001, C00003);
 The above query will retrieve only those rows where client_no is either in
C00001 or C00003

 Like operator:
 Like operator is used to search character pattern.
 The like operator is used with special character % and _(underscore)
 Example:
select * from client_master where city like ‘ b% ’;
 The above select statement will display only those rows where city is start
with ‘b’ followed by any number of any characters.
 % sign is used to refer number of character (it is similar to * asterisk wildcard
in DOS).
 While _(underscore) is used to refer single character.
 Example:
select * from client_master where name like ‘_ahul’;

GOVERNMENT POLYTECHNIC, PORBANDAR 22


DATABASE MANAGEMENT SYSTEM (3330703)

EXERCISE:-
Create table described below:-

Table Name : Emp

Column Name Data Type Size


Eid Char 4
Ename Varchar2 10
Birthdate Date --
Salary Number 7
City Char 10

Insert data for Emp table:

Eid Ename Birthdate Salary City


E01 Tulsi 26-Jan-82 12000 Ahmedabad
E02 Gopi 15-Aug-83 15000 Anand
E03 Rajshree 31-Oct-84 20000 Vadodara
E04 Vaishali 23-Mar-85 25000 Surat
E05 Laxmi 14-Feb-83 18000 Anand
E06 Shivani 05-Sep-84 20000 Surat

Exercise on altering table structure, deleting table structure and renaming a table:-

1) Display birthdate for employees having id ‘E01’ and ‘E03’.

2) List all employees having salary either 12000 or 20000.

GOVERNMENT POLYTECHNIC, PORBANDAR 23


DATABASE MANAGEMENT SYSTEM (3330703)

3) List all employees who lives in city ‘Ahmedabad’ or ‘Surat’.

4) List all employees who stay in cities having ‘d’ as a last character.

5) List all employees whose name ends with ‘ali’.

GOVERNMENT POLYTECHNIC, PORBANDAR 24


DATABASE MANAGEMENT SYSTEM (3330703)

6) List all employees whose name starts with ‘T’ or ‘V’ and ends with ‘i’.

7) List ename and salary of those employees having salary between 10000 and
20000.

8) List eid, ename and city of those employees who lives in city ‘Ahmedabad’ or
‘Surat’ or ‘Vadodara’.

GOVERNMENT POLYTECHNIC, PORBANDAR 25


DATABASE MANAGEMENT SYSTEM (3330703)

PRACTICAL: 6
AIM: DEMONSTRATE VARIOUS DATE FUNCTIONS.
 DATE FUNCTION

 ADD_MONTHS()
 It returns the date after adding the number of months specified within the
function.
 Syntax: ADD_MONTHS(date,n)

 MONTHS_BETWEEN
 It returns the date the number of months between the specified dates.
 Syntax: MONTHS_BETWEEN(date1,date2)

 Round()
 This function round a date in specified format.
 Syntax: ROUND(date,format)

 NEXT_DAY
 It returns the date of the first weekday named by ‘char’ that is after the date
named by ‘date’. ‘char’ must be day of the week.
 Syntax: NEXT_DAY(date,’char’)

 TRUNC()
 This function trunc a date in specified format.
 Syntax: TRUNC(date,format)

 GREATEST()
 It displays the latest date from a list of dates.
 Syntax: GREATEST (date1, date2, date3 …)

 NEW_TIME()
 It display the date in the required time zone.
 Syntax: NEW_TIME (date,currenttimezone,newtimezone)

GOVERNMENT POLYTECHNIC, PORBANDAR 26


DATABASE MANAGEMENT SYSTEM (3330703)

EXERCISE:-
Write output of following queries:-

1) select ADD_MONTHS (’04-jun-2009’,2) from dual;

2) select MONTHS_BETWEEN (’02-FEB-09’,’02-JAN-09’) from dual;

3) select ROUND(sysdate,’year’) from dual;

4) select NEXT_DAY(‘04-FEB-09’,’FRIDAY’) from dual;

5) select trunc(sysdate,’year’) from dual;

6) select GREATEST(’02-FEB-09’, ’02-JAN-09’) from dual;

7) select NEW_TIME(sysdate,'EST','HST') from dual;

GOVERNMENT POLYTECHNIC, PORBANDAR 27


DATABASE MANAGEMENT SYSTEM (3330703)

PRACTICAL: 7
AIM: DEMONSTRATE VARIOUS NUMERIC FUNCTIONS.

 NUMERIC FUNCTIONS
 ABS()
 It returns the absolute value.
 Syntax: ABS(n)

 CEIL()
 It returns the smallest integer that is greater than or equal to a specific value.
 Syntax: CEIL(n)

 COS()
 It returns the cosine of a given value.
 Syntax: COS(n)

 COSH()
 It returns hyperbolic cosine of a given value.
 Syntax: COSH(n)

 EXP()
 It returns e raised to the nth power, where e=2.71828183.
 Syntax: EXP(n)

 FLOOR()
 It returns the greatest integer that is less than or equal to a specific value.
 Syntax: FLOOR(n)

 POWER()
 It returns the value raised to a given positive exponent.
 Syntax: POWER(value, exponent)

 MOD()
 It divides a value by a divisor and returns the remainder.
 Syntax: MOD(value, divisor)

 ROUND()
 It rounds a number to given number of digits of precision
 Syntax: ROUND(value, precision)

 TRUNC()
 It truncates digits of precision from a number
 Syntax: TRUNC(value, precision)

 SORT()
 It returns the square root given number
 Syntax: SQRT(n)

GOVERNMENT POLYTECHNIC, PORBANDAR 28


DATABASE MANAGEMENT SYSTEM (3330703)

EXERCISE:-
Write output of following queries:-

1) select ABS(-15) from dual;

2) select CEIL(1.3) from dual;

3) select CEIL(2) from dual;

4) select COS(45) from dual;

5) select COSH(45) from dual;

6) select EXP(5) “Exponent” from dual;

7) select FLOOR(1.3) from dual;

8) select FLOOR(-2.3) from dual;

9) select POWER(3,2) from dual;

GOVERNMENT POLYTECHNIC, PORBANDAR 29


DATABASE MANAGEMENT SYSTEM (3330703)

10) select POWER(64,0.5) from dual;

11) select MOD(100,10) from dual;

12) select MOD(10,3) from dual;

13) select ROUND(66.666,2) from dual;

14) select TRUNC (66.666,2) from dual;

15) select SQRT(64) from dual;

GOVERNMENT POLYTECHNIC, PORBANDAR 30


DATABASE MANAGEMENT SYSTEM (3330703)

PRACTICAL: 8
AIM: DEMONSTRATE VARIOUS CHARACTER FUNCTIONS.
 CHARACTER FUNCTIONS

 INITCAP()
 Returns a string with the first letter of each word in UPPER CASE.
 Syntax: INITCAP(char)

 LOWER()
 It takes any string or column and converts it into lowercase
 Syntax: LOWER(string)

 UPPER()
 It takes any string or column and converts it into upper case
 Syntax: UPPER(string)

 LTRIM()
 It removes character from the left of char with initial characters removed up to
the first character not in set.
 Syntax: LTRIM(char, set)

 RTRIM()
 It returns char with final character removed after the last character not in the
set. ’set’ is optional, it defaults to spaces.
 Syntax: RTRIM(char, set)

 TRANSLATE()
 Replace a sequence of character in a string with another set of character.
 Syntax: TRANSLATE(string1,string to replace, replacement string)

 REPLACE()
 It replaces a character in a string with zero or more character.
 Syntax: REPLACE(string1, character to be replaced, characters)

 SUBSTRING()
 It returns a substring from a given string.
 It returns a portion of char, beginning at character m exceeding up to n
characters.
 Syntax: SUBSTR(string)

GOVERNMENT POLYTECHNIC, PORBANDAR 31


DATABASE MANAGEMENT SYSTEM (3330703)

EXERCISE:-
Write output of following queries:-

1) select INITCAP('rahul kumar') from dual;

2) select LOWER(‘RAHUL’) from dual;

3) select UPPER(‘rahul’) from dual;

4) select LTRIM(‘RAHUL’,’R’) from dual;

5) select RTRIM(‘RAHUL’,’L’) from dual;

6) select TRANSLATE(‘1sct523’,’123’,’7a9’) from dual;

7) select REPLACE(‘Hello’, ’o’, ’rec’ ) from dual;

8) select SUBSTR(‘SECURE’,3,4) from dual;

GOVERNMENT POLYTECHNIC, PORBANDAR 32


DATABASE MANAGEMENT SYSTEM (3330703)

PRACTICAL: 9
AIM: DEMONSTRATE VARIOUS CONVERSION FUNCTIONS.
 CONVERSION FUNCTIONS

 TO_CHAR()
 It converts a value of DATE data type to CHAR value. It accept a date as well
as the format in which the date has to appear.
 The format must be a date format
 Syntax: TO_CHAR(date, format)

 TO_DATE()
 It converts a CHAR filed to a DATE filed.
 Syntax: TO_DATE (‘char’, format)

 TO_NUMBER()
 It converts a character value containing a number to a value of NUMBER data
type.
 Syntax: TO_NUMBER(’char’)

 MISCELLANEOUS FUNCTIONS

 UID
 It returns an integer that uniquely identifies the session user.
8
 USER
 It returns the name by which the current user is known to Oracle.

 NVL
 It stands for Null value Substitution.
 Syntax: NVL(value, substitute)
 If the value is NULL, this function is equal to substitute.
 If the value is not NULL, this unction is equal to value.
 Example:
Table name: Shipping

Client Weight
Johnson tools 59
Inf Software 27
Peterson Industries NULL

Select NVL (weight, 43) from shipping;

Client Weight
Johnson tools 59
Inf Software 27
Peterson Industries 43

GOVERNMENT POLYTECHNIC, PORBANDAR 33


DATABASE MANAGEMENT SYSTEM (3330703)

 In the above output, the NVL function replaces the value specified in
wherever it encounters a NULL value. Hence, 43 is inserted for client Peterson
Industries.

 VSIZE
 It returns the number of bytes in the internal representation of an expression.
 Syntax: VSIZE(expression)

GOVERNMENT POLYTECHNIC, PORBANDAR 34


DATABASE MANAGEMENT SYSTEM (3330703)

EXERCISE:-
Write output of following queries:-

1) select TO_CHAR(SYSDATE,’DD-MM-YY’) from dual;

2) select TO_DATE(‘26/08/09’,’DD/MM/YY’) from dual;

3) select TO_NUMBER(‘100’) from dual;

4) select UID from dual;

5) select USER from dual;

6) select VSIZE(‘SCT on the net’) from dual;

GOVERNMENT POLYTECHNIC, PORBANDAR 35


DATABASE MANAGEMENT SYSTEM (3330703)

PRACTICAL: 10
AIM: DEMONSTRATE VARIOUS GROUP FUNCTIONS.
 GROUP FUNCTIONS

 AVG()
 It returns average value of the specified column, ignoring NULL values.
 Syntax: AVG(Column name)

 MIN()
 It returns the minimum value in the specified column.
 Syntax: MIN(column name)

 MAX()
 It returns the maximum value in the specified column.
 Syntax: MAX(column name)

 SUM()
 It returns the sum of values of the specified column.
 Syntax: SUM(column name)

 COUNT()
 It returns the number of rows in specified column or the total number of rows
in the table.
 Syntax: COUNT(column name)
 Syntax: COUNT(*)

 DECODE()
 It is transformation function that does an orderly value-by-value substitution.
 Syntax: DECODE(string,if1,then1,if2,then2,…..,else)

GOVERNMENT POLYTECHNIC, PORBANDAR 36


DATABASE MANAGEMENT SYSTEM (3330703)

EXERCISE:-
Write output of following queries:-

1) select AVG(sell_price) from product_master;

2) select MIN(sell_price) from product_master;

3) select MAX(sell_price) from product_master;

4) select SUM(salary) from salesman_master;

5) select COUNT(salesman_no) from salesman_master;

6) select COUNT(*) from salesman_master;

7) select DECODE(‘SATUR’, ’SUN’, ’HOLIDAY’, ’SATUR’, ’HALF DAY’,


’FULL DAY’) from dual;

GOVERNMENT POLYTECHNIC, PORBANDAR 37


DATABASE MANAGEMENT SYSTEM (3330703)

PRACTICAL: 11
AIM: PERFORM QUERIES USING GROUP BY, HAVING AND
ORDER BY CLAUSE.

 GROUP BY

 GROUP BY clause is another section of the select statement


 This optional clause tells Oracle to group rows based on distinct values that exist
for specified columns.
 The GROUP BY clause creates a data set, containing several sets of records
grouped together based on a condition
 Syntax:
Select <column name1>,<column name2>, <column nameN>
AGGREGATE_FUNCTION (<Expression>)
From Table Name WHERE <condition>
GROUP BY<column name1>,<column name2>,<column nameN>;
 Example:
SELECT branch_no“branch_no”,COUNT(emp_no)”No Employees”
From emp_master GROUP BY branch_no;

 HAVING

 HAVING Clause can be used in conjunction with the GROUP BY clause


HAVING imposed a condition on the GROUP BY clause.
 which further filter the group created by the groups by clause
 Syntax:
Select <column name1>,<column name2>, <column nameN>
AGGREGATE_FUNCTION (<Expression>)
From Table Name WHERE <condition>
GROUP BY<column name1>,<column name2>,<column nameN>;
HAVING <condition>
 Example:
Select product_no,sum (qty_ordered) “total QTY ordered”
From sales_order GROUP BY product_no
HAVING product_no=’P00001’ or product_no=’P00004’
 HAVING clause is used to display specified row on screen.

 ORDER BY

 ORDER BY is use want the information it return sorted in the order specify.
 Syntax:
Select <column name1>,<column name2>, <column nameN>
From Table Name Where <condition>
ORDER BY <column name1>;
 Example:
Select feature, section, page from NEWSPAPER where section=’F’
ORDER BY feature;

GOVERNMENT POLYTECHNIC, PORBANDAR 38


DATABASE MANAGEMENT SYSTEM (3330703)

EXERCISE:-
Create the tables described below:-

Table Name : EMPLOYEE

Column Name Data Type Size


Emp-no Number 4
Ename Varchar2 10
Salary Number 6
Join_date Date --
Deptno Number 2

Insert data for EMPLOYEE table:

Emp_no Ename Salary Join_date Deptno


101 Jiya 10000 12-DEC-90 10
102 Rohit 15000 15-NOV-94 20
103 Dinesh 7000 10-JAN-89 10
104 Chhaya 12000 25-OCT-98 30
105 Rima 14000 21-JUL-88 20
106 Payal 8000 17-MAR-90 10

For above table solve following queries using group by, having and order by:

1) Display department wise salary total.

GOVERNMENT POLYTECHNIC, PORBANDAR 39


DATABASE MANAGEMENT SYSTEM (3330703)

2) Display salary total of all employees who works in department number 10.

3) Sort the data of EMPLOYEE table based on salary column in descending order.

4) Display ename and salary of employees based on name in alphabetical order.

GOVERNMENT POLYTECHNIC, PORBANDAR 40

You might also like