Nebc Database Course Postgresql Cheat-Sheet V2 December 2008
Nebc Database Course Postgresql Cheat-Sheet V2 December 2008
December 2008
For each example, replace mytable and mycol with your own table and column names. The
new element in each example is highlighted in bold.
SELECT
Command
What it does
ORDER BY foo
SELECT mycol1 as foo, mycol2 AS bar FROM mytable1
INTERSECT
What it does
UPDATE mytable
What it does
FROM mytable
WHERE mycol3 = 1
GROUP BY mycol1 HAVING count(*) < 3
SELECT to_date('200425thJune','YYYYDDthmonth')
SELECT CASE WHEN mycol1 = true THEN 'badger' ELSE 'mushroom' END
FROM mytable1
Data Types
Data Type
Description
integer,int,int4
Whole number/integer
float
numeric(p,s)
Exact numeric type with total digits 'p' and digits after
decimal point 's'
date
Calender date
timestamp
char(n)
text
boolean, bool
!
#
%&
&
%
#
(
%&
))
+
&))
(
+ ,%
,
.( /
%
! 0100.0&
%
#
%+ #
2
!
#2
&
# 00!
0&&&(0+
&
3( 45!%! +!#!+%
%67
+60%
0
1
!!
+
! %67(&
8
!
! ,,2
+
9
8
+ %
!%9
:(!
# %! ,%67;&8
9
7( +!#
44#<5=%
8
44+ 9
>(?!
#
9
#
#%
#!! &
M Day 1 : Advanced Querying
!
#
%
#
(
#)
!
+
#)
%
&
, %
%
./%%
% #
0
&1&0
2034&5%6
%%
7
0
%#!%
%
#
8
9
0
%#
:
##
;
)=
6
>3/
?!
%2
#
;!
6
!
#%
#!
!
!
%!
(
)
)
+#,+
+
(
!+
!
..
.!
+,
!(
(
!
(
!
(
!
1
+,3
4(55
!
!!
(5 (
55!6
!
+
(
!
7
!
!0
8
9
!
,
!
1
:
!
(
.
!
!+
!
3
!
!
!
(
!
;
!
!
(<
!
(
( !
!
.
(
(
=
>?>>>>
##>>>>
>?#>>
###>>
##>>
)
;:
#>>>>
#>>>
#%>>>
#
##>>>
#>>>
#>>>
#/
##%>>
#%>>
#%>>
>
((
#>>>>
#>>>>
#/>>>>
#%>>>>
#>>>>
#>>>>
##>>>>
#>>>>
((
#>>>>
#%>>>>
#>>>>
#>
##>>
##>>
#?#>>
#%>>>
#%>>>
#?%>>>
)
;:
#%>>>
#>>>
>>>>
#%>>
#/%>>
#%>>
>
=
#>>>>
#>>>>
>>>>
! !#
%
&
)
#
(
!
#
!
#
#
%&&& !
!!!
! (
%
!
&#((((##
&#
& )&
&
)& ! + (
!
,
+++
+
+++
+
+
+++
&+&
!
#
,#
& ! + (
(
! !(
! #. !& &#(
#/ (#
!
#
./
0 #!#
+
0#
+++
+
+++
+
+
+++
&+&
0 !
1 #2# #31# 4
&&&&&
&&
!&&
&&
&
! 1#02#
&&&&&
&&
!3
&&
&
! 1#02#
&&
8
( !9 :!
+;<
4
=>/
7+&78
99
7+&:8
)
)+&+&
+&
)++
+
)8
8
8
+80
++
8
9
/) ;+89
80
98
;0
8;0
99799
+/
#<
#
5:
99+
)
#
#
599+&
)
#
#
599
)
#
#
8
599 !
8
=5
)
#
#
58
99 !
8
>5
8
!
#
!
%&
SITE
Found at
SWORD
()
+,.//
!
!
!
(
00 00!
!+
!
&
EMPLOYEE
CAR
RENTAL
NAMED_DRIVER
& ) !
# !)
#
ROUTE
runs on
SERVICE
stops at
STOP
TOWN
located in
between 2
SPECIAL_PRICE
has a
DEPOT
between 2
TIME
FARE
! #
%
&
#()+,
&
#()+,
&
#()+,
+,
+./%/012 1#%
)
)3
&
!
&#
&
#4()+,
&%
&
#()+,
%
&
#()+,
+./%
&/012 1#
&%
)
)3
&&
5
!
#
&%
&
#()+,
%%
&
#()+,
% +,
& %
+,
+./%
%& % 6 2##& % 789))
+./%
/012 1#%%
% )
+./%
%
&%
+ /:2 1#
&%
) .
&#
&%
)
+./%
%%%
+ /:2 1#%%
) .#%
)
)3
;
&
5
!
%#
%%
&
#()+,
% +,
&%
&
#()+,
+./%
%/012 1#%%
%
&%
)
+./%
%%
&%
+ /:2 1#
&%
) .
&#
&%
)
+./%
%%%%
+ /:2 1#%%
) .#%
)
)3