You are on page 1of 5

LAB NO.

5
SQL: Single Row Functions
OBJECTIVE:

1. Practice Single Row queries.

DESCRIPTION:

1. Number Functions
You can manipulate retrieved numbers by rounding up to specific
precessions or perform extra calculation functionalities. SQL has several
number functions that you can use to manipulate retrieved numbers. The
following are list of functions and example of each:

Function Description Example Result


SELECT ABS (price- If (price-cost) =
Returns the absolute value of a
ABS(number) cost) 3.5
number Result is 3.5
FROM product;
SELECT CEIL (price-
cost) If (price-cost) =
Rounds up the number to next
CEIL(number) FROM product 3.5
integer
WHERE product_id Result is 4
=1;
SELECT
FLOOR(price-cost) If (price- cost) =
Rounds down the number to
FLOOR(number) FROM product 3.5
next integer
WHERE product_id Result is 3
=1;
SELECT MOD
(price,2)
MOD(number, Returns the remainder of a If price=3
FROM product
divisor) division Result is 1
WHERE product_id
=1;
POWER(number, Returns result of number raised SELECT If price = 3
power) specified power POWER(price,2) Result is 9
FROM product
WHERE product_id
=1;
SELECT SIGN(price- If (price-cost) =
SIGN(number) cost) 3.51
Tests if the number negative or Result is -1
FROM product
positive and returns 1 or – 1
WHERE product_id
=1;
SELECT
ROUND(price-cost, 2) If (price-cost) =
ROUND(number, Returns the a number rounded
FROM product 3.5121212
precision) to the specified precision
WHERE product_id Result is 3.51
=1;
SELECT SQRT(price)
Returns the square root of a FROM product If (price) = 4
SQRT(number)
number WHERE product_id Result is 2
=1;
SELECT
TRUNC(price-cost, 1) If (price-cost) =
TRUNC(number, Returns a number truncated to
FROM product 3.5133
precision) the specified precision
WHERE product_id Result is 3.5
=1;

2. Single-Row Character Manipulation Functions

Function Description Example Result


If
(first_name='Joh
SELECT CONCAT n',
Concatenates two
CONCAT(string1, string2) (first_name, last_name) last_name='Morg
strings
FROM employees; an')
Result is
JohnMorgan
If
Returns string with SELECT INITCAP
(first_name='john
INITCAP(string) first letter in upper (first_name)
')
case FROM employees;
Result is John
If
Returns an integer SELECT
(first_name='Joh
LENGTH(string) representing the LENGTH(first_name)
n')
string length FROM employees;
Result is 4
LPAD(string,padding_char) Returns the string SELECT LPAD (price,
If price =100
padding characters 7,'*')
FROM product
added to the left Result is ****100
WHERE product_id =1;
SELECT LPAD (price,
Returns the string
7,'*') If price =100
RPAD(string,padding_char) padding characters
FROM product Result is 100****
added to the right
WHERE product_id =1;
LTRIM(string,searchString) Removes characters SELECT LTRIM If (first_name='
RTRIM(string,searchString) from left/right of (first_name, 'j') John')
char FROM employees Result is 'ohn'
WHERE employee_id
=1;
REPLACE returns SELECT
char with every REPLACE(first_name,'J If
REPLACE(string,searchString,r occurrence of ','H') (first_name='Joh
eplacement) searchString FROM employees n')
replaced with WHERE employee_id Result is 'Hohn'
replacementString =1;
SELECT
SUBSTR(first_name,'1', If
Returns substring
'3') (first_name='Joh
SUBSTR(string,start,length) starting at start and
FROM employees n')
of specified length
WHERE employee_id Result is 'Joh'
=1;
SELECT
If
Returns string with UPPER(first_name)
UPPER(string) (first_name='Joh
all upper/lower case FROM employees
LOWER(string) n')
characters WHERE employee_id
Result is 'JOHN'
=1;

LAB TASK

Sample Data
1. Display ename, salary and concated result of deptno and empno from
table EMP where salary is less than 2500 or job is equal to MANAGER.
2. Display first 3 characters of ename, deptno and job from table EMP where
second character of ename is ‗A‘ and deptno is 30 or job is SALESMAN.
3. Right Append dollar sign to make salary 8 digit value and calculate
remainder of annual salary from monthly salary and rename it as
REMAINDER where ename is KING and salary is greater than 1500 from
table EMP.
4. Round 345.6665 upto 2 digits from table DUAL.
5. Write a query that displays enames with first letter capitalized and all
other letters in lowercase and length of enames whose enames start with J,
A or N.
Execute these queries and analyze the results.

1. Select ‗The job id for‘ || UPPER(ename) || ‗is‘ || LOWER (job) AS


―Employee Details‖ FROM emp;
2. Select empno, ename, deptno from emp where LOWER(ename) =
‗smith‘;
3. Select empno, UPPER(ename), deptno from emp where
INITCAP(ename) = ‗Martin‘;
4. Select empno, CONCAT(ename,job), LENGTH(ename), INSTR(ename,
‗a‘) from emp where SUBSTR (job,5) = ‗MAN‘;
5. Select empno, CONCAT(ename,job), LENGTH(ename), INSTR(ename,
‗a‘) from emp where SUBSTR (ename,-1,1) = ‗S‘;
6. Select ROUND (45.698,2), ROUND (45.987,0), ROUND (45.369,-1)
from DUAL;
7. Select TRUNC (67.765, 2), TRUNC(67.876), TRUNC(67.567, -2) from
DUAL;
8. Select ename, sal, MOD(sal, 5000) FROM emp WHERE job =
‗SALESMAN‘;
9. Select ename, hiredate from emp where ename LIKE ‗S%‘;
10. Select SYSDATE from DUAL;
11. Select aname, (SYSDATE – hiredate)/7 WEEKS from emp where deptno
= 10;
12. Select empno, hiredate, MONTHS_BETWEEN( SYSDATE, hiredate)
Tenure,ADD_MONTHS(hiredate, 6) Review, NEXT_DAY (hiredate,
‗FRIDAY‘),
LAST_DAY(hiredate) from emp where
MONTHS_BETWEEN(SYSDATE, hiredate) >
36;
13. Select empno, hiredate, ROUND (hiredate, ‗MONTH‘), TRUNC
(hiredate, ‗MONTH‘) from emp WHERE hiredate LIKE ‗%97‘;

DELIVERABLES:

1. SingleRowQueries.txt.
2. Screenshots of results achieved.

You might also like