SQL
12:43 Thursday, 13 January 2022
Count ()-
Distinct -
:
Min (), Max (), Avg (), Sum ()
Round- 3
1:
2:
3:
':
round(AVG (replacement_cost),2)
***
Where
=,<,>,=<,=>,!=
Between
Between
Not Between
'YYYY-MM-DD'
.
Like=
ilike=
Not Like/Not ilike =
Group by
OR) IN (
OR) Not IN (
Order by Ace -
Order by Desc
Limit-
Limit 1-
Regular Expressions:
%=
A A%
_=
2
_her% = Cheryl, Sherri
***
New Section 1 Page 1
***
Join =
INNER JOIN
select * from payment
inner join customer
on payment.customer_id = customer.customer_id
Full outer Join =
null
WHERE
Left join =
select film.film_id, film.title, inventory_id, store_id
from film
left join inventory on
inventory.film_id = film. film_id
JOIN RIGHT
Union
select address.district, customer.email, address.address_id
from address
full outer join customer
on address.address_id = customer.address_idwhere district='California
New Section 1 Page 2
Extract
how old AGE
TO_char()
Pattern Description
HH hour of day (01-12)
HH12 hour of day (01-12)
New Section 1 Page 3
HH24 hour of day (00-23)
MI minute (00-59)
SS second (00-59)
MS millisecond (000-999)
US microsecond (000000-999999)
SSSS seconds past midnight (0-86399)
AM, am, PM or pm meridiem indicator (without periods)
A.M., a.m., P.M. or p meridiem indicator (with periods)
.m.
Y,YYY year (4 or more digits) with comma
YYYY year (4 or more digits)
YYY last 3 digits of year
YY last 2 digits of year
Y last digit of year
IYYY ISO 8601 week-numbering year (4 or more digits)
IYY last 3 digits of ISO 8601 week-numbering year
IY last 2 digits of ISO 8601 week-numbering year
I last digit of ISO 8601 week-numbering year
BC, bc, AD or ad era indicator (without periods)
B.C., b.c., A.D. or a. era indicator (with periods)
d.
MONTH full upper case month name (blank-padded to 9 chars)
Month full capitalized month name (blank-padded to 9 chars)
month full lower case month name (blank-padded to 9 chars)
MON abbreviated upper case month name (3 chars in English, localized lengths
vary)
Mon abbreviated capitalized month name (3 chars in English, localized lengths vary)
mon abbreviated lower case month name (3 chars in English, localized lengths vary)
MM month number (01-12)
DAY full upper case day name (blank-padded to 9 chars)
Day full capitalized day name (blank-padded to 9 chars)
day full lower case day name (blank-padded to 9 chars)
DY abbreviated upper case day name (3 chars in English, localized lengths vary)
Dy abbreviated capitalized day name (3 chars in English, localized lengths vary)
dy abbreviated lower case day name (3 chars in English, localized lengths vary)
DDD day of year (001-366)
IDDD day of ISO 8601 week-numbering year (001-371; day 1 of the year is Monday
of the first ISO week)
DD day of month (01-31)
D day of the week, Sunday (1) to Saturday (7)
ID ISO 8601 day of the week, Monday (1) to Sunday (7)
W week of month (1-5) (the first week starts on the first day of the month)
WW week number of year (1-53) (the first week starts on the first day of the year)
IW week number of ISO 8601 week-numbering year (01-53; the first Thursday of
the year is in week 1)
CC century (2 digits) (the twenty-first century starts on 2001-01-01)
J Julian Date (integer days since November 24, 4714 BC at local midnight;
see Section B.7)
New Section 1 Page 4
see Section B.7)
Q quarter (ignored by to_date and to_timestamp)
RM month in upper case Roman numerals (I-XII; I=January)
rm month in lower case Roman numerals (i-xii; i=January)
TZ upper case time-zone abbreviation (only supported in to_char)
tz lower case time-zone abbreviation (only supported in to_char)
OF time-zone offset from UTC (only supported in to_char)
From <https://www.postgresql.org/docs/9.6/functions-formatting.html>
Function Return Type Description Example
to_char(timestamp, t text convert time stamp to to_char(current_timestamp,
ext) string 'HH12:MI:SS')
to_char(interval, text) text convert interval to string to_char(interval '15h 2m 12s',
'HH24:MI:SS')
to_char(int, text) text convert integer to string to_char(125, '999')
to_char(double text convert real/double to_char(125.8::real, '999D9')
precision, text) precision to string
to_char(numeric, text text convert numeric to string to_char(-125.8, '999D99S')
)
to_date(text, text) date convert string to date to_date('05 Dec 2000',
'DD Mon YYYY')
to_number(text, text) numeric convert string to numeric to_number('12,454.8-',
'99G999D9S')
to_timestamp(text, te timestamp with convert string to time to_timestamp('05 Dec 2000',
xt) time zone stamp 'DD Mon YYYY')
From <https://www.postgresql.org/docs/9.6/functions-formatting.html>
SQL
Operator Description Example Result
+ addition 2+3 5
- subtraction 2-3 -1
* multiplication 2*3 6
/ division (integer division truncates the result) 4/2 2
% modulo (remainder) 5%4 1
^ exponentiation (associates left to right) 2.0 ^ 3.0 8
|/ square root |/ 25.0 5
||/ cube root ||/ 27.0 3
! factorial (deprecated, use factorial() instead) 5! 120
!! factorial as a prefix operator (deprecated, use factorial() instead) !! 5 120
@ absolute value @ -5.0 5
& bitwise AND 91 & 15 11
| bitwise OR 32 | 3 35
# bitwise XOR 17 # 5 20
~ bitwise NOT ~1 -2
<< bitwise shift left 1 << 4 16
>> bitwise shift right 8 >> 2 2
New Section 1 Page 5
From <https://www.postgresql.org/docs/9.5/functions-math.html>
Function Return Description Example Result
Type
string || string text String concatenation 'Post' || 'greSQL' Postgre
SQL
string || non- text String concatenation with one non- 'Value: ' || 42 Value:
string or non- string input 42
string || string
bit_length(string) int Number of bits in string bit_length('jose') 32
char_length(string) o int Number of characters in string char_length('jose' 4
r character_length(st )
ring)
lower(string) text Convert string to lower case lower('TOM') tom
octet_length(string) int Number of bytes in string octet_length('jose 4
')
overlay(string placin text Replace substring overlay('Txxxxas' Thomas
g string from int [for i placing 'hom'
nt]) from 2 for 4)
position(substring in int Location of specified substring position('om' in 3
string) 'Thomas')
substring(string [fro text Extract substring substring('Thoma hom
m int] [for int]) s' from 2 for 3)
substring(string from text Extract substring matching POSIX substring('Thoma mas
pattern) regular expression. See Section 9.7 for s' from '...$')
more information on pattern matching.
substring(string from text Extract substring matching SQL regular substring('Thoma oma
pattern for escape) expression. See Section 9.7 for more s' from '%
information on pattern matching. #"o_a#"_' for '#')
trim([leading | trailing text Remove the longest string containing trim(both 'x' from Tom
| both] [characters] only the characters (a space by default) 'xTomxx')
from string) from the start/end/both ends of
the string
upper(string) text Convert string to upper case upper('tom') TOM
From <https://www.postgresql.org/docs/9.1/functions-string.html>
SUBQUERY
2
VLOOKUP Self join
select f1.title, f2.title, f1.length
from film as f1
inner join film as f2 on
f1.film_id != f2.film_id
and f1.length = f2.length
New Section 1 Page 6