You are on page 1of 9

INTERACTIVE SQL 33

COMPUTATIONS IN EXPRESSION LISTS USED TO SELECT DATA


Arithmetic operators:
Addition Multiplication
Subtraction Exponentiation
Division Enclosed operation

Example
1. Select product_no, description and compute sell price 0.05 and sell_price 1.05
for each row retrieved;
SELECT product_no, description, sell price
FROM product_master,
0.05, sell price 1.05

Here, sell_price *
0.05 and sell price 1.05 are not columns in the table
but are calculations done on the contents of the product_master,
column sell price of the table
product master.
By default, the DBA will use the column of the table
output to the user on screen.
names
product master to display
Since there are no columns with names as
table product_master, the DBA will sell_price 0.05 and sell _price 1.05 in the
as the default column
perform the required operations and use these names
names when
displaying the output as shown below:
Product No Description Sell Price 0.05 Sell Price* 1.05
PO0001 1.44 Floppies 25
P03453 525
Monitors 600 12600
P06734 Mouse 50 1050
PO7865 1.22 Floppies 25 525
PO7868 Keyboards 150
PO7885 3150
CD Drive 250
PO7965 HDD
5250
400 8400
PO7975 1.44 Drive
PO8865 50 1050
1.22 Drive 50 1050
DEVELOPER
2000
ORACLE 7,
USING
DEVELOPMENT

APPLICATION

C O M M E R C I A L Lists:
Expression if required.
Used With by the
user

Renaming Columns
renamed
names can be
column
The default output columnname
r e s u l t _ c o l u m n n aame
me

result_columnname,
columnname
SELECT
Syntax
Syntax
FROMtablename;

0.05 and sell_price 1 0s


Example compute sell_price
no, description and Increase and salary* 1.05
5 aas
1. Select product Rename sell_price
0.05 as
retrieved.
row
for each
New Price;

SELECT product no, description, * 1.05 New_Price


* 0.05 Increase, sell price
sellprice
FROM product_master,

Increase
New Price
Product No Description

25 525
PO0001 1.44 Floppies
600 12600
P03453 Monitors
50 1050
P06734 Mouse
P07865 1.22 Floppies 25 525
150 3150
PO7868 Keyboards
PO7885 CD Drive 250 5250
PO7965 HDD 400 8400
P07975 1.44 Drive 50 1050
PO8865 1.22 Drive 50 1050

LOGICAL OPERATORS
The logical operators that can be used in SQL sentences are
and all of must be included
or any of may be included
not none of would be included
Examples
1. Select client information like client ode
for all the clients in 'BOMBAY' or
no, name, address1, address2, city and plnco
'DELHT;
SELECT client_no, name, address1, address2, city,
FROM client_master WHERE pincode
city "BOMBAY' OR city "DELHI,
=
=
INTERACTIVE SQL 35
2. Select client information like client no, name, address1, address2, city and pincode
for all the clients in Santacruz(w) or Vile
Parle (E);
SELECT client no, name, address1, address2, city, pincode
FROM client_master WHERE city =

"BOMBAY' AND
(pincode = 400054 OR pincode = 400057);

3. Select product no, description, profit_percent, sell price where profit_percent is


between 10 and 20 both inclusive;

SELECT product no, description, profit percent, sell _price


FROM product_master
WHERE profit percent>= 10 AND profit_percent=20;
RANGE SEARCHING
Examples
1. Select product_no, description, profit_percent, sell price where profit percent is
between 10 and 20 both inclusive;
SELECT product_no, description, profit _percent, sell _price
FROM product_master
WHERE profit percent BETWEEN 10 AND 20;

Select produet no, description, profit_percent, sell_price where profit percent is not
2.
between 10 and 20;

SELECT product no, description, profit_percent, sell _price


FROM product_master
10 AND 15;
WHERE profit percent NOT BETWEEN
PATTERN MATCHING
The use of the like predicate
For character data types: % matches any string
(underscore) matches any single character

Examples
I. Select supplier_name from supplier_master where the first two characters of name

ja'
SELECT supplier name FROM supplier_master
WHERE supplier name LIKE ja%;
6 COMMRCIAL APPLICATON DEVELOPMENT USING ORACLE 7, DEVELOPER 2000

2 Select supplier name from supplier _master where the second character of
or h name is 'r
na.

SELECT supplier name


FROM supplier master
WHERE supplier_name LIKE"r% OR supplier_name LIKE" h%
Select supplier name, address l, address2, city and
pincode from
where name is 3 characters
long and the first two characters are ja'; supplier master
SELECT supplier name,
FROM supplier address1, address2, city, pincode
master WHERE supplier_name like ja_,
Thein and not in
Example predicates:
1. Select supplier name, address1, address2,
where name is city and pincode
'ramos', 'clark', pramada' or 'aruna'; from
supplier_master
SELECT supplier
name,
address1,
FROM supplier master address2, city, pincode
WHERE
supplier_name IN ('Ramos, 'Clark,
'Pramada', 'Aruna');
INTERACTIVE SOI. 37
ORACLE FUNCTIONS
Functions are used to manipulate data items and returna result. Functions follow the format of
function name(argument l,argument2,.). An argument is a user-supplied variable or constant. The
structure of functions is such that it accepts zero or more arguments.

Examples
AVG Syntax AVG([DISTINCT|ALL] n)
Purpose Returns average value of n, ignoring null values
Example SELECT AVG(sell_price) "Average"
FROM product_master,
Output Average
2012.3654

Note In the above SELECT statement, AVG function is used to calculate the average selling price of
all products. The selected column is renamed as 'Average' in the
output.
MIN Syntax MIN([DISTINCT ALL expr)
Purpose Returns minimum value of expr.
Example SELECT MIN(s _order _date) "Minimum Date"
FROMsales_order
Output Minimum Date

26-Jan-93

COUNT(expr) Syntax COUNT(DISTINCTJALL] expr)


Purpose Returns the number of rows where expr is not null.
Example SELECT COUNT(order_no) "No of Orders"
FROM sales_order,
Output No of Orders

COUNT() Syntax COUNT()


Purpose Returns the number of rows in the table, including
duplicates and those with nulls.
Example SELECT OUNT(*) "Total" FROM client master,
Output Total

15

MAX Syntax MAX([DISTINCT ALL] expr)


Purpose Retuns maximum value of expr
Example SELECT MAX(qty_ordered) "Maximum
FROM sales_order_details
Output Maximum

5000.00
DEVELOPER
2000
ORACLE 7,
APPLICATION DEVEL OPMENT USING
vMERCIAL
SUM([DISTINCTALL] n)
SUM Syntax of values of
n.

Purpose
Returns sum

ordered)
"Total Qty"
SELECT SUM(qty
Example FROM sales_order_details

product_no
=
"P00001
WHERE

Total Qty
Output
29025.00

ABS Syntax ABS(n)


Returns the absolute value of n.
Purpose "Absolute" FROM dual;
SELECT ABS(-15)
Example
Output Absolute

15

POWER Syntax POWER(m, n)


Returns m raised to nth power. n must be an integer; else
Purpose
an error Is returned.
SELECT POWER(3,2) "Raised" FROM dual;
Example
Output Raised

ROUND Syntax ROUNDn[,m])


Purpose Returns n rounded to m places right of the decimal point
ifm is omitted, to 0 places. m can benegative to round
off digits left of the decimal point. m must be an
integer.
Example SELECT ROUND(15.19,1) "Round" FROM dual,
Output Round

15.2

SQRT Syntax SQRT(n)


Purpose Returns square root of
n; if n<0, NULL. SQRT returns a
real result.
Example SELECT SQRT(25)
Output "Square Root" FROM dual,
Square Root

LOWER Syntax
Purpose LOWER(char)
Returns
Example char, with all letters in
SELECT lowercase
LOWER(TVAN
FROM dual BAYROSS) "Lower"
Output Lowen

ivan bayross
INTERACTIVE SQL 39
INITCAP Syntax INITCAP(char)
Purpose Returns string with the first letter in upper case
Example SELECT INITCAP(IVAN BAYROSS') "Title Case"
FROM dual;
Output Title Case

Ivan Bayross

UPPER Syntax UPPER(char)


Purpose Returns char, with all letters forced to uppercase.
Example SELECT UPPER(Ms. Carol) FROM dual,
Output UPPER(Ms. Carol')
MS. CAROL
SUBSTR Syntax SUBSTR(char, m[.n])
Purpose Returns a portion of char, beginning at character m, n
characters long (if n is omitted, to the end char). The
first position of char is 1.
Example SELECT SUBSTR(ABCDEFG,2,3) "Substring"
FROM dual,
Output Substring
BCD

LENGTH Syntax LENGTH(char)


Purpose Returns the length of char
Example SELECT LENGTH(ELEPHANT) "Length" FROM dual,
Output Length

LTRIM Syntax LTRIM(char[,set])


Purpose Removes characters from the left of char with initial
characters removed upto the first character not in set.
Example SELECT LTRIM('xxxXxxLAST WORD',x)
"Left trim example" FROM dual
Output Left trim example

XxxLAST wORD

RTRIM Syntax RTRIM(char, [set])


Purpost Returns char, with final characters removed after the
last character not in the set. set is optional it defaults to"
Example SELECT RTRIM(TURNERKxXxx, x)
"RTRIM Example" FROM dual
Output RTRIM Example

TURNERxxX
0 COMMERCIAL APPLICATION DEVELOPMENT USING ORACLE 7, DEVELOPER 2000

LPAD Syntax LPAD char1,n [,char21)


Purpose Returns char1, left padded to length n with the sequence
of characters in char2; char2 defaults to blanks
Example SELECT LPAD(Page l', 14,*) "Lpad" FROM dual;
Output Lpad

*** Page 1

RPAD Syntax RPAD(charl,n[, char2])D


Purpose Returns char1, right-padded to length n with the
characters in char2, replicated as many times as
necessary, if char2 is omitted, right-pad with blanks
Example SELECT RPAD(name, 10,x) "RPAD Example"
FROM client_master
WHERE name = TURNER';

Output RPAD Example

TURNERxxxx
TO_NUMBER Syntax TONUMBER(char)
Purpose Converts char, a character value containing a number, to
a value of NUMBER
datatype.
Example UPDATE product_master SET sell_price =

sell_price+
TO_NUMBER(SUBSTR($100',2,3);
TO DATE Syntax TO DATE(char [, fmt))
Purpose Converts a character field to a date field.
Example INSERT INTO sales order
(s_order no,
s_order_date)
VALUES (087650°, TO DATE( 30-SEP-85 10:55 A.M.',
DD-MON-YY HH:MI A.M.');
If the date has to be entered in any other format
thanother
DD-MON-YY° then we need to use the
To enter the time TO DATE function
portion of a date, the TO DATE function
must be used with a format mask indicating the time portion.
TO CHAR Syntax TOCHAR(n[,fmt])
Tumber
Purpose Converts a value of NUMBER
datatype to a value of
Conversion) CHAR datatype, using the
optional format string. fmt
must be a number format. If fmt is
omitted, n is the
converted to a char value exactly
long enough
to hold
significant digits.
Example SELECT TO CHAR(17145,
$099,999) "Char"
FROM dual;
Output Char

$017,145
INTERACTIVE SQL 41
TOCHAR Syntax TO CHAR(d[, fmt])
(date Purpose Convertss a value of DATE
conversion) the format datatype to CHAR value in
specified by the char value fmt. fmt must be a
date format. If fmt is
omitted, d is converted to a character
value in the default date
Example SELECT TO format, i.e. "DD-MON-YY".
CHAR(s order date, Month DD, YYYY')
"New Date Format" FROM
WHERE s order no sales order
Output New Date Format
=
'042453",
January 26, 1996
The above
Oracle
Oracle. These arefunctions
the Oracle just
are afew selected
from the many
functions that are most
development. They will functions that are in built
own functions using SQLserve to indicate
how commonly
Oracle functions are used in
commercial
into
Oracle function
already exists PL/SQL Oracle manuals used. Before you application
or
refer to the
that would
allow you to and to see develop
if an in your
process your datacheck
as built
required.

You might also like