-------------------------------------------------------------
1. left join of two tables with where condition 
         (leftdl join because of multiple occurances of HSTD key field)
select * 
	from nzplaces = n leftdl join pointdesc = p 
	on n.desc_code = p.code 
	where n.name like 'had*' 
	order by n.name, n.id

n.id n.name n.easting n.northing n.desc_code n.district n.sheet n.latitude n.longitude p.code p.name 
55800 Haddington 2849197.10 6167019.50 HSTD NA V21 -39.63 176.95 HSTD HSTD:_Homestead 
21961 Haddon 2479668.20 5812327.00 HSTD CH M33 -42.89 172.62 HSTD HSTD:_Homestead 
20713 Hadlea 2445565.70 5719347.10 HSTD CH L36 -43.73 172.19 HSTD HSTD:_Homestead 
15972 Hadleigh 2363360.80 5603487.60 HSTD CH J40 -44.76 171.14 HSTD HSTD:_Homestead 
20597 Hadleigh 2438123.60 5733461.60 HSTD CH L36 -43.60 172.10 HSTD HSTD:_Homestead 
47176 Hadleigh 2751691.60 6033971.50 HSTD WN T26 -40.86 175.86 HSTD HSTD:_Homestead 
15713 Hadlow 2358932.10 5647518.80 HSTD CH J39,K39 -44.36 171.10 HSTD HSTD:_Homestead 
15732 Hadlow 2363880.80 5646767.80 LOC CH J39,K39 -44.37 171.16 LOC LOCALITY:_Defined_area_of_low_or_nil_population 
15723 Hadlow_Corner 2361801.00 5647582.80 RDRF CH J39,K39 -44.36 171.14 RDRF ROAD_RELATED_FEATURE:Named_road_feature 
15720 Hadlow_Downs 2361275.80 5647814.20 HSTD CH J39,K39 -44.36 171.13 HSTD HSTD:_Homestead 
15724 Hadlow_Grange 2362467.90 5647763.70 HSTD CH J39,K39 -44.36 171.14 HSTD HSTD:_Homestead 
-------------------------------------------------------------
2. same as above but requesting specific fields
select n.name, n.district, p.name 
	from nzplaces (as n) leftdl join pointdesc (as p) 
	on n.desc_code = p.code 
	where n.name like 'had*' 
	order by n.name, n.id

n.name n.district p.name 
Haddington NA HSTD:_Homestead 
Haddon CH HSTD:_Homestead 
Hadlea CH HSTD:_Homestead 
Hadleigh CH HSTD:_Homestead 
Hadleigh CH HSTD:_Homestead 
Hadleigh WN HSTD:_Homestead 
Hadlow CH HSTD:_Homestead 
Hadlow CH LOCALITY:_Defined_area_of_low_or_nil_population 
Hadlow_Corner CH ROAD_RELATED_FEATURE:Named_road_feature 
Hadlow_Downs CH HSTD:_Homestead 
Hadlow_Grange CH HSTD:_Homestead 
-------------------------------------------------------------
3. join of ordinary file with table
	(leftdl join since file has mutliple occurances of CH key field)
select * into /tmp/shsql_tmp1 from nzplaces where latitude inrange -44.34,-44.32 and longitude inrange 170.2,170.4

id name easting northing desc_code district sheet latitude longitude 
select * from /tmp/shsql_tmp1 (as t) leftdl join landdist (as d) on t.district = d.code order by t.name, t.id

t.id t.name t.easting t.northing t.desc_code t.district t.sheet t.latitude t.longitude d.code d.name 
12118 Grays_Hills 2301013.70 5650268.20 HSTD CH I38 -44.32 170.37 CH Canterbury 
10000 Haldon_Boat_Harbour 2288232.60 5647372.10 MPLA CH H39 -44.34 170.21 CH Canterbury 
12231 Little_Pass 2298530.50 5649526.30 RDRF CH I39 -44.33 170.34 CH Canterbury 
9959 Tekapo_River 2287319.40 5649040.20 STRM CH H39 -44.33 170.20 CH Canterbury 
-------------------------------------------------------------
4. Three table join, using ordinary file as intermediary
select n.name (as placename), n.district, p.name (as pointdesc) 
	into /tmp/shsql_tmp2 
	from nzplaces (as n) leftdl join pointdesc (as p) 
	on n.desc_code = p.code 
	where n.name like 'had*' 

placename n.district pointdesc 
select a.placename (as placename), b.name (as district), a.pointdesc (as featuretype)
	from /tmp/shsql_tmp2 (as a) leftdl join landdist (as b)
	on a.district = b.code
	order by a.placename

placename district featuretype 
Haddington Hawke's_Bay HSTD:_Homestead 
Haddon Canterbury HSTD:_Homestead 
Hadlea Canterbury HSTD:_Homestead 
Hadleigh Canterbury HSTD:_Homestead 
Hadleigh Canterbury HSTD:_Homestead 
Hadleigh Wellington HSTD:_Homestead 
Hadlow Canterbury HSTD:_Homestead 
Hadlow Canterbury LOCALITY:_Defined_area_of_low_or_nil_population 
Hadlow_Corner Canterbury ROAD_RELATED_FEATURE:Named_road_feature 
Hadlow_Downs Canterbury HSTD:_Homestead 
Hadlow_Grange Canterbury HSTD:_Homestead 
