29.10.2014 Views

exercises, answers and "cheat sheet"

exercises, answers and "cheat sheet"

exercises, answers and "cheat sheet"

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

NEBC Database Course PostgreSQL Cheat-sheet v2<br />

December 2008<br />

For each example, replace mytable <strong>and</strong> mycol with your own table <strong>and</strong> column names. The<br />

new element in each example is highlighted in bold.<br />

SELECT<br />

SELECT * from mytable<br />

SELECT DISTINCT * FROM mytable<br />

SELECT mycol1, mycol2 FROM table<br />

Comm<strong>and</strong><br />

SELECT mycol1 AS foo FROM mytable<br />

SELECT * FROM mytable ORDER BY mycol1 ASC, mycol2 DESC<br />

SELECT * FROM mytable WHERE mycol1 = mycol2<br />

SELECT * FROM mytable WHERE mycol1 = 5 OR<br />

( mycol1 = 6 AND lower(mycol2) = 'snake' )<br />

SELECT * FROM table WHERE mycol1 IS NULL<br />

get all entries<br />

get all unique entries<br />

What it does<br />

get only columns mycol1 <strong>and</strong> mycol2<br />

get mycol1, but call it “foo” in the results<br />

sort by mycol1 – if 2 rows have the same value here then<br />

sort these by mycol2 in reverse order<br />

a simple search condition based on equality<br />

find all rows where mycol1 is 5, or where mycol1 is 6 but<br />

also mycol2 contains 'snake', 'Snake', 'sNAke' etc.<br />

searching for NULL values with 'IS NULL'<br />

SELECT * FROM mytable WHERE mycol1 IN (3, 4, 5) specify that col1 must be 3, 4 or 5<br />

SELECT * FROM mytable1 WHERE mycol1 IN<br />

(SELECT mycol2 FROM mytable2)<br />

obtain the list of matches from col2 in table2<br />

SELECT * FROM my table1 t1 WHERE EXISTS<br />

(SELECT * FROM mytable2 t2 WHERE t2.mycol2 = t1.mycol1)<br />

SELECT t1.*, t2.* FROM mytable1 t1 INNER JOIN mytable2 t2 ON<br />

(t1.pk_col = t2.fk_col)<br />

SELECT t1.*, t2.* FROM mytable1 t1 LEFT OUTER JOIN mytable2 t2 ON<br />

(t1.pk_col = t2.fk_col)<br />

SELECT mycol1 as foo, mycol2 AS bar FROM mytable1<br />

UNION<br />

SELECT mycol1 as foo, mycol3 AS bar FROM mytable2<br />

same as the previous, but using the EXISTS clause.<br />

Choose whichever you find easiest!<br />

inner join, where tables 1 <strong>and</strong> 2 have a one-to-many<br />

relationship<br />

an outer join where columns from table 1 will be<br />

displayed even if there is no matching entry in table 2<br />

combine two SELECTS into a single result set – note the<br />

SELECT keyword appears twice, but the ORDER BY<br />

affects the whole result. Duplicate rows are not shown.<br />

Use 'UNION ALL' to show all duplicate rows<br />

ORDER BY foo<br />

SELECT mycol1 as foo, mycol2 AS bar FROM mytable1<br />

INTERSECT<br />

Shows all rows common to two SELECT statements.<br />

'INTERSECT ALL' will show all duplicate rows<br />

SELECT mycol1 as foo, mycol3 AS bar FROM mytable2<br />

ORDER BY foo<br />

SELECT mycol1, sum(mycol2) FROM mytable1<br />

WHERE mycol3 = 1<br />

GROUP BY mycol1 HAVING count(*) < 3<br />

SELECT * INTO mynewtable FROM myoldtable<br />

group entries in table1 by col1, showing each value of<br />

col1 as well as the sum of col2 for those rows<br />

disregard rows where col3 is not 1, <strong>and</strong> show only results<br />

with 1 or 2 occurrences of that value of col1 in the table<br />

Create newtable from oldtable


DELETE, INSERT, UPDATE<br />

Comm<strong>and</strong><br />

DELETE FROM mytable<br />

DELETE FROM mytable WHERE mycol1 != 5<br />

INSERT INTO mytable (mycol1, mycol2) VALUES ('foo', 'bar')<br />

INSERT INTO mytable2 SELECT mycol1, mycol2 FROM table1<br />

UPDATE mytable<br />

What it does<br />

delete EVERYTHING in a table<br />

delete by some condition<br />

insert with specific values<br />

insert values based on a SELECT statement<br />

update with WHERE condition<br />

SET mycol1 = 'foo', mycol2 = mycol2 * mycol2<br />

WHERE mycol3 IS NOT NULL<br />

OPERATORS AND FUNCTIONS<br />

Comm<strong>and</strong><br />

SELECT (mycol1 / mycol2) *100 AS percentage FROM mytable<br />

SELECT sqrt(mycol1) FROM mytable<br />

SELECT trunc(mycol1, 3) FROM mytable<br />

What it does<br />

perform some arithmetic on the columns (other<br />

mathematical operators include + <strong>and</strong> -)<br />

Select the square root of mycol1<br />

Truncate the value of mycol3 after 3 decimal points<br />

SELECT abs(mycol), round(mycol,2), ln(mycol1), sin(mycol), tan(mycol)<br />

FROM mytable<br />

SELECT mycol1 || ' <strong>and</strong> ' || mycol2 FROM mytable<br />

Absolute value of mycol,round mycol to 2 decimal<br />

places, natural log mycol,sine of mycol,tangent of mycol<br />

See documentation for further mathematical functions<br />

Concatenate mycol1 to mycol2 with the word '<strong>and</strong>'<br />

SELECT substr(mycol1, 2, 3) FROM mytable Select substring of mycol1 starting at character 2, 3<br />

characters in length<br />

SELECT upper(mycol1) FROM mytable<br />

SELECT lower(mycol1) FROM mytable<br />

SELECT * FROM mytable WHERE mycol1 = 'badger'<br />

SELECT * FROM mytable WHERE mycol1 != 'badger'<br />

SELECT * FROM mytable WHERE mycol1 LIKE '%adg%'<br />

SELECT sum(mycol1), avg(mycol1) FROM mytable<br />

SELECT count(*) FROM mytable<br />

SELECT mycol1, sum(mycol2) FROM mytable<br />

WHERE mycol3 = 1<br />

GROUP BY mycol1 HAVING count(*) < 3<br />

SELECT current_timestamp, now()<br />

SELECT to_char(mydatecol,'DD/MM/YYYY') FROM mytable<br />

SELECT to_date('200425thJune','YYYYDDthmonth')<br />

SELECT mydatecol::text FROM mytable1<br />

Display mycol1 in upper case<br />

Display mycol1 in lower case<br />

Select rows where mycol1 exactly matches 'badger'<br />

Select rows where mycol1 does not match 'badger'<br />

Select rows where mycol1 contains the string 'adg' (note<br />

use of '%' wildcard character)<br />

return both sum <strong>and</strong> average of mycol1<br />

Count the number of rows in specified table<br />

group entries in mytable by mycol1, showing each value<br />

of mycol1 as well as the sum of mycol2 for those rows<br />

disregard rows where mycol3 is not 1, <strong>and</strong> show only<br />

results with 1 or 2 occurrences of that value of mycol1<br />

Two ways to display the current date <strong>and</strong> time<br />

Select mydatecol in the format<br />

DD/MM/YYYY(eg.21/04/2008)<br />

Convert the string '200425thJune' to a date<br />

Typecast date values explicitly to data type varchar<br />

SELECT coalesce(mycol1::text, 'No value') FROM mytable1 Display 'No value' if value in mydf -h<br />

col1 is null<br />

SELECT CASE WHEN mycol1 = true THEN 'badger' ELSE 'mushroom' END<br />

FROM mytable1<br />

If boolean value in mycol1 is true print 'badger' otherwise<br />

print 'mushroom'


Data Types<br />

integer,int,int4<br />

float<br />

numeric(p,s)<br />

date<br />

timestamp<br />

varchar(n), character varying(n)<br />

char(n)<br />

text<br />

boolean, bool<br />

Data Type<br />

Whole number/integer<br />

Floating point number<br />

Description<br />

Exact numeric type with total digits 'p' <strong>and</strong> digits after<br />

decimal point 's'<br />

Calender date<br />

Date <strong>and</strong> time<br />

Variable length character string of max length 'n'<br />

Fixed length character string of length 'n'<br />

Variable length character of unlimited length<br />

A single true or false value (supported values:<br />

true/false,'t'/'f','true'/'false','y'/'n','yes'/'no','1','0')


!∀∀ # ∃ <br />

%& &<br />

%∋#(%&<br />

)) ∗ +∀&))<br />

( + ,%−∃,−<br />

.( / %! 0100.0&<br />

%#∃%+ #∃2! <br />

#2&<br />

∗ ∀ # 00!<br />

0∋&&&(0+&<br />

3( 45!%! +!#!+%<br />

%67<br />

+60% 0<br />

1 !!∋+! %67(&<br />

8 ! ! ,−,2−+9<br />

8 + % !%9<br />

:(!∀# %! ,%−67;&8 ∀∀9<br />

7( +!#<br />

44#(?!# 9<br />

?+ ,∋)(&&&−∀ ∗ ∃+! <br />

∀ !+&<br />

≅( 4 ∀+%.;∀&<br />

Α(%!0&<br />

Β(< +!#Χ! 9<br />

+∆∆00∆∆


M Day 1 : Advanced Querying<br />

<br />

! ∀#∀∃%∀<br />

∃∃<br />

&∀∀∋#∃<br />

( ∀#)∀∃∋∃<br />

!∗∃<br />

+ ∀#)∀∃∋∃∃<br />

%∋∃&∋∃, % ∃∋<br />

−∋%∃<br />

./%%%#∃<br />

∃∀0∃&1&0<br />

−2034−&5%∃6%∃%<br />

7∀∃0∃∃<br />

∀∃%#!%%#∀∃<br />

∃8<br />

9∀∃0%#<br />

∃<br />

:∃##∃∋;3−/<br />

?!∃∀∃∀∃∃%2∋∃∋∀<br />

#≅ <br />

;!∀6


!∀<br />

#∃%<br />

#! <br />

! &<br />

! ∋∀∃(!<br />

%! )(∗<br />

)∀ ∋∗<br />

)∀ ∗<br />

+#,++ <br />

( ∀∋!+ <br />

∀ ∀∃!<br />

−∀∀...!<br />

∀ <br />

∃∃∀∃ ∃∃<br />

+,<br />

∀∋∋!(∀<br />

∋(!<br />

(∃∃∃∀<br />

∃<br />

∀/!0∀(<br />

∋ ∋∋∃∀<br />

!<br />

∋∃ ( ∃ ∋∋!<br />

1 2(∀!<br />

+,3<br />

4(∀55<br />

!!!(5(∀55!6<br />

.(∀<br />

∃∀∀ ∀∃∃∃∃<br />

∃ ∃ ∃∃∀!&<br />

!<br />

0 !<br />

+ ∀ (∀!<br />

7!!0 <br />

∀∃ 8<br />

∀4∀.<br />

6!<br />

∀∀∃<br />

∋∀∀!


+%,<br />

∃9 !,∋∋!<br />

1 ∀∋ :<br />

∋∋! (<br />

.∃∀!<br />

∋ !+ ∋∃!<br />

3 !<br />

∋ ∃∀ !<br />

∃ ∋∋!<br />

(∀ ∀∋!<br />

; ∋∃∀∃ ∀ !<br />

∀∀!<br />

(∀?>>>> ##>>>> #>>>> 3=∀<br />

≅ >?#Α>> ###Α>> ##Α>> ΒΑ<br />

) ;: #>>>> #>>> #%>>> Β#<br />

Χ ##>>> #>>> #Α>>> Β#/<br />

≅ ##%Α>> #%Α>> #Α%Α>> Β><br />

(( #>>>> #Α>>>> #/>>>> ΒΑ<br />

#%>>>> #∆>>>> #Ε>>>> ΒΑ<br />

##>>>> #>>>> #/>>>> 3<br />

(( #>>>> #%>>>> #Ε>>>> Β#><br />

≅ ##Α>> #Α#Α>> #?#Α>> Β#Α<br />

Χ #%>>> #Α%>>> #?%>>> Β#Ε<br />

) ;: #%>>> #∆>>> >>>> Β<br />

≅ #Α%Α>> #/%Α>> #%Α>> Β><br />

=∀ #∆>>>> #Ε>>>> >>>> ΒΑ


! !∀#<br />

∃%∀&∋<br />

(∀#<br />

<br />

<br />

<br />

<br />

<br />

∀)<br />

∀#<br />

∀∀∗<br />

(<br />

(∀+


!∀∀#∃<br />

<br />

<br />

!∀##<br />

%&&& !∃<br />

!!! ∃<br />

∋! ( ∃<br />

∀∃%!&∃#∋∋((((#∃#<br />

&#<br />

&)&&<br />

)& ! ∗+(! <br />

,∃<br />

∗∃∗∀<br />

++∀++<br />

∗+∗++∀<br />

+∀+<br />

+∗+∗+<br />

&∗+∗&<br />

!∗#,#<br />

−& !∀∀ ∗+(<br />

( ! ∃∋!(! #∗.!&&#(<br />

#/ (#∃<br />

∗∀∃ !∗#−./0#!#<br />

+0#∗∋<br />

++∀++<br />

∗+∗++∀<br />

+∀+<br />

+∗+∗+<br />

&∗+∗&<br />

0∀∀∀!∀ ∀<br />

∃1#∋2##3∋1#4<br />

<br />

∗∀<br />

∀&&∗&&∗&<br />

&∗&∗<br />

!&∗&<br />

&&∗∗&<br />

!∀1#02#<br />

∗∀


∀&&∗&&∗&<br />

&∗&∗<br />

!3<br />

&&∗∗&<br />

!∀1#02#<br />

∋&∗&<br />

5 !∀∀1 ∗∀∀<br />

∀∃∀∀6<br />

∗∀<br />

∀&&∗&&∗<br />

&&∗&∗<br />

!∗456<br />

)<br />

∗∀<br />

+&∀+&&<br />

+∗&&∗<br />

&&∗&∗<br />

!+∗456<br />

7 !∀∀1 ∗∀<br />

!<br />

∗∀<br />

∀&&∗&&∗<br />

&&∗&∗<br />

!∗456<br />

<br />

∗∀<br />

+&∀+&&<br />

+∗&&∗<br />

&&∗&∗<br />

!+∗456<br />

8 (!9∀:!<br />

∃<br />

+;/∀<br />

7+&78∀∃<br />

99∃<br />

7+&:8∀<br />

)∀∃<br />

)+&+&∀<br />

+&∃<br />

)++∀


+∃<br />

)8∀8∀8∀<br />

<br />

+8∀0++8∀<br />

9/);∗+8∀9∀80<br />

98∀;08;0∗<br />

∃99∃799<br />

+/<br />

<br />

#5<br />

8∀<br />

?∀∀∃≅((∀∀<br />

! ∀& ∃<br />

+&∀<br />

∃&∃∃∃<br />

∃&∃∃%+∃<br />


!∀#∃∃<br />

!<br />

%&<br />

SITE<br />

Found at<br />

SWORD<br />

∋()∗∗∗<br />

∋+,−∗.∗∗/∗/<br />

!<br />

!<br />

!<br />

(0000! ∗<br />

∃∃∃∗!+<br />

!<br />

&<br />

CAR<br />

EMPLOYEE<br />

RENTAL<br />

NAMED_DRIVER<br />

−/∗#∗∗<br />

12,∀∗∃/∗/∃<br />

−)2∃/.∗//.∗/ ∗/ <br />

/−(3−4//∗/ &.∗∃/.<br />

<br />

+ −)2∗∃<br />

/ !<br />

(#−)2∗/∗<br />

15∃15∃∃!<br />

6<br />

/ 789∗∃∃∃<br />

& )∃∃!<br />

∃∗#∃!)<br />

#


¡∋,∗∗∗//∗//∗.∗.<br />

:#!<br />

( ;;;;∗<br />

+ !)#∃<br />

∗!<br />

∗∗∗∗.∗.<br />

+,∗∗∗∗.∗.<br />

)∗∗∃∃ ;!∃<br />

!) !<br />

−−(?.∗.∗∗<br />

#≅Α<br />

#≅Α<br />

+∗∗∃#∗∃4<br />

>&!)#Β∃∗<br />

!+∃


!∀ #<br />

∃%&∃#∋()∗+∗,∀∀<br />

−&∃#()∗+∗,∀∀<br />

&∃#()∗+∗,∀∀<br />

∃∗+∗,∀∀<br />

+∗./∗−%/012 1#∃%)<br />

)3<br />

&<br />

!∀ &#<br />

&∃#4()∗+∗,∀∀<br />

&%&∃#∋()∗+∗,∀∀<br />

%&∃#()∗+∗,∀∀<br />

+∗./∗−%&/012 1#&%)<br />

)3<br />

<br />

∗−<br />

∃&−&<br />

∃5<br />

!∀ #<br />

&%&∃#∋()∗+∗,∀∀<br />

%∃%&∃#∋()∗+∗,∀∀<br />

%∗+∗,∀∀<br />

&%∃∗+∗,∀∀<br />

+∗./∗−%%&%6 2##&%789))<br />

+∗./∗−%/012 1#%∃%%)<br />

+∗./∗−%%&%+ /:∗2 1#&%) ∗ .&#&%)<br />

+∗./∗−%%%∃%+ /:∗2 1#%∃%) ∗ .#∃%)<br />

)3<br />

;<br />

&∃∃5<br />

!∀ %#<br />

%∃%&∃#∋()∗+∗,∀∀<br />

%∗+∗,∀∀<br />

&%&∃#∋()∗+∗,∀∀<br />

+∗./∗−%%/012 1#%∃%%&%)<br />

+∗./∗−%%%&%+ /:∗2 1#&%) ∗ .&#&%)<br />

+∗./∗−%%%%∃%+ /:∗2 1#%∃%) ∗ .#∃%)<br />

)3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!