B48 SQL Exp2
B48 SQL Exp2
Theory: -
Number Functions
- ROUND: rounds value to specified decimal
SELECT ROUND (45.923, 2), ROUND (45.923, 0) ,
ROUND (45.923, -1)
FROM DUAL;
ROUND(45.923,2) ROUND (45.923, 0) ROUND (45.923, -1)
45.92 46 50
DUAL is a dummy table
- TRUNC: truncates value to specified decimal
SELECT TRUNC (45.923, 2), TRUNC (45.923),
TRUNC (45.923, -2)
FROM DUAL;
TRUNC(45.923 , 2) TRUNC (45.923) TRUNC(45.923,-2)
45.92 45 0
- MOD: returns remainder of division
SELECT last-name, salary, MOD (salary, 5000)
FROM employees
WHERE job_id = „SA_REP‟;
LAST_NAME SALARY MOD(SLARY, 5000)
Abel 1100 1000
Taylor 6600 3600
Grant 7000 2000
Date Functions
- MONTHS_BETWEEN: Number of months between two dates
MONTHS_BETWEEN („01-SEM-95‟, ‟11-JAN-94‟)
ANS: 19.6774194
- ADD_MONTHS: Add calendar months to date
ADD_MONTHS (‟11-JAN-94‟, 6)
ANS: ’11-Jul-94’
- NEXT_DAY: Next day of month
NEXT_DAY („01-SEP-95‟, „FRIDAY’)
ANS: ’08-SEP-95’
- LAST_DAY: Last day of the month
LAST_DAY (‟01-FEB-95‟)
ANS: ’28-FEB-95’
- ROUND: Round date
Assume SYSDATE= ‟25-JUL-95‟:
ROUND (SYSDATE, „MONTH‟)
ANS: 01-AUG-95
- TRUNC: Truncate date
TRUNC (SYSDATE, „YEAR‟)
ANS: 01-JAN-95
Conversion Functions
Data type conversion
- from number to character
General Functions
These functions work with any data type and pertain to using nulls.
- NVL (expr1, expr2)
- NVL2 (expr1, expr2, expr3)
- NULLIF (expr1, expr2)
- COALESCE ( expr1, expr2, …., exprn)
Others are Conditional Expressions:
- Use of IF-THEN-ELSE logic within a SQL statement
- Use two methods: CASE expression and DECODE function
Conclusion:
This practical covers topics:
- Selecting, restricting, and sorting data.
- Perform calculations on data using various functions.
LAB ASSIGNMENT -2
1. Create query to display the last name and salary for all employees whose salary
is not in the range of $5,000 and $12,000.
2. Display employees last name, job ID, and start date of employees hired between
February 20, 1998, and May 1, 1998. Order the query in ascending order by
start date.
3. Display the last name and department number of all employees in departments 20
and 50 in alphabetical order by name.
4. Display the last name, salary, and commission for all employees who
earn commissions. Sort data in descending order of salary and
commissions.
5. Display the last name, job, and salary for all employees whose job is sales
Representative or stock clerk and whose salary is not equal to $2, 500, $3,500, or
$7,000.
6. Write a query to display the current date. Label the column Date.
7. For each employee, display the employee number, last name, salary, and salary
increased by 15% and expressed as a whole number.
8. Write a query that displays the employee‟s last names with the first letter
capitalized and all other letters lowercase, and length of the names, for all
employees whose name starts with J,A, or M. Give each column an appropriate
label. Sort the results by the employees last names.