Uploaded by alahmadi1999pay

1.Lab-7

advertisement
Lab 7 Data Retrieval
(continuation)
Term2
Spring 2017
Statement purpose
Discussions about how to retrieve data from single table with suitable examples.
Activity Outcomes
students will understand how to retrieve required data from table.
Instructor Note:
solve the exercise and submit .
Notes
SQL statement (select statements) are used for data retrieval.SQL statements are not case
sensitive.SQL statements can be on one or more lines. Keywords cannot be abbreviated or split
across lines. Clauses are usually placed on separate lines. Indents are used to enhance
readability.
Procedure
Using Like Operator
Select ename from emp where ename like‘A%’;
The result of the above query is to list all the names of all employees whose name starts
with ‘A’;
List all the names of all employees whose name ends with ‘S’
Select ename from emp where ename like‘%S’;
CPCS-241 - The Lab Note
Lab-7
Term2
Spring 2017
Lab 7 Data Retrieval
(continuation)
Lists all the employees names whose name starts with ‘A’ and ends with ‘S’.
SELECT ENAME FROM EMP WHERE ENAME LIKE’A%S’;
Using Null;
list all the employee names and their salary who have no commission.
Select ename, sal, comm from emp where comm is null;
CPCS-241 - The Lab Note
Lab-7
Lab 7 Data Retrieval
(continuation)
Term2
Spring 2017
Character Functions
Case Manipulation Functions
These functions convert case for character strings.
Result
Function
sql course
LOWER('SQL Course')
SQL COURSE
UPPER('SQL Course’)
Sql Course
INITCAP('SQL Course')
Select lower(‘SQL Course’) from dual;
Dual is a dummy table created by oracle with data dictionary. It has only one row and
one column. It is used to evaluate expressions.
Character Manipulation Functions
CPCS-241 - The Lab Note
Lab-7
Lab 7 Data Retrieval
(continuation)
Term2
Spring 2017
These functions manipulate character strings:
Result
HelloWorld
Hello
`
10
6
*****24000
24000*****
elloWorld
Function
CONCAT('Hello', 'World')
SUBSTR('HelloWorld',1,5)
LENGTH('HelloWorld')
INSTR('HelloWorld', 'W')
LPAD(salary,10,'*')
RPAD(salary, 10, '*')
TRIM('H' FROM 'HelloWorld')
Example:
Select concat(‘hello’,’world’) from dual;
Exercise:
Implement above functions in SQL.
Number Functions
ROUND: Rounds value to specified decimal
45.93
ROUND(45.926, 2)
TRUNC: Truncates value to specified decimal
45.92
TRUNC(45.926, 2)
MOD: Returns remainder of division
100
MOD(1600, 300)
Exercise:
Implement all above functions in SQL.
Arithmetic with Dates
Add or subtract a number to or from a date for a resultant date value.
Subtract two dates to find the number of days between those dates.
Add hours to a date by dividing the number of hours by 24.
Operation
Date + number
CPCS-241 - The Lab Note
`
Result
Date
Lab-7
Lab 7 Data Retrieval
(continuation)
Term2
Spring 2017
(Adds a number of days to a date)
date - number
(Subtracts a number of days from a date )
date - date
(Subtracts one date from another)
date + number/24
(Adds a number of hours to a date)
Date
Number of days
Date
Exercise
Implement the above functions in SQL.
Date Functions
Function
Description
MONTHS_BETWEEN
ADD_MONTHS
NEXT_DAY
LAST_DAY
ROUND
TRUNC
Number of months between two dates
Add calendar months to date
Next day of the date specified
Last day of the month
Round date
Truncate date
CPCS-241 - The Lab Note
Lab-7
Lab 7 Data Retrieval
(continuation)
Term2
Spring 2017
Exercise
Implement the above function in SQL.
Conversion Functions
Implicit Data-Type Conversion
For assignments, the Oracle server can automatically
Convert the following:
From
To
VARCHAR2 or CHAR
VARCHAR2 or CHAR
NUMBER
DATE
NUMBER
DATE
VARCHAR2
VARCHAR2
For expression evaluation, the Oracle Server can automatically convert the following:
From
To
VARCHAR2 or CHAR
VARCHAR2 or CHAR
NUMBER
DATE
Using the TO_CHAR Function with Dates
TO_CHAR(date, 'format_model')
The format model:
• Must be enclosed in single quotation marks and is case sensitive
• Can include any valid date format element
• Is separated from the date value by a comma
Elements of the Date Format Model
YYYY
YEAR
MM
MONTH
MON
DAY
DD
Full year in numbers
Year spelled out
Two-digit value for month
Full name of the month
Three-letter abbreviation of the month
Full name of the day of the week
Numeric day of the month
CPCS-241 - The Lab Note
Lab-7
Lab 7 Data Retrieval
(continuation)
Term2
Spring 2017
Using the TO_CHAR Function with Dates
SELECT ENAME,TO_CHAR(hiredate, 'fmDD Month YYYY') HIREDATE
FROM EMP;
Using the TO_NUMBER and TO_DATE Functions
Convert a character string to a number format using the TO_NUMBER function:
TO_NUMBER(char[,'format_model'])
Convert a character string to a date format using the TO_DATE function:
TO_DATE(‘char’, 'format_model')
Nesting Functions
Single-row functions can be nested to any level.
• Nested functions are evaluated from deepest level to the least deep level.
F3(F2(F1(col,arg1),arg2),arg3)
SELECT ENAME,
NVL(TO_CHAR(MGR), 'No Manager')
FROM EMP
WHERE MGR IS NULL;
Lists the names of employees who has no manager.
SELECT ENAME,
NVL(TO_CHAR(MGR), 'No Manager')
FROM EMP WHERE MGR IS NULL;
CPCS-241 - The Lab Note
Lab-7
Term2
Spring 2017
Lab 7 Data Retrieval
(continuation)
NVL Function
Converts a null to an actual value
• Data types that can be used are date, character and number.
• Data types must match:
– NVL(comm,0)
– NVL(joindate,'01-JAN-97')
– NVL(job,'No Job Yet')
SELECT ENAME, sal, NVL(comm, 0),
SAL*12 ANNUAL_SALARY FROM EMP;
23
Conditional Expressions
• Give you the use of IF-THEN-ELSE logic within a
SQL statement.
CPCS-241 - The Lab Note
Lab-7
Lab 7 Data Retrieval
(continuation)
Term2
Spring 2017
• Use two methods:
– CASE expression
– DECODE function
The CASE Expression
Facilitates conditional inquiries by doing the work of an IF-THEN-ELSE statement:
CASE expr WHEN comparison_expr1 THEN return_expr1
[WHEN comparison_expr2 THEN return_expr2
WHEN comparison_exprn THEN return_exprn
ELSE else_expr]
END
SELECT ENAME, job, SAL,
CASE job WHEN ‘SALESMAN’ THEN 1.10*sal
WHEN ‘ANALYST’ THEN 1.15*sal
WHEN ‘CLERK’ THEN 1.20*SAL
ELSE SAL
END "REVISED_SAL"
FROM EMP;
The DECODE Function
Facilitates conditional inquiries by doing the work of
a CASE or IF-THEN-ELSE statement:
DECODE(col|expression, search1, result1
[, search2, result2,...,]
[, default])
CPCS-241 - The Lab Note
Lab-7
Lab 7 Data Retrieval
(continuation)
Term2
Spring 2017
SELECT ENAME, JOB, SAL,
DECODE(JOB, ‘SALESMAN’, 1.10*SAL,’ANALYST’, 1.15*SAL,
‘CLERK’, 1.20*SAL, SAL) REVISED_SALARY FROM EMP;
Set operators which combine the results of two queries into a single result are listed below
Operator
Description
Example
UNION
Returns all distinct rows selected by either
query.
SELECT * FROM
(SELECT ENAME FROM EMP WHERE JOB =
'CLERK'
UNION
SELECT ENAME FROM EMP WHERE JOB =
'ANALYST');
UNION ALL Returns all rows selected by either query,
SELECT * FROM
including all duplicates.
(SELECT SAL FROM EMP WHERE JOB =
'CLERK'
UNION ALL
SELECT SAL FROM EMP WHERE JOB =
'ANALYST');
INTERSECT Returns all distinct rows selected by both
CPCS-241 - The Lab Note
SELECT * FROM orders_list1
Lab-7
Lab 7 Data Retrieval
(continuation)
Term2
Spring 2017
Operator
Description
Example
queries
This example of INTERSECT produces all rows
that are in both tables T1 and T2, removing
duplicates.
(SELECT * FROM T1)
INTERSECT
(SELECT * FROM T2)
INTERSECT
SELECT * FROM orders_list2;
.
MINUS
Returns all distinct rows selected by the first
query but not the second.
SELECT * FROM (SELECT SAL FROM
EMP WHERE JOB = 'PRESIDENT'
MINUS
SELECT SAL FROM EMP WHERE JOB =
'MANAGER');
CPCS-241 - The Lab Note
Lab-7
Download