You are on page 1of 2

SQL

Special Tables

Grants

Select

Select varlist
From tablename
Where criteria
Group By varlist
Having criteria
Order By varlist;

ALL_TABLES
ALL_VIEWS
ALL_TAB_COLS
ALL_CONSTRAINTS
ALL_DEPENDENCIES
DICTIONARY

Grant Select On tablename To username;


Grant Update On tablename To username;
Grant Delete On tablename To username;
Grant All Privileges On tablename To username;
with grant option;

DDL

Create Table tablename


(columnname datatype,
columnname datatype,
);

Joins & More

Select varlist
From tablename
Where Rownum <= number;

More

Create Table tablename As


(Select * From tablename);
Drop Table tablename;
Alter Table tablename
Add columnname datatype;
Alter Table tablename
Drop Column columnname;

Select varlist
From tablename,
tablename
Where criteria;
Select varlist
From tablename
Left/Right/Full Join tablename
On criteria;

Describe tablename;
Select varlist
From tablename
As Of Timestamp timestamp;
('03-JUN-14 00:00:00',
'DD-Mon-RR HH24:MI:SS.FF')

Select tablename
Union
Select tablename;
Select tablename

DML

Delete From tablename


Where criteria;

Union All
Select tablename;

Insert Into tablename


(columnlist) values (valuelist);

Select tablename
Intersect
Select tablename;

Update tablename Set


columnname = value

Select tablename

Where criteria;

Minus
Select tablename;

Views

Create View viewname As


SQLQuery;

SQL Quick Reference

www.codeitmagazine.com

For more SQL, SAS, MS Office,


MS Visual Studios, and Scripting
Tips and Tricks, visit
www.CodeItMagazine.com
and subscribe for our free e-magazine.
Christopher Johnson

Function
Definition
to_char(string, <format>)
Text
Convert a string to text
to_date(string, <format>)
Convert a string to a date
to_number(string)
Convert a string to a number
substr(string, start, length)
Extracts part of a string
upper(string)
Converts a string to upper case
lower(string)
Converts a string to lower case
lpad(string, length, char)
Pads a string from the left
rpad(string, length, char)
Pads a string from the right
length(string)
Returns the length of a string
replace(string, chars, new)
Replaces patterns in a string
translate(string, chars, new)
Replaces characters in a string
trim(string)
Removes leading and trailing spaces
concat(string1, string2)
Combines strings
greatest(col1, col2)
Math
Returns the largest column value in a row
least(col1, col2)
Returns the smallest column value in a row
abs(number)
Returns the absolute value
round(number, places)
Rounds a number
add_months(date, number)
Date
Adds months to a date
sysdate, current_timestamp
Returns today's date, timestamp
trunc(date)
Removes the time from a timestamp
min(col)
Aggregate
Returns the smallest value in a column
max(col)
Returns the largest value in a column
count(col)
Returns the number of values
sum(col)
Returns the sum of values
avg(col)
Returns the average value
nvl(value, value)
More
Find the first non-null value
coalesce(value, value)
Find the first non-null value
decode(variable, value, result, value, result, , elseresult)
Case When criteria Then value When criteria Then value Else value End
In (item, item, )

SQL Quick Reference

www.codeitmagazine.com

Example
to_char(sysdate, 'YYYY')
to_date('31dec2014')
to_number('4')
substr('12345', 3, 2)
upper('abc')
lower('ABC')
lpad('abc', 5, '0')
rpad('abc', 5, '0')
length('abc')
replace('abcde', 'bc', 'xx')
translate('abcde', 'ac', 'xx')
trim(' abc ')
concat('abc', 'def')

abs(-1)
round(1.234, 1)
add_months(sysdate, 1)

nvl('', '1')
coalesce('', '1')

Christopher Johnson

You might also like