Lab2 Part#2_Prepared by TA.Jaber Taradeh

advertisement
TA. Jaber Taradeh
Title: Database Management System Lab
Lab Two_Part#2
 BUILT IN FUNCTIONS IN SQL

CHARACTER/STRING FUNCTION
1. Upper case, lower case, initcap case function : return UPPERCASE,lower
case, Capitalize Each Word

SYANTX:
Select upper (col1), lower (col2), col3... From <Table Name>;

EXAMPLE:
Select upper(ename),lower(job),empno from emp;
Select initcap (ename),lower(job),empno from emp;
2. LTRIM, RTRIM Function: LTRIM function removes all specified
characters from the left-hand side of a string, RTRIM function removes all
specified characters from the right-hand side of a string,

SYANTX:
Select ltrim(col1, specified characters) FROM Table name;
Select rtrim(col1, specified characters) FROM Table name;

EXAMPLE:
select LTRIM(ename,'A') from FROM where empno=10;
select RTRIM(ename,'d') from FROM where empno=10;
3. CONCAT Function: Returns a string that is the result of concatenating two
or more string values.

SYANTX:
select concat(col,' sequence of characters ') from Table name;

EXAMPLE:
select concat(ename,' al_quds') FROM emp where empno=10;
select concat(ename,' Student') FROM emp;
4. LENGTH Function: Returns the number of bytes used to represent any
expression.

SYANTX:
select length(data |col) FROM Table name;

EXAMPLE:
select length(ename) FROM emp where empno=10;
14 | P a g e
Manual Of Database Lab
TA. Jaber Taradeh
Title: Database Management System Lab
5. REPLACE Function: Replaces all occurrences of a specified string value
with another string value.

SYANTX:
Select replace (string_expression, string pattern,
string_replacement) FROM Table name;

EXAMPLE:
Select replace (ename, 'Ah','moham') from emp where empno=
10;
6. SUBSTRING Function: Returns part of a character, binary, text, or image
expression in SQL Server.

SYANTX:
Select substr( expression, start, length) FROM Table name;

EXAMPLE:
Select substr(ename,2,3)from emp ;

DATE & TIME FUNCTION
1. SYSTEMDATE Function: Returns a date value that contains the date of the
computer.

SYANTX:
Select sysdate FROM Table name;
select last_day(sysdate) FROM Table name;
select add_months(sysdate,# of month) FROM Table name;
select next_day(sysdate,day) FROM Table name;

EXAMPLE:
Select sysdate from emp ;

NUMERIC FUNCTION
1. ROUND Function: Returns a numeric value, rounded to the specified length
or precision.

SYANTX:
Select ROUND (numeric_expression , length [ ,function ]
)FROM Table name;

EXAMPLE:
select round(15.6789)from emp;
15 | P a g e
Manual Of Database Lab
TA. Jaber Taradeh
Title: Database Management System Lab
2. CEILING Function: Returns the smallest integer greater than, or equal to,
the specified numeric expression..


SYANTX:
Select ceil ( numeric_expression ) FROM Table name;
EXAMPLE:
select ceil (12.25 ) from emp;
3. ABS Function: returns the absolute (positive) value of the specified numeric
expression.

SYANTX:
Select ABS ( numeric_expression ) ROM Table name;


EXAMPLE:
select abs (-12 ) from emp;
MATH FUNCTION:
1. ABS Function: returns the absolute (positive) value of the specified numeric
expression.

SYANTX:
Select ABS ( numeric_expression ) ROM Table name;

EXAMPLE:
select abs (-12 ) from emp;
2. POWER Function: Returns the value of the specified expression to the
specified power..

SYANTX:
Select POWER ( float_expression , y ) ROM Table name;

EXAMPLE:
select power(10,2) from emp;
3. MOD Function: returns the remainder value of specified expression to the
specified value.

SYANTX:
Select MOD ( int_expression , x) ROM Table name;

EXAMPLE:
select MOD(13,2) from emp;
4. SQRT Function: Returns the square root of the specified float value..

SYANTX:
Select SQRT ( float_expression ) ROM Table name;

EXAMPLE:
select SQRT (100) from emp;
16 | P a g e
Manual Of Database Lab
TA. Jaber Taradeh
Title: Database Management System Lab
 NESTED QUERIES AND JOIN QUERIES
EX. Display all employee names and salary whose salary is greater than minimum salary of the company
and job title starts with M
SOLUTION:
Select ename,salary,job from emp where salary >(select min(salary) from emp where job like 'M%');
select * from emp where deptno=(select deptno from emp where empno=4);

SET OPERATORS
JOINS OPERATOR: Oracle JOINS are used to retrieve data from multiple tables. An Oracle JOIN is
performed whenever two or more tables are joined in a SQL statement.

Oracle INNER JOIN (or sometimes called simple join)

SYANTX:
SELECT columns FROM table1 INNER JOIN table2
ON table1.column = table2.column;

EXAMPLE:
SELECT deptname FROM dept INNER JOIN emp ON
dept.deptno = emp.deptno;

Oracle LEFT OUTER JOIN (or sometimes called LEFT JOIN)

SYANTX:
SELECT columns FROM table1 LEFT OUTER JOIN
table2 ON table1.column = table2.column;
17 | P a g e
Manual Of Database Lab
TA. Jaber Taradeh
Title: Database Management System Lab

EXAMPLE:
SELECT * FROM emp LEFT OUTER JOIN dept ON
emp.deptno = dept.deptno;

Oracle RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)

SYANTX:
SELECT columns FROM table1 RIGHT OUTER JOIN
table2 ON table1.column = table2.column;

EXAMPLE:
SELECT * FROM dept RIGHT OUTER JOIN emp ON
dept.deptno = emp.deptno;

Oracle FULL OUTER JOIN (or sometimes called FULL JOIN)

SYANTX:
SELECT columns FROM table1 FULL OUTER JOIN
table2 ON table1.column = table2.column;

EXAMPLE:
SELECT * FROM emp FULL OUTER JOIN dept ON
emp.deptno = dept.deptno;
18 | P a g e
Manual Of Database Lab
TA. Jaber Taradeh
Title: Database Management System Lab
UNION OPERATOR: Combines the results of two or more queries into a single result set that includes
all the rows that belong to all queries in the union. The UNION operation is different from using joins
that combine columns from two tables.

SYANTX:
{ <query_specification> | ( <query_expression> ) }
UNION [ ALL ]
<query_specification | ( <query_expression> )
[ UNION [ ALL ] <query_specification> | (
<query_expression> )
[ ...n ] ]

EXAMPLES:
Ex1. Display all the deptno available with the dept and emp
tables avoiding duplicates.
Select deptno from emp union select deptno from dept;
Ex2. Display all the deptno available with the dept and emp
tables
select deptno from emp union all select deptno from dept;
Ex3. Display all the deptno available in emp and not in dept
tables and vice versa.
select deptno from dept minus select deptno from emp;
19 | P a g e
Manual Of Database Lab
Download