Introduction To Oracle: SQL
Introduction To Oracle: SQL
Strategy
and
Analysis
Design
Build
and
Document
Transition
Production
Data Storage on Different Media
SALGRADE
DEPT GRADE LOSAL HISAL
DEPTNO DNAME LOC 1 700 1200
10 ACCOUNTING NEW YORK 2 1201 1400
20 RESEARCH DALLAS 3 1401 2000
4 2001 3000
30 SALES CHICAGO
5 3001 9999
40 OPERATIONS BOSTON
a a a a a
a a a a a a a a a a
a a a a a a a a a a
a a a a a a a a a a
a a a a a a a a a a
a a a a a a a a a a
a a a a a
Database
a a a a a
a a a a a
a a a a a
a a a a a
a a a a a
a a a a a
Table1 Selection
Join
Table 1 Table2
Basic SQL Statement
SQL> SELECT *
2 FROM dept;
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
Using Arithmetic Operators
* / + -
ENAME 12*SAL+COMM
KING
Defining a Column Alias
• Renames a column heading.
• Is useful with calculations.
• Immediately follows column name; Optional AS
keyword between column name and alias.
• Requires double quotation marks if it contains
space or special characters or is case sensitive.
Using Column Aliases
KINGPRESIDENT
BLAKEMANAGER
CLARKMANAGER
JONESMANAGER
MARTINSALESMAN
ALLENSALESMAN
…
14 rows selected.
Literal Character Strings
• A literal is a character, a number, or a date included
in the SELECT list.
• Date and Character literal values must be enclosed
within single quotation marks.
• Each character string is output once for each row
returned.
Using Literal Character Strings
SQL> SELECT ename || ‘is a ’ || job
AS “Employees Details”
FROM emp;
Employees Details
KING is a PRESIDENT
BLAKE is a MANAGER
CLARK is a MANAGER
JONES is a MANAGER
MARTIN
… is a SALESMAN
14 rows selected.
Duplicate Rows
The default display of queries is all rows, including
duplicate rows.
SQL> SELECT deptno
FROM dept;
DEPTNO
10
30
10
… 20
14 rows selected.
Eliminating Duplicate Rows
Eliminate duplicate rows by using the DISTINCT
keyword in the SELECT clause.
SQL> SELECT DISTINCT deptno
FROM dept;
DEPTNO
10
20
30
SQL and SQL Plus Interaction
SQL Statements SQL Statements
Buffer
Server
SQL*Plus a a a a a
a a a a a
a a a a a
a a a a a
a a a a a
a a a a a
a a a a a
a a a a a
a a a a a
SQL Statements a a a a a Query Results
a a a a a
a a a a a
Formatted Report
SQL and SQL*Plus
SQL SQL*Plus
• A Language • An environment
• ANSI Standard • Oracle proprietary
• Keyboard cannot be abbreviated • Keyboards can be abbreviated
• Statements manipulated data and • Commands do not allow
table definitions in the database manipulation of values in the
database
• SAVE filename.ext
• GET filename
• START filename.ext
• @ filename
• EDIT filename.ext
• SPOOL filename.ext
• EXIT
Practice 1
1. Initiate a SQL *Plus Session using the user ID and
password give to you.
2. Whether this SELECT statement will execute ?
Select ename, job, sal Salary from emp;
3. Whether this SELECT statement will execute ?
Select * from Salgrade;
4. Whether this SELECT statement will execute ?
Select empno, ename, salary x 12 Annual Sal
From emp;
5. Show the Structure of Dept table. Select all data from
the Dept table.
6. Show the Structure of Emp table. Display 4 columns.
Practice 1
EMP
EMPNO ENAME JOB MGR HIREDATE
7839 KING PRESIDENT 17-Nov-81
7782 CLARK MANAGER 7839 9-Jun-81
7934 MILLER CLERK 7782 23-Jan-82
Using the WHERE Clause
SQL> SELECT ename, job, deptno From Emp
WHERE job=‘CLERK’;
JAMES CLERK 30
SMITH CLERK 20
ADMAS CLERK 20
MILLER CLERK 10
Character Strings and Dates
• Character strings and date values are enclosed in
single quotation marks
• Character values are case-sensitive and date values
are format-sensitive
• Default date format is ‘DD-MON-YY’
Operator Meaning
AND Returns TRUE if both component
conditions are TRUE
OR Returns TRUE if either component
conditions are TRUE
NOT Returns TRUE if the component
conditions are TRUE
Using the AND Operator
AND Operator both conditions to be TRUE
ENAME JOB
KING PRESIDENT
MARTIN SALESMAN
ALLEN SALESMAN
TURNER SALESMAN
WARD SALESMAN
2 NOT
3 AND
4 OR
Single-row Functions :
These functions operate on single rows only and return one
result per row. There are diff. types of single-row functions :
• Character
• Number
• Date
• Conversion
Two Types of SQL Functions (cont…)
Multi-Row Functions :
These functions manipulates groups of rows to give one
Result per group of rows.
Single-Row Functions
• Manipulate data items
• Accept arguments and return one value
• Act on each row returned
• Return one result per row
• May modify the datatypes
• Can be used in SELECT, WHERE, and ORDER BY
• Can be nested
LOWER CONCAT
UPPER SUBSTR
INITCAP LENGTH
INSTR
LPAD
TRIM
Case Conversion Functions
Convert case for Character Strings
Function Result
Lower (‘SNTI Course’) Snti course
Upper (‘snti course’) SNTI COURSE
Initcap (‘SNTI course’) Snti Course
7698 BLAKE 30
Character Manipulation Functions
Manipulate Character Strings
Function Result
Concat (‘World’ , ‘Cup’) World Cup
Substr (‘World Cup’,1,5) World
Length (‘World Cup’) 9
Instr (‘World Cup’, ‘r’) 3
LPAD (sal, 10, ‘*’ ) ******5000
TRIM(‘S’, ‘SSMITH’) MITH
Using Character Manipulation Functions
SQL> SELECT ename, CONCAT (ename, job),
LENGTH (ename), INSTR (ename, ‘A’)
FROM emp
WHERE SUBSTR(JOB,1,5)=‘SALES’;
MARTIN MARTINSALESMAN 6 2
ALLEN ALLENSALESMAN 5 1
TURNER TURNERSALESMAN 6 0
WARD WARDSALESMAN 4 2
Using Character Manipulation Functions
SQL> SELECT ename, CONCAT (ename, job),
LENGTH (ename), INSTR (ename, ‘A’)
FROM emp
WHERE SUBSTR(ENAME, -1, 1) = ‘N’;
MARTIN MARTINSALESMAN 6 2
ALLEN ALLENSALESMAN 5 1
Using Character Manipulation Functions
SQL> SELECT LPAD(DNAME,20,'*'), LPAD(DNAME,20),
LPAD(DEPTNO,20,'.')
FROM DEPT;
Single-values Functions
Using Number Functions
Round(Single-values Function) : It rounds the column, expression
or value to n decimal places or if n is omitted, no decimal places (if
n is negative, numbers to the left to the decimal point are rounded.
LOG(10,10)
-----------------
1
Working with Dates
• Oracle stores dates in an internal numeric format :
century, year, month, day, hours, minutes, seconds.
• The default date format is DD-MON-YY.
• SYSDATE is a function returning date and time.
• DUAL is a dummy table used to view SYSDATE.
The DUAL table is owned by the user SYS and can be accessed by all
users. It contains one column, DUMMY, and one row with value X. The
DUAL table is useful when you want to return a value one only – for
instance, the value of a constant, pseuudocolumn, or expression that is not
derived from a table with user data. The DUAL table generally used for
SELECT clause syntax completeness, because both SELECT and FROM
clause are mandatory, several calculations do not need to select from
actual tables.
Working with Dates
SQL> SELECT SYSDATE FROM DUAL;
SYSDATE
----------------------
19-JUL-04
ENAME WEEKS
---------------------- ---------------------------
KING 830.93709
CLARK 853.93709
MILLER 821.36566
Working with Dates
Function Description
MONTHS_BETWEEN No. of months between two
date
ADD_MONTHS Add calendar month to date
NEXT_DAY Next day of date specified
LAST_DAY No. of months between two
ROUND Round date
TRUNC Truncate date
Working with Dates
ENAME WEEKS
---------------------- ---------------------------
KING 830.93709
CLARK 853.93709
MILLER 821.36566