Some More Abt SQL
Some More Abt SQL
SQL
Dual
• The DUAL is special one row, one column table
present by default in all Oracle databases.
• The owner of DUAL is SYS but DUAL can be
accessed by every user.
• The table has a single VARCHAR2() column called
DUMMY that has a value calculated dynamically.
• Oracle DUAL table is a special table used for
evaluating expressions or calling functions.
• Used when you're not actually interested in the
data, but instead want the results of some
system function in a select statement.
Dual
• In Oracle, the SELECT statement must have a FROM
clause. However, some queries don’t require any
table for example converting string to uppercase.
• To use the built in function UPPER, which will convert
a string to uppercase oracle don’t allow
• SELECT UPPER(‘This is a string’)
• But we can use select statement using dual as
• SELECT UPPER('This is a string‘) FROM dual;
• Besides calling built-in function, you can use
expressions in the SELECT clause of a query that
accesses the DUAL table:
• SELECT (10+ 5)/2 FROM dual;
Handling Null Value
• To find NULL values or NOT NULL
values, you need to use the IS NULL
operator.
• The = or != operator will not work
with NULL values.
• For example to find employees
whose commission is null we can
write sql as:
• Select * from emp where commission
IS NULL;
Functions in SQL
• SQL has built in function that can be used
for variety of tasks.
• There are two classes of functions:
– Single Row functions
– Group function
• Single row function know number of
arguments in advance
• Group function don’t know amount of data
to be handled until all data is fetched and
grouped in categories.
Single Row Functiom
• Act on each row
• Produce one result per row.
• Classified as
– Character
– Number
– Date
– Conversion
– General
Character Function
• CHR(x) : Give character First
corresponding to value x. --------------
– Select CHR(97) first from dual; a
• CONCAT(c1,c2): Concatenate two
strings. ColgName
– Select CONCAT(‘Modi’,’College’) --------------
colgName from dual;
Modi College
• Length(C): returns length of string
• LOWER(c): Convert to lower case
• UPPER ( c ) : Convert to upper
case.
Character Function
• LTRIM(c1, c2 ) : Trim all characters of c2 from left side
of c1
• RTRIM(c1,c2): Trim all characters of c2 from right side
of c1
• REPLACE(c1,c2,c3): return c1 with all ocuurence of c2
replaced with c3.
– Select REPLACE(‘uptown’,’up’,’down’) from dual;
– Results in ‘downtown’
• SUBSTR(string [,m,n]): returns specified characters
from character value starting at position m and n
characters long.
– SELECT substr('ORACLE DATA RECOVERY',8,4) FROM DUAL;
– Results in Data
Numeric Function
• ROUND : rounds the value to the n decimal values. If
n is not specified, there won't be any decimal places.
– Syntax: round(number,n)
– SELECT round(123.67,1) FROM DUAL;
• MOD: returns the remainder of m divided by n.
– Syntax: mod(m,n)
– SELECT mod(10,5) FROM DUAL;
• ABS(n): returns Absolute value.
• EXP(n): returns en
• POWER(n1,n2) : returns n1n2
• SQRT(n) : returns square root of n.
Date Functions
• SYSDATE: The Sysdate function returns the
current oracle database server date and time.
– SELECT sysdate FROM DUAL;
• Arithmetic with Dates
You can add or subtract the number of days or
hours to the dates. You can also subtract the
dates
– SELECT sysdate+2 "add_days" FROM DUAL;
– SELECT sysdate-3 "sub_days" FROM DUAL;
– SELECT sysdate+3/24 "add_hours" FROM DUAL;
– SELECT sysdate-2/24 "sub_hours" FROM DUAL;
Date Functions
• MONTHS_BETWEEN : returns the number
of months between the two given dates.
– Syntax: months_between(date1,date2)
– SELECT months_between(sysdate,hire_date)
FROM EMPLOYEES;
• ADD_MONTHS: used to add or subtract the
number of calendar months to the given
date.
– Syntax: add_months(date,n)
– SELECT add_months(sysdate,3) FROM DUAL;
Conversion function
• Used to convert from one datatype to other
• To_char (number|date): convert a number or
date to varchar2 character string.
– Select to_char(sal) Salary from emp where
ename=‘ABC’;
• To_date(char,[fmt]): convert a character value
into date value and fmt is format of date.
– Select TO_DATE('070903', 'MMDDYY') from dual
– Result: date value of July 9, 2003
• To_number(text): convert text to number
– Select to_number(‘49583’) from dual;
Grouping function
• Also called aggregate functions, return a
value based on number of inputs.
• Exact number of input is not determined
until query is executed.
• Do not process NULL value and never
return a NULL value.
• Some of functions are: COUNT, SUM,
MAX, MIN, AVG etc.
• Already covered.