blob: ddfae1d3747387ddc5288d3fa0c77ccf447b8417 [file] [log] [blame]
--
-- PostgreSQL port of the MySQL "World" database.
--
-- The sample data used in the world database is Copyright Statistics
-- Finland, http://www.stat.fi/worldinfigures.
--
-- Modified to use it with GPDB
set extra_float_digits=0;
--start_ignore
create schema qp_with_clause;
set search_path = qp_with_clause;
DROP TABLE IF EXISTS city cascade;
NOTICE: table "city" does not exist, skipping
DROP TABLE IF EXISTS country cascade;
NOTICE: table "country" does not exist, skipping
DROP TABLE IF EXISTS countrylanguage cascade;
NOTICE: table "countrylanguage" does not exist, skipping
--end_ignore
SET optimizer_trace_fallback=on;
BEGIN;
--SET client_encoding = 'LATIN1';
CREATE TABLE city (
id integer NOT NULL,
name text NOT NULL,
countrycode character(3) NOT NULL,
district text NOT NULL,
population integer NOT NULL
) distributed by(id);
CREATE TABLE country (
code character(3) NOT NULL,
name text NOT NULL,
continent text NOT NULL,
region text NOT NULL,
surfacearea numeric(10,2) NOT NULL,
indepyear smallint,
population integer NOT NULL,
lifeexpectancy real,
gnp numeric(10,2),
gnpold numeric(10,2),
localname text NOT NULL,
governmentform text NOT NULL,
headofstate text,
capital integer,
code2 character(2) NOT NULL
) distributed by (code);
CREATE TABLE countrylanguage (
countrycode character(3) NOT NULL,
"language" text NOT NULL,
isofficial boolean NOT NULL,
percentage real NOT NULL
)distributed by (countrycode,language);
COPY city (id, name, countrycode, district, population) FROM stdin;
--
-- Data for Name: country; Type: TABLE DATA; Schema: public; Owner: chriskl
--
COPY country (code, name, continent, region, surfacearea, indepyear, population, lifeexpectancy, gnp, gnpold, localname, governmentform, headofstate, capital, code2) FROM stdin WITH NULL AS '';
--
-- Data for Name: countrylanguage; Type: TABLE DATA; Schema: public; Owner: chriskl
--
COPY countrylanguage (countrycode, "language", isofficial, percentage) FROM stdin;
ALTER TABLE ONLY city
ADD CONSTRAINT city_pkey PRIMARY KEY (id);
ALTER TABLE ONLY country
ADD CONSTRAINT country_pkey PRIMARY KEY (code);
ALTER TABLE ONLY countrylanguage
ADD CONSTRAINT countrylanguage_pkey PRIMARY KEY (countrycode, "language");
COMMIT;
ANALYZE city;
ANALYZE country;
ANALYZE countrylanguage;
-- queries with one CTE that is referenced once
-- Using CTE in the FROM clause
--query1
with capitals as
(select country.code,id,city.name from city,country
where city.countrycode = country.code AND city.id = country.capital)
select * from
capitals,countrylanguage
where capitals.code = countrylanguage.countrycode and isofficial='true'
order by capitals.code,countrylanguage.language;
code | id | name | countrycode | language | isofficial | percentage
------+------+-----------------------------------+-------------+------------------+------------+------------
ABW | 129 | Oranjestad | ABW | Dutch | t | 5.3
AFG | 1 | Kabul | AFG | Dari | t | 32.1
AFG | 1 | Kabul | AFG | Pashto | t | 52.4
AIA | 62 | The Valley | AIA | English | t | 0
ALB | 34 | Tirana | ALB | Albaniana | t | 97.9
AND | 55 | Andorra la Vella | AND | Catalan | t | 32.3
ANT | 33 | Willemstad | ANT | Dutch | t | 0
ANT | 33 | Willemstad | ANT | Papiamento | t | 86.2
ARE | 65 | Abu Dhabi | ARE | Arabic | t | 42
ARG | 69 | Buenos Aires | ARG | Spanish | t | 96.8
ARM | 126 | Yerevan | ARM | Armenian | t | 93.4
ASM | 54 | Fagatogo | ASM | English | t | 3.1
ASM | 54 | Fagatogo | ASM | Samoan | t | 90.6
ATG | 63 | Saint Johns | ATG | English | t | 0
AUS | 135 | Canberra | AUS | English | t | 81.2
AUT | 1523 | Wien | AUT | German | t | 92
AZE | 144 | Baku | AZE | Azerbaijani | t | 89
BDI | 552 | Bujumbura | BDI | French | t | 0
BDI | 552 | Bujumbura | BDI | Kirundi | t | 98.1
BEL | 179 | Bruxelles [Brussel] | BEL | Dutch | t | 59.2
BEL | 179 | Bruxelles [Brussel] | BEL | French | t | 32.6
BEL | 179 | Bruxelles [Brussel] | BEL | German | t | 1
BGD | 150 | Dhaka | BGD | Bengali | t | 97.7
BGR | 539 | Sofija | BGR | Bulgariana | t | 83.2
BHR | 149 | al-Manama | BHR | Arabic | t | 67.7
BIH | 201 | Sarajevo | BIH | Serbo-Croatian | t | 99.2
BLR | 3520 | Minsk | BLR | Belorussian | t | 65.6
BLR | 3520 | Minsk | BLR | Russian | t | 32
BLZ | 185 | Belmopan | BLZ | English | t | 50.8
BMU | 191 | Hamilton | BMU | English | t | 100
BOL | 194 | La Paz | BOL | Aimara | t | 3.2
BOL | 194 | La Paz | BOL | Ketdua | t | 8.1
BOL | 194 | La Paz | BOL | Spanish | t | 87.7
BRA | 211 | Brasilia | BRA | Portuguese | t | 97.5
BRB | 174 | Bridgetown | BRB | English | t | 0
BRN | 538 | Bandar Seri Begawan | BRN | Malay | t | 45.5
BTN | 192 | Thimphu | BTN | Dzongkha | t | 50
CAN | 1822 | Ottawa | CAN | English | t | 60.4
CAN | 1822 | Ottawa | CAN | French | t | 23.4
CCK | 2317 | West Island | CCK | English | t | 0
CHE | 3248 | Bern | CHE | French | t | 19.2
CHE | 3248 | Bern | CHE | German | t | 63.6
CHE | 3248 | Bern | CHE | Italian | t | 7.7
CHE | 3248 | Bern | CHE | Romansh | t | 0.6
CHL | 554 | Santiago de Chile | CHL | Spanish | t | 89.7
CHN | 1891 | Peking | CHN | Chinese | t | 92
COK | 583 | Avarua | COK | Maori | t | 0
COL | 2257 | Santafe de Bogota | COL | Spanish | t | 99
COM | 2295 | Moroni | COM | Comorian | t | 75
CPV | 1859 | Praia | CPV | Portuguese | t | 0
CRI | 584 | San Jose | CRI | Spanish | t | 97.5
CUB | 2413 | La Habana | CUB | Spanish | t | 100
CXR | 1791 | Flying Fish Cove | CXR | English | t | 0
CYM | 553 | George Town | CYM | English | t | 0
CYP | 2430 | Nicosia | CYP | Greek | t | 74.1
CYP | 2430 | Nicosia | CYP | Turkish | t | 22.4
CZE | 3339 | Praha | CZE | Czech | t | 81.2
DEU | 3068 | Berlin | DEU | German | t | 91.3
DJI | 585 | Djibouti | DJI | Arabic | t | 10.6
DNK | 3315 | Kobenhavn | DNK | Danish | t | 93.5
DOM | 587 | Santo Domingo de Guzman | DOM | Spanish | t | 98
DZA | 35 | Alger | DZA | Arabic | t | 86
ECU | 594 | Quito | ECU | Spanish | t | 93
EGY | 608 | Cairo | EGY | Arabic | t | 98.8
ERI | 652 | Asmara | ERI | Tigrinja | t | 49.1
ESH | 2453 | El-Aaiun | ESH | Arabic | t | 100
ESP | 653 | Madrid | ESP | Spanish | t | 74.4
EST | 3791 | Tallinn | EST | Estonian | t | 65.3
FIN | 3236 | Helsinki [Helsingfors] | FIN | Finnish | t | 92.7
FIN | 3236 | Helsinki [Helsingfors] | FIN | Swedish | t | 5.7
FJI | 764 | Suva | FJI | Fijian | t | 50.8
FLK | 763 | Stanley | FLK | English | t | 0
FRA | 2974 | Paris | FRA | French | t | 93.6
FRO | 901 | Torshavn | FRO | Danish | t | 0
FRO | 901 | Torshavn | FRO | Faroese | t | 100
GBR | 456 | London | GBR | English | t | 97.3
GEO | 905 | Tbilisi | GEO | Georgiana | t | 71.7
GIB | 915 | Gibraltar | GIB | English | t | 88.9
GLP | 919 | Basse-Terre | GLP | French | t | 0
GNB | 927 | Bissau | GNB | Portuguese | t | 8.1
GRC | 2401 | Athenai | GRC | Greek | t | 98.5
GRL | 917 | Nuuk | GRL | Danish | t | 12.5
GRL | 917 | Nuuk | GRL | Greenlandic | t | 87.5
GTM | 922 | Ciudad de Guatemala | GTM | Spanish | t | 64.7
GUM | 921 | Agaaa | GUM | Chamorro | t | 29.6
GUM | 921 | Agaaa | GUM | English | t | 37.5
HKG | 937 | Victoria | HKG | English | t | 2.2
HND | 933 | Tegucigalpa | HND | Spanish | t | 97.2
HRV | 2409 | Zagreb | HRV | Serbo-Croatian | t | 95.9
HTI | 929 | Port-au-Prince | HTI | French | t | 0
HUN | 3483 | Budapest | HUN | Hungarian | t | 98.5
IDN | 939 | Jakarta | IDN | Malay | t | 12.1
IND | 1109 | New Delhi | IND | Hindi | t | 39.9
IRL | 1447 | Dublin | IRL | English | t | 98.4
IRL | 1447 | Dublin | IRL | Irish | t | 1.6
IRN | 1380 | Teheran | IRN | Persian | t | 45.7
IRQ | 1365 | Baghdad | IRQ | Arabic | t | 77.2
ISL | 1449 | Reykjavik | ISL | Icelandic | t | 95.7
ISR | 1450 | Jerusalem | ISR | Arabic | t | 18
ISR | 1450 | Jerusalem | ISR | Hebrew | t | 63.1
ITA | 1464 | Roma | ITA | Italian | t | 94.1
JOR | 1786 | Amman | JOR | Arabic | t | 97.9
JPN | 1532 | Tokyo | JPN | Japanese | t | 99.1
KAZ | 1864 | Astana | KAZ | Kazakh | t | 46
KGZ | 2253 | Bishkek | KGZ | Kirgiz | t | 59.7
KGZ | 2253 | Bishkek | KGZ | Russian | t | 16.2
KHM | 1800 | Phnom Penh | KHM | Khmer | t | 88.6
KIR | 2256 | Bairiki | KIR | Kiribati | t | 98.9
KNA | 3064 | Basseterre | KNA | English | t | 0
KOR | 2331 | Seoul | KOR | Korean | t | 99.9
KWT | 2429 | Kuwait | KWT | Arabic | t | 78.1
LAO | 2432 | Vientiane | LAO | Lao | t | 67.2
LBN | 2438 | Beirut | LBN | Arabic | t | 93
LBY | 2441 | Tripoli | LBY | Arabic | t | 96
LCA | 3065 | Castries | LCA | English | t | 20
LIE | 2446 | Vaduz | LIE | German | t | 89
LKA | 3217 | Colombo | LKA | Singali | t | 60.3
LKA | 3217 | Colombo | LKA | Tamil | t | 19.6
LSO | 2437 | Maseru | LSO | English | t | 0
LSO | 2437 | Maseru | LSO | Sotho | t | 85
LTU | 2447 | Vilnius | LTU | Lithuanian | t | 81.6
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | French | t | 4.2
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | German | t | 2.3
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | Luxembourgish | t | 64.4
LVA | 2434 | Riga | LVA | Latvian | t | 55.1
MAC | 2454 | Macao | MAC | Portuguese | t | 2.3
MAR | 2486 | Rabat | MAR | Arabic | t | 65
MCO | 2695 | Monaco-Ville | MCO | French | t | 41.9
MDA | 2690 | Chisinau | MDA | Romanian | t | 61.9
MDG | 2455 | Antananarivo | MDG | French | t | 0
MDG | 2455 | Antananarivo | MDG | Malagasy | t | 98.9
MDV | 2463 | Male | MDV | Dhivehi | t | 100
MEX | 2515 | Ciudad de Mexico | MEX | Spanish | t | 92.1
MHL | 2507 | Dalap-Uliga-Darrit | MHL | English | t | 0
MHL | 2507 | Dalap-Uliga-Darrit | MHL | Marshallese | t | 96.8
MKD | 2460 | Skopje | MKD | Macedonian | t | 66.5
MLT | 2484 | Valletta | MLT | English | t | 2.1
MLT | 2484 | Valletta | MLT | Maltese | t | 95.8
MMR | 2710 | Rangoon (Yangon) | MMR | Burmese | t | 69
MNG | 2696 | Ulan Bator | MNG | Mongolian | t | 78.8
MNP | 2913 | Garapan | MNP | English | t | 4.8
MSR | 2697 | Plymouth | MSR | English | t | 0
MTQ | 2508 | Fort-de-France | MTQ | French | t | 0
MWI | 2462 | Lilongwe | MWI | Chichewa | t | 58.3
MYS | 2464 | Kuala Lumpur | MYS | Malay | t | 58.4
MYT | 2514 | Mamoutzou | MYT | French | t | 20.3
NCL | 3493 | Noumea | NCL | French | t | 34.3
NFK | 2806 | Kingston | NFK | English | t | 0
NIC | 2734 | Managua | NIC | Spanish | t | 97.6
NIU | 2805 | Alofi | NIU | English | t | 0
NLD | 5 | Amsterdam | NLD | Dutch | t | 95.6
NOR | 2807 | Oslo | NOR | Norwegian | t | 96.6
NPL | 2729 | Kathmandu | NPL | Nepali | t | 50.4
NRU | 2728 | Yaren | NRU | English | t | 7.5
NRU | 2728 | Yaren | NRU | Nauru | t | 57.5
NZL | 3499 | Wellington | NZL | English | t | 87
OMN | 2821 | Masqat | OMN | Arabic | t | 76.7
PAK | 2831 | Islamabad | PAK | Urdu | t | 7.6
PAN | 2882 | Ciudad de Panama | PAN | Spanish | t | 76.8
PER | 2890 | Lima | PER | Aimara | t | 2.3
PER | 2890 | Lima | PER | Ketdua | t | 16.4
PER | 2890 | Lima | PER | Spanish | t | 79.8
PHL | 766 | Manila | PHL | Pilipino | t | 29.3
PLW | 2881 | Koror | PLW | English | t | 3.2
PLW | 2881 | Koror | PLW | Palau | t | 82.2
POL | 2928 | Warszawa | POL | Polish | t | 97.6
PRI | 2919 | San Juan | PRI | Spanish | t | 51.3
PRK | 2318 | Pyongyang | PRK | Korean | t | 99.9
PRT | 2914 | Lisboa | PRT | Portuguese | t | 99
PRY | 2885 | Asuncion | PRY | Guarani | t | 40.1
PRY | 2885 | Asuncion | PRY | Spanish | t | 55.1
PYF | 3016 | Papeete | PYF | French | t | 40.8
QAT | 2973 | Doha | QAT | Arabic | t | 40.7
ROM | 3018 | Bucuresti | ROM | Romani | t | 0.7
ROM | 3018 | Bucuresti | ROM | Romanian | t | 90.7
RUS | 3580 | Moscow | RUS | Russian | t | 86.6
RWA | 3047 | Kigali | RWA | French | t | 0
RWA | 3047 | Kigali | RWA | Rwanda | t | 100
SAU | 3173 | Riyadh | SAU | Arabic | t | 95
SDN | 3225 | Khartum | SDN | Arabic | t | 49.4
SEN | 3198 | Dakar | SEN | Wolof | t | 48.1
SGP | 3208 | Singapore | SGP | Chinese | t | 77.1
SGP | 3208 | Singapore | SGP | Malay | t | 14.1
SGP | 3208 | Singapore | SGP | Tamil | t | 7.4
SHN | 3063 | Jamestown | SHN | English | t | 0
SJM | 938 | Longyearbyen | SJM | Norwegian | t | 0
SLV | 645 | San Salvador | SLV | Spanish | t | 100
SMR | 3171 | San Marino | SMR | Italian | t | 100
SOM | 3214 | Mogadishu | SOM | Arabic | t | 0
SOM | 3214 | Mogadishu | SOM | Somali | t | 98.3
SPM | 3067 | Saint-Pierre | SPM | French | t | 0
SVK | 3209 | Bratislava | SVK | Slovak | t | 85.6
SVN | 3212 | Ljubljana | SVN | Slovene | t | 87.9
SWE | 3048 | Stockholm | SWE | Swedish | t | 89.5
SWZ | 3244 | Mbabane | SWZ | Swazi | t | 89.9
SYC | 3206 | Victoria | SYC | English | t | 3.8
SYC | 3206 | Victoria | SYC | French | t | 1.3
SYR | 3250 | Damascus | SYR | Arabic | t | 90
TCA | 3423 | Cockburn Town | TCA | English | t | 0
TCD | 3337 | NDjamena | TCD | Arabic | t | 12.3
TGO | 3332 | Lome | TGO | Ewe | t | 23.2
TGO | 3332 | Lome | TGO | Kabye | t | 13.8
THA | 3320 | Bangkok | THA | Thai | t | 52.6
TJK | 3261 | Dushanbe | TJK | Tadzhik | t | 62.2
TKL | 3333 | Fakaofo | TKL | English | t | 0
TKM | 3419 | Ashgabat | TKM | Turkmenian | t | 76.7
TMP | 1522 | Dili | TMP | Portuguese | t | 0
TON | 3334 | Nukualofa | TON | English | t | 0
TON | 3334 | Nukualofa | TON | Tongan | t | 98.3
TUN | 3349 | Tunis | TUN | Arabic | t | 69.9
TUR | 3358 | Ankara | TUR | Turkish | t | 87.6
TUV | 3424 | Funafuti | TUV | English | t | 0
TUV | 3424 | Funafuti | TUV | Tuvalu | t | 92.5
TWN | 3263 | Taipei | TWN | Mandarin Chinese | t | 20.1
TZA | 3306 | Dodoma | TZA | Swahili | t | 8.8
UKR | 3426 | Kyiv | UKR | Ukrainian | t | 64.7
URY | 3492 | Montevideo | URY | Spanish | t | 95.7
USA | 3813 | Washington | USA | English | t | 86.2
UZB | 3503 | Toskent | UZB | Uzbek | t | 72.6
VAT | 3538 | Citta del Vaticano | VAT | Italian | t | 0
VCT | 3066 | Kingstown | VCT | English | t | 0
VEN | 3539 | Caracas | VEN | Spanish | t | 96.9
VGB | 537 | Road Town | VGB | English | t | 0
VIR | 4067 | Charlotte Amalie | VIR | English | t | 81.7
VNM | 3770 | Hanoi | VNM | Vietnamese | t | 86.8
VUT | 3537 | Port-Vila | VUT | Bislama | t | 56.6
VUT | 3537 | Port-Vila | VUT | English | t | 28.3
VUT | 3537 | Port-Vila | VUT | French | t | 14.2
WSM | 3169 | Apia | WSM | English | t | 0.6
WSM | 3169 | Apia | WSM | Samoan | t | 47.5
YEM | 1780 | Sanaa | YEM | Arabic | t | 99.6
YUG | 1792 | Beograd | YUG | Serbo-Croatian | t | 75.2
ZAF | 716 | Pretoria | ZAF | Afrikaans | t | 14.3
ZAF | 716 | Pretoria | ZAF | English | t | 8.5
ZAF | 716 | Pretoria | ZAF | Xhosa | t | 17.7
ZAF | 716 | Pretoria | ZAF | Zulu | t | 22.7
ZWE | 4068 | Harare | ZWE | English | t | 2.2
(237 rows)
--query2
with lang_total as
( select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
select lang_count,country.code,country.name,country.continent,country.region,country.population
from country left outer join lang_total
on (lang_total.code = country.code)
where country.indepyear > 1970 and lang_total.lang_count >=1 and lang_total.lang_count >=1
order by lang_total.lang_count desc;
lang_count | code | name | continent | region | population
------------+------+---------------------------------+-----------+-----------------+------------
12 | RUS | Russian Federation | Europe | Eastern Europe | 146934000
6 | FSM | Micronesia, Federated States of | Oceania | Micronesia | 119000
4 | AZE | Azerbaijan | Asia | Middle East | 7734000
1 | BIH | Bosnia and Herzegovina | Europe | Southern Europe | 3972000
(4 rows)
-- Using CTE in the WHERE clause
--query3
with lang_total as
( select lang_count,countrycode from
(
(
select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
UNION ALL
(
select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Monarchy')
group by country.code,countrylanguage.countrycode order by country.code)
) FOO1
)
select * from
(
select count(*) as cnt,country.code,country.name
from
country,countrylanguage
where country.code=countrylanguage.countrycode group by country.code,country.name) AS FOO
where foo.cnt = (select max(lang_count) from lang_total) order by foo.code;
cnt | code | name
-----+------+--------------------
12 | CAN | Canada
12 | CHN | China
12 | IND | India
12 | RUS | Russian Federation
12 | USA | United States
(5 rows)
--query4
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select diversecountries.name,city.name,diversecountries.CNT
from diversecountries,city where city.id = diversecountries.capital;
name | name | cnt
--------------------+------------------+-----
Austria | Wien | 8
Russian Federation | Moscow | 12
Italy | Roma | 8
Myanmar | Rangoon (Yangon) | 8
China | Peking | 12
Denmark | Kobenhavn | 7
Canada | Ottawa | 12
United States | Washington | 12
South Africa | Pretoria | 11
Iran | Teheran | 10
Australia | Canberra | 8
India | New Delhi | 12
(12 rows)
--query5 -Using a CTE in the select list
with bigcities as
(
select city.id,city.name,city.population,city.countrycode
from city
where city.population >= 0.5 * (select population from country where country.code=city.countrycode)
)
select
(select max(bigcities.population) from bigcities where bigcities.countrycode='QAT') as MAX_POP,
(select avg(bigcities.population) from bigcities) AS WORLD_AVG,
city.name,city.population
from
city where city.countrycode='QAT';
max_pop | world_avg | name | population
---------+---------------------+------+------------
355000 | 376916.466666666667 | Doha | 355000
(1 row)
--query6 using CTE in the select list with a qual
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select * from
(
select
(select max(CNT) from diversecountries where diversecountries.code = country.code) CNT,country.name COUNTRY,city.name CAPITAL
from country,city where country.capital = city.id) FOO where FOO.CNT is not null;
cnt | country | capital
-----+--------------------+------------------
8 | Austria | Wien
12 | Russian Federation | Moscow
12 | China | Peking
8 | Myanmar | Rangoon (Yangon)
10 | Iran | Teheran
11 | South Africa | Pretoria
12 | United States | Washington
8 | Italy | Roma
12 | Canada | Ottawa
7 | Denmark | Kobenhavn
8 | Australia | Canberra
12 | India | New Delhi
(12 rows)
--queries Using a CTE in the HAVING clause
with notdiversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) < 3) d
where d.countrycode = country.code and country.gnp > 100000)
select country.name COUNTRY,city.name CAPITAL,count(*) LANGCNT from
country,city,countrylanguage
where country.code = countrylanguage.countrycode and country.capital = city.id
group by country.name,city.name
HAVING count(*) NOT IN (select CNT from notdiversecountries where notdiversecountries.name = country.name)
order by country.name
LIMIT 10;
country | capital | langcnt
---------------------+------------------+---------
Afghanistan | Kabul | 5
Albania | Tirana | 3
Algeria | Alger | 2
American Samoa | Fagatogo | 3
Andorra | Andorra la Vella | 4
Angola | Luanda | 9
Anguilla | The Valley | 1
Antigua and Barbuda | Saint Johns | 2
Argentina | Buenos Aires | 3
Armenia | Yerevan | 2
(10 rows)
with somecheapasiandiversecountries as
(
select FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,count(*) ASIAN_COUNT from
(
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Southeast Asia'
and country.continent = 'Asia'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Eastern Asia'
and country.continent = 'Asia'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Middle East'
and country.continent = 'Asia'
) FOO, countrylanguage
where FOO.code = countrylanguage.countrycode
group by FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,countrylanguage.countrycode
HAVING count(*) >=
(select min(CNT) FROM
(select count(*) CNT,country.code from countrylanguage,country
where countrylanguage.countrycode=country.code
and country.continent = 'Asia'
and country.region = 'Southern and Central Asia'
group by country.code
) FOO1
)
)
select FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,count(*) COMPARED_WITH_CHEAP_ASIAN_CNT
from
(
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.continent = 'North America'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.continent = 'South America'
) FOO,countrylanguage
where FOO.code = countrylanguage.countrycode
group by FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate
HAVING count(*) >=
(select min(ASIAN_COUNT) FROM
(select ASIAN_COUNT FROM somecheapasiandiversecountries,country
where somecheapasiandiversecountries.code = country.code
and country.gnp >= country.gnpold
) ASIANCOUNT
)
order by COUNTRY;
code | country | capital | headofstate | compared_with_cheap_asian_cnt
------+----------------------------------+-------------------------+-----------------------------------+-------------------------------
ATG | Antigua and Barbuda | Saint Johns | Elisabeth II | 2
ARG | Argentina | Buenos Aires | Fernando de la Rua | 3
ABW | Aruba | Oranjestad | Beatrix | 4
BHS | Bahamas | Nassau | Elisabeth II | 2
BRB | Barbados | Bridgetown | Elisabeth II | 2
BLZ | Belize | Belmopan | Elisabeth II | 4
BOL | Bolivia | La Paz | Hugo Banzer Suarez | 4
BRA | Brazil | Brasilia | Fernando Henrique Cardoso | 5
CAN | Canada | Ottawa | Elisabeth II | 12
CHL | Chile | Santiago de Chile | Ricardo Lagos Escobar | 4
COL | Colombia | Santafe de Bogota | Andres Pastrana Arango | 5
CRI | Costa Rica | San Jose | Miguel Angel Rodriguez Echeverria | 4
DMA | Dominica | Roseau | Vernon Shaw | 2
DOM | Dominican Republic | Santo Domingo de Guzman | Hipolito Mejia Dominguez | 2
ECU | Ecuador | Quito | Gustavo Noboa Bejarano | 2
SLV | El Salvador | San Salvador | Francisco Guillermo Flores Perez | 2
GUF | French Guiana | Cayenne | Jacques Chirac | 2
GRL | Greenland | Nuuk | Margrethe II | 2
GLP | Guadeloupe | Basse-Terre | Jacques Chirac | 2
GTM | Guatemala | Ciudad de Guatemala | Alfonso Portillo Cabrera | 5
GUY | Guyana | Georgetown | Bharrat Jagdeo | 3
HTI | Haiti | Port-au-Prince | Jean-Bertrand Aristide | 2
HND | Honduras | Tegucigalpa | Carlos Roberto Flores Facusse | 4
JAM | Jamaica | Kingston | Elisabeth II | 2
MTQ | Martinique | Fort-de-France | Jacques Chirac | 2
MEX | Mexico | Ciudad de Mexico | Vicente Fox Quesada | 6
ANT | Netherlands Antilles | Willemstad | Beatrix | 3
NIC | Nicaragua | Managua | Arnoldo Aleman Lacayo | 4
PAN | Panama | Ciudad de Panama | Mireya Elisa Moscoso Rodriguez | 6
PRY | Paraguay | Asuncion | Luis Angel Gonzalez Macchi | 4
PER | Peru | Lima | Valentin Paniagua Corazao | 3
PRI | Puerto Rico | San Juan | George W. Bush | 2
KNA | Saint Kitts and Nevis | Basseterre | Elisabeth II | 2
LCA | Saint Lucia | Castries | Elisabeth II | 2
VCT | Saint Vincent and the Grenadines | Kingstown | Elisabeth II | 2
SUR | Suriname | Paramaribo | Ronald Venetiaan | 2
TTO | Trinidad and Tobago | Port-of-Spain | Arthur N. R. Robinson | 3
USA | United States | Washington | George W. Bush | 12
VEN | Venezuela | Caracas | Hugo Chavez Frias | 3
VIR | Virgin Islands, U.S. | Charlotte Amalie | George W. Bush | 3
(40 rows)
-- ensure select includes with clause in it's syntax
-- query 1 using the same name for the CTE as a table. Main query should reference the CTE
with country as
(select country.code,country.name COUNTRY, city.name CAPITAL, language, isofficial, percentage
FROM country,city,countrylanguage
WHERE country.code = countrylanguage.countrycode
and country.capital = city.id
and country.continent = 'Europe')
select * from
(select * from country where isofficial='True') e1,
(select * from country where percentage > 50) e2
where e1.code = e2.code order by e2.COUNTRY,e1.language LIMIT 20;
code | country | capital | language | isofficial | percentage | code | country | capital | language | isofficial | percentage
------+------------------------+------------------------+----------------+------------+------------+------+------------------------+------------------------+----------------+------------+------------
ALB | Albania | Tirana | Albaniana | t | 97.9 | ALB | Albania | Tirana | Albaniana | t | 97.9
AUT | Austria | Wien | German | t | 92 | AUT | Austria | Wien | German | t | 92
BLR | Belarus | Minsk | Belorussian | t | 65.6 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BLR | Belarus | Minsk | Russian | t | 32 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | French | t | 32.6 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | German | t | 1 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2 | BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2
BGR | Bulgaria | Sofija | Bulgariana | t | 83.2 | BGR | Bulgaria | Sofija | Bulgariana | t | 83.2
HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9 | HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Praha | Czech | t | 81.2
DNK | Denmark | Kobenhavn | Danish | t | 93.5 | DNK | Denmark | Kobenhavn | Danish | t | 93.5
EST | Estonia | Tallinn | Estonian | t | 65.3 | EST | Estonia | Tallinn | Estonian | t | 65.3
FRO | Faroe Islands | Torshavn | Danish | t | 0 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FRO | Faroe Islands | Torshavn | Faroese | t | 100 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FIN | Finland | Helsinki [Helsingfors] | Swedish | t | 5.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FRA | France | Paris | French | t | 93.6 | FRA | France | Paris | French | t | 93.6
DEU | Germany | Berlin | German | t | 91.3 | DEU | Germany | Berlin | German | t | 91.3
GIB | Gibraltar | Gibraltar | English | t | 88.9 | GIB | Gibraltar | Gibraltar | English | t | 88.9
(20 rows)
-- query 2 using multiple CTEs with same names as tables.
with country as
(select country.code,country.name COUNTRY, city.name CAPITAL, language, isofficial, percentage
FROM country,city,countrylanguage
WHERE country.code = countrylanguage.countrycode
and country.capital = city.id
and country.continent = 'Europe'),
countrylanguage(code1,country1,capital1,language1,isofficial1,percentage1,code2,country2,capital2,language2,isofficial2,percentage2) as
(
select * from
(select * from country where isofficial='True') e1,
(select * from country where percentage > 50) e2
where e1.code = e2.code order by e2.COUNTRY,e1.language
)
select code1,country1,capital1,language1,isofficial1,percentage1,country.COUNTRY from country,countrylanguage where country.code = countrylanguage.code1
and country.percentage = countrylanguage.percentage1
order by COUNTRY,percentage1 LIMIT 20;-- queries using same name for CTEs and the subquery aliases in the main query
code1 | country1 | capital1 | language1 | isofficial1 | percentage1 | country
-------+------------------------+------------------------+----------------+-------------+-------------+------------------------
ALB | Albania | Tirana | Albaniana | t | 97.9 | Albania
AUT | Austria | Wien | German | t | 92 | Austria
BLR | Belarus | Minsk | Russian | t | 32 | Belarus
BLR | Belarus | Minsk | Belorussian | t | 65.6 | Belarus
BEL | Belgium | Bruxelles [Brussel] | German | t | 1 | Belgium
BEL | Belgium | Bruxelles [Brussel] | French | t | 32.6 | Belgium
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | Belgium
BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2 | Bosnia and Herzegovina
BGR | Bulgaria | Sofija | Bulgariana | t | 83.2 | Bulgaria
HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9 | Croatia
CZE | Czech Republic | Praha | Czech | t | 81.2 | Czech Republic
DNK | Denmark | Kobenhavn | Danish | t | 93.5 | Denmark
EST | Estonia | Tallinn | Estonian | t | 65.3 | Estonia
FRO | Faroe Islands | Torshavn | Danish | t | 0 | Faroe Islands
FRO | Faroe Islands | Torshavn | Faroese | t | 100 | Faroe Islands
FIN | Finland | Helsinki [Helsingfors] | Swedish | t | 5.7 | Finland
FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7 | Finland
FRA | France | Paris | French | t | 93.6 | France
DEU | Germany | Berlin | German | t | 91.3 | Germany
GIB | Gibraltar | Gibraltar | English | t | 88.9 | Gibraltar
(20 rows)
-- query1
with c1 as
(select country.code,country.name COUNTRY, city.name CAPITAL, language, isofficial, percentage
FROM country,city,countrylanguage
WHERE country.code = countrylanguage.countrycode
and country.capital = city.id
and country.continent = 'Europe'),
c2 as
(select country.code,country.name COUNTRY, city.name CAPITAL, language, isofficial, percentage
FROM country,city,countrylanguage
WHERE country.code = countrylanguage.countrycode
and country.capital = city.id
and country.continent = 'Asia')
select * from
(select * from c1 where isofficial='True') c1,
(select * from c2 where percentage > 50) c2
where c1.percentage = c2.percentage order by c2.COUNTRY,c1.language;
code | country | capital | language | isofficial | percentage | code | country | capital | language | isofficial | percentage
------+---------------+------------+----------------+------------+------------+------+------------+---------+----------------+------------+------------
LIE | Liechtenstein | Vaduz | German | t | 89 | AZE | Azerbaijan | Baku | Azerbaijani | t | 89
AUT | Austria | Wien | German | t | 92 | CHN | China | Peking | Chinese | t | 92
ALB | Albania | Tirana | Albaniana | t | 97.9 | JOR | Jordan | Amman | Arabic | t | 97.9
SVK | Slovakia | Bratislava | Slovak | t | 85.6 | MAC | Macao | Macao | Canton Chinese | f | 85.6
FRO | Faroe Islands | Torshavn | Faroese | t | 100 | MDV | Maldives | Male | Dhivehi | t | 100
SMR | San Marino | San Marino | Italian | t | 100 | MDV | Maldives | Male | Dhivehi | t | 100
HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9 | PSE | Palestine | Gaza | Arabic | f | 95.9
(7 rows)
-- query2 using same names as tables
with country as
(select country.code,country.name COUNTRY, city.name CAPITAL, language, isofficial, percentage
FROM country,city,countrylanguage
WHERE country.code = countrylanguage.countrycode
and country.capital = city.id
and country.continent = 'Europe'),
countrylanguage as
(select country.code,country.COUNTRY,country.language,country.isofficial,country.percentage
FROM country,countrylanguage
WHERE country.code = countrylanguage.countrycode
)
select * from
(select * from country where isofficial='True') country,
(select * from countrylanguage where percentage > 50) countrylanguage
where country.percentage = countrylanguage.percentage order by countrylanguage.COUNTRY,country.language LIMIT 40;
code | country | capital | language | isofficial | percentage | code | country | language | isofficial | percentage
------+------------------------+---------------------+----------------+------------+------------+------+------------------------+----------------+------------+------------
ALB | Albania | Tirana | Albaniana | t | 97.9 | ALB | Albania | Albaniana | t | 97.9
ALB | Albania | Tirana | Albaniana | t | 97.9 | ALB | Albania | Albaniana | t | 97.9
ALB | Albania | Tirana | Albaniana | t | 97.9 | ALB | Albania | Albaniana | t | 97.9
AUT | Austria | Wien | German | t | 92 | AUT | Austria | German | t | 92
AUT | Austria | Wien | German | t | 92 | AUT | Austria | German | t | 92
AUT | Austria | Wien | German | t | 92 | AUT | Austria | German | t | 92
AUT | Austria | Wien | German | t | 92 | AUT | Austria | German | t | 92
AUT | Austria | Wien | German | t | 92 | AUT | Austria | German | t | 92
AUT | Austria | Wien | German | t | 92 | AUT | Austria | German | t | 92
AUT | Austria | Wien | German | t | 92 | AUT | Austria | German | t | 92
AUT | Austria | Wien | German | t | 92 | AUT | Austria | German | t | 92
BLR | Belarus | Minsk | Belorussian | t | 65.6 | BLR | Belarus | Belorussian | t | 65.6
BLR | Belarus | Minsk | Belorussian | t | 65.6 | BLR | Belarus | Belorussian | t | 65.6
BLR | Belarus | Minsk | Belorussian | t | 65.6 | BLR | Belarus | Belorussian | t | 65.6
BLR | Belarus | Minsk | Belorussian | t | 65.6 | BLR | Belarus | Belorussian | t | 65.6
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Dutch | t | 59.2
BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2 | BIH | Bosnia and Herzegovina | Serbo-Croatian | t | 99.2
BGR | Bulgaria | Sofija | Bulgariana | t | 83.2 | BGR | Bulgaria | Bulgariana | t | 83.2
BGR | Bulgaria | Sofija | Bulgariana | t | 83.2 | BGR | Bulgaria | Bulgariana | t | 83.2
BGR | Bulgaria | Sofija | Bulgariana | t | 83.2 | BGR | Bulgaria | Bulgariana | t | 83.2
BGR | Bulgaria | Sofija | Bulgariana | t | 83.2 | BGR | Bulgaria | Bulgariana | t | 83.2
HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9 | HRV | Croatia | Serbo-Croatian | t | 95.9
HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9 | HRV | Croatia | Serbo-Croatian | t | 95.9
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Czech | t | 81.2
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Czech | t | 81.2
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Czech | t | 81.2
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Czech | t | 81.2
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Czech | t | 81.2
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Czech | t | 81.2
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Czech | t | 81.2
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Czech | t | 81.2
DNK | Denmark | Kobenhavn | Danish | t | 93.5 | DNK | Denmark | Danish | t | 93.5
DNK | Denmark | Kobenhavn | Danish | t | 93.5 | DNK | Denmark | Danish | t | 93.5
DNK | Denmark | Kobenhavn | Danish | t | 93.5 | DNK | Denmark | Danish | t | 93.5
DNK | Denmark | Kobenhavn | Danish | t | 93.5 | DNK | Denmark | Danish | t | 93.5
(40 rows)
-- Using the same name for the CTE at every subquery-level
select avg(population12),CITY12
from
(
with city(CITY1,POPULATION1) as (select city.name,city.population from city where city.population >= 100)
select CITY12,POPULATION12 from
(
with city(CITY2,POPULATION2) as ( select city1,population1 from city where population1 >= 1000 )
select CITY12,POPULATION12 from
(
with city(CITY3,POPULATION3) as (select city2,population2 from city where population2 >= 10000)
select CITY12,POPULATION12 from
(
with city(CITY4,POPULATION4) as (select city3,population3 from city where population3 >= 20000)
select CITY12,POPULATION12 from
(
with city(CITY5,POPULATION5) as (select city4,population4 from city where population4 >= 50000)
select CITY12,POPULATION12 from
(
with city(CITY6,POPULATION6) as (select city5,population5 from city where population5 >= 80000)
select CITY12,POPULATION12 from
(
with city(CITY7,POPULATION7) as (select city6,population6 from city where population6 >= 150000)
select CITY12,POPULATION12 from
(
with city(CITY8,POPULATION8) as (select city7,population7 from city where population7 >= 200000)
select CITY12,POPULATION12 from
(
with city(CITY9,POPULATION9) as (select city8,population8 from city where population8 >= 250000)
select CITY12,POPULATION12 from
(
with city(CITY10,POPULATION10) as (select city9,population9 from city where population9 >= 300000)
select city12,population12 from
(
with city(CITY11,POPULATION11) as (select city10,population10 from city where population10 >= 6500000)
select CITY12,POPULATION12 from
(
with city(CITY12,POPULATION12) as (select city11,population11 from city where population11 >= 7000000)
select s1.city12,s1.population12 from city s1,city s2
) FOO11
) FOO10
) FOO9
) FOO8
) FOO7
) FOO6
) FOO5
) FOO4
)FOO3
) FOO2
) FOO1
) FOO0 group by city12 order by city12;-- negative cases where queries have duplicate names in CTEs
avg | city12
-----------------------+------------------
8591309.000000000000 | Ciudad de Mexico
7206704.000000000000 | Delhi
8787958.000000000000 | Istanbul
9604900.000000000000 | Jakarta
9269265.000000000000 | Karachi
7285000.000000000000 | London
8389200.000000000000 | Moscow
10500000.000000000000 | Mumbai (Bombay)
8008278.000000000000 | New York
7472000.000000000000 | Peking
9968485.000000000000 | Sao Paulo
9981619.000000000000 | Seoul
9696300.000000000000 | Shanghai
7980230.000000000000 | Tokyo
(14 rows)
-- Tests for duplicate column aliases
with capitals as
(select country.code,id,city.name,city.countrycode as code from city,country
where city.countrycode = country.code AND city.id = country.capital)
select * from capitals where id < 100;
code | id | name | code
------+----+------------------+------
NLD | 5 | Amsterdam | NLD
ALB | 34 | Tirana | ALB
DZA | 35 | Alger | DZA
AND | 55 | Andorra la Vella | AND
AIA | 62 | The Valley | AIA
ATG | 63 | Saint Johns | ATG
AFG | 1 | Kabul | AFG
ARE | 65 | Abu Dhabi | ARE
ARG | 69 | Buenos Aires | ARG
ANT | 33 | Willemstad | ANT
ASM | 54 | Fagatogo | ASM
AGO | 56 | Luanda | AGO
(12 rows)
with allofficiallanguages as
(select countrylanguage.countrycode,city.countrycode,language from
city,countrylanguage where countrylanguage.countrycode = city.countrycode and isofficial = 'True')
select * from allofficiallanguages where language like 'A%';
countrycode | countrycode | language
-------------+-------------+-------------
DZA | DZA | Arabic
DZA | DZA | Arabic
DZA | DZA | Arabic
DZA | DZA | Arabic
DZA | DZA | Arabic
ARM | ARM | Armenian
AZE | AZE | Azerbaijani
IRQ | IRQ | Arabic
IRQ | IRQ | Arabic
IRQ | IRQ | Arabic
IRQ | IRQ | Arabic
IRQ | IRQ | Arabic
IRQ | IRQ | Arabic
IRQ | IRQ | Arabic
IRQ | IRQ | Arabic
KWT | KWT | Arabic
KWT | KWT | Arabic
SOM | SOM | Arabic
DJI | DJI | Arabic
BHR | BHR | Arabic
JOR | JOR | Arabic
JOR | JOR | Arabic
JOR | JOR | Arabic
JOR | JOR | Arabic
OMN | OMN | Arabic
OMN | OMN | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SDN | SDN | Arabic
SDN | SDN | Arabic
SDN | SDN | Arabic
TCD | TCD | Arabic
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ARE | ARE | Arabic
ARE | ARE | Arabic
ARE | ARE | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
YEM | YEM | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
SYR | SYR | Arabic
SYR | SYR | Arabic
SYR | SYR | Arabic
SYR | SYR | Arabic
TUN | TUN | Arabic
ISR | ISR | Arabic
ISR | ISR | Arabic
ISR | ISR | Arabic
ISR | ISR | Arabic
BOL | BOL | Aimara
BOL | BOL | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
DZA | DZA | Arabic
DZA | DZA | Arabic
DZA | DZA | Arabic
DZA | DZA | Arabic
DZA | DZA | Arabic
DZA | DZA | Arabic
DZA | DZA | Arabic
DZA | DZA | Arabic
DZA | DZA | Arabic
ARM | ARM | Armenian
IRQ | IRQ | Arabic
LBY | LBY | Arabic
LBY | LBY | Arabic
OMN | OMN | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SDN | SDN | Arabic
SDN | SDN | Arabic
SDN | SDN | Arabic
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ALB | ALB | Albaniana
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
YEM | YEM | Arabic
YEM | YEM | Arabic
LBN | LBN | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
SYR | SYR | Arabic
SYR | SYR | Arabic
SYR | SYR | Arabic
SYR | SYR | Arabic
TUN | TUN | Arabic
TUN | TUN | Arabic
TUN | TUN | Arabic
TUN | TUN | Arabic
TUN | TUN | Arabic
ISR | ISR | Arabic
ISR | ISR | Arabic
ISR | ISR | Arabic
BOL | BOL | Aimara
BOL | BOL | Aimara
BOL | BOL | Aimara
BOL | BOL | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
DZA | DZA | Arabic
DZA | DZA | Arabic
DZA | DZA | Arabic
DZA | DZA | Arabic
ARM | ARM | Armenian
AZE | AZE | Azerbaijani
AZE | AZE | Azerbaijani
AZE | AZE | Azerbaijani
IRQ | IRQ | Arabic
IRQ | IRQ | Arabic
IRQ | IRQ | Arabic
IRQ | IRQ | Arabic
IRQ | IRQ | Arabic
IRQ | IRQ | Arabic
KWT | KWT | Arabic
ESH | ESH | Arabic
SOM | SOM | Arabic
SOM | SOM | Arabic
JOR | JOR | Arabic
LBY | LBY | Arabic
LBY | LBY | Arabic
OMN | OMN | Arabic
OMN | OMN | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SAU | SAU | Arabic
SDN | SDN | Arabic
SDN | SDN | Arabic
SDN | SDN | Arabic
SDN | SDN | Arabic
SDN | SDN | Arabic
SDN | SDN | Arabic
TCD | TCD | Arabic
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ZAF | ZAF | Afrikaans
ARE | ARE | Arabic
ARE | ARE | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
EGY | EGY | Arabic
YEM | YEM | Arabic
YEM | YEM | Arabic
YEM | YEM | Arabic
LBN | LBN | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
MAR | MAR | Arabic
QAT | QAT | Arabic
SYR | SYR | Arabic
SYR | SYR | Arabic
SYR | SYR | Arabic
TUN | TUN | Arabic
TUN | TUN | Arabic
ISR | ISR | Arabic
ISR | ISR | Arabic
ISR | ISR | Arabic
ISR | ISR | Arabic
ISR | ISR | Arabic
ISR | ISR | Arabic
ISR | ISR | Arabic
BOL | BOL | Aimara
BOL | BOL | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
PER | PER | Aimara
(282 rows)
with capitals(code,id,name,code) as
(select country.code,id,city.name,city.countrycode from city,country
where city.countrycode = country.code AND city.id = country.capital)
select * from capitals where id < 100;
code | id | name | code
------+----+------------------+------
NLD | 5 | Amsterdam | NLD
ALB | 34 | Tirana | ALB
DZA | 35 | Alger | DZA
AND | 55 | Andorra la Vella | AND
AIA | 62 | The Valley | AIA
ATG | 63 | Saint Johns | ATG
AFG | 1 | Kabul | AFG
ARE | 65 | Abu Dhabi | ARE
ARG | 69 | Buenos Aires | ARG
ANT | 33 | Willemstad | ANT
ASM | 54 | Fagatogo | ASM
AGO | 56 | Luanda | AGO
(12 rows)
-- query1 CTE referencing itself
with lang_total as
( select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode
HAVING count(*) > (select max(lang_count) from lang_total)
)
select count(*) from lang_total;
ERROR: relation "lang_total" does not exist
LINE 5: HAVING count(*) > (select max(lang_count) from lang_total)
^
DETAIL: There is a WITH item named "lang_total", but it cannot be referenced from this part of the query.
HINT: Re-order the WITH items to remove forward references.
--query2 CTE forward referencing another CTE
with capitals as
(select country.code,id,city.name from city,country
where city.countrycode = country.code AND city.id = country.capital),
lang_total as
( select count(*) as lang_count,country.code,alleuropeanlanguages.code
from country join alleuropeanlanguages on (country.code=alleuropeanlanguages.code and governmentform='Federal Republic')
group by country.code,alleuropeanlanguages.code order by country.code),
alleuropeanlanguages as
(select country.code,country.name COUNTRY, city.name CAPITAL, language, isofficial, percentage
FROM country,city,countrylanguage
WHERE country.code = countrylanguage.countrycode
and country.capital = city.id
and country.continent = 'Europe')
select * from alleuropeanlanguages;-- negative cases with mismatching column list provided for CTEs
ERROR: relation "alleuropeanlanguages" does not exist
LINE 6: from country join alleuropeanlanguages on (country.code=al...
^
DETAIL: There is a WITH item named "alleuropeanlanguages", but it cannot be referenced from this part of the query.
HINT: Re-order the WITH items to remove forward references.
--query1
with capitals(code,id) as
(select country.code,id,city.name from city,country
where city.countrycode = country.code AND city.id = country.capital)
select * from capitals;
code | id | name
------+------+-----------------------------------
AIA | 62 | The Valley
ARE | 65 | Abu Dhabi
AUS | 135 | Canberra
BHS | 148 | Nassau
BHR | 149 | al-Manama
BGD | 150 | Dhaka
BLZ | 185 | Belmopan
BWA | 204 | Gaborone
CYM | 553 | George Town
CHL | 554 | Santiago de Chile
COK | 583 | Avarua
CRI | 584 | San Jose
DJI | 585 | Djibouti
DMA | 586 | Roseau
ERI | 652 | Asmara
ETH | 756 | Addis Abeba
GAB | 902 | Libreville
GEO | 905 | Tbilisi
GIB | 915 | Gibraltar
GLP | 919 | Basse-Terre
GUM | 921 | Agaaa
HTI | 929 | Port-au-Prince
SJM | 938 | Longyearbyen
IDN | 939 | Jakarta
IND | 1109 | New Delhi
IRN | 1380 | Teheran
JAM | 1530 | Kingston
JPN | 1532 | Tokyo
CAF | 1889 | Bangui
COM | 2295 | Moroni
COD | 2298 | Kinshasa
CYP | 2430 | Nicosia
LVA | 2434 | Riga
LBN | 2438 | Beirut
LIE | 2446 | Vaduz
MAC | 2454 | Macao
MWI | 2462 | Lilongwe
MLI | 2482 | Bamako
MLT | 2484 | Valletta
MAR | 2486 | Rabat
MTQ | 2508 | Fort-de-France
MRT | 2509 | Nouakchott
MCO | 2695 | Monaco-Ville
MOZ | 2698 | Maputo
NPL | 2729 | Kathmandu
NIU | 2805 | Alofi
NFK | 2806 | Kingston
CIV | 2814 | Yamoussoukro
PAK | 2831 | Islamabad
PLW | 2881 | Koror
PER | 2890 | Lima
GNQ | 2972 | Malabo
QAT | 2973 | Doha
GUF | 3014 | Cayenne
SHN | 3063 | Jamestown
LCA | 3065 | Castries
VCT | 3066 | Kingstown
STP | 3172 | Sao Tome
SAU | 3173 | Riyadh
SEN | 3198 | Dakar
SVN | 3212 | Ljubljana
SUR | 3243 | Paramaribo
SWZ | 3244 | Mbabane
SYR | 3250 | Damascus
THA | 3320 | Bangkok
TKL | 3333 | Fakaofo
TON | 3334 | Nukualofa
TTO | 3336 | Port-of-Spain
TUN | 3349 | Tunis
TUR | 3358 | Ankara
TKM | 3419 | Ashgabat
UKR | 3426 | Kyiv
NCL | 3493 | Noumea
WLF | 3536 | Mata-Utu
EST | 3791 | Tallinn
AFG | 1 | Kabul
ANT | 33 | Willemstad
ALB | 34 | Tirana
DZA | 35 | Alger
ATG | 63 | Saint Johns
ARG | 69 | Buenos Aires
BRB | 174 | Bridgetown
BEN | 187 | Porto-Novo
BTN | 192 | Thimphu
BOL | 194 | La Paz
GBR | 456 | London
VGB | 537 | Road Town
BRN | 538 | Bandar Seri Begawan
BDI | 552 | Bujumbura
DOM | 587 | Santo Domingo de Guzman
ECU | 594 | Quito
EGY | 608 | Cairo
SLV | 645 | San Salvador
PHL | 766 | Manila
GMB | 904 | Banjul
GRD | 916 | Saint Georges
GTM | 922 | Ciudad de Guatemala
HND | 933 | Tegucigalpa
IRQ | 1365 | Baghdad
ISR | 1450 | Jerusalem
ITA | 1464 | Roma
AUT | 1523 | Wien
YEM | 1780 | Sanaa
CXR | 1791 | Flying Fish Cove
YUG | 1792 | Beograd
KAZ | 1864 | Astana
COG | 2296 | Brazzaville
CCK | 2317 | West Island
GRC | 2401 | Athenai
HRV | 2409 | Zagreb
LBR | 2440 | Monrovia
LBY | 2441 | Tripoli
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg]
MKD | 2460 | Skopje
MYS | 2464 | Kuala Lumpur
MUS | 2511 | Port-Louis
FSM | 2689 | Palikir
MSR | 2697 | Plymouth
NAM | 2726 | Windhoek
NRU | 2728 | Yaren
NIC | 2734 | Managua
NGA | 2754 | Abuja
PNG | 2884 | Port Moresby
PRY | 2885 | Asuncion
PCN | 2912 | Adamstown
POL | 2928 | Warszawa
FRA | 2974 | Paris
REU | 3017 | Saint-Denis
ROM | 3018 | Bucuresti
SPM | 3067 | Saint-Pierre
WSM | 3169 | Apia
SMR | 3171 | San Marino
SGP | 3208 | Singapore
SVK | 3209 | Bratislava
LKA | 3217 | Colombo
SDN | 3225 | Khartum
CHE | 3248 | Bern
TGO | 3332 | Lome
TCD | 3337 | NDjamena
CZE | 3339 | Praha
TCA | 3423 | Cockburn Town
TUV | 3424 | Funafuti
URY | 3492 | Montevideo
NZL | 3499 | Wellington
VUT | 3537 | Port-Vila
VAT | 3538 | Citta del Vaticano
VEN | 3539 | Caracas
RUS | 3580 | Moscow
VNM | 3770 | Hanoi
NLD | 5 | Amsterdam
ASM | 54 | Fagatogo
AND | 55 | Andorra la Vella
AGO | 56 | Luanda
ARM | 126 | Yerevan
ABW | 129 | Oranjestad
AZE | 144 | Baku
BEL | 179 | Bruxelles [Brussel]
BMU | 191 | Hamilton
BIH | 201 | Sarajevo
BRA | 211 | Brasilia
BGR | 539 | Sofija
BFA | 549 | Ouagadougou
ESP | 653 | Madrid
ZAF | 716 | Pretoria
FLK | 763 | Stanley
FJI | 764 | Suva
FRO | 901 | Torshavn
GHA | 910 | Accra
GRL | 917 | Nuuk
GIN | 926 | Conakry
GNB | 927 | Bissau
GUY | 928 | Georgetown
HKG | 937 | Victoria
IRL | 1447 | Dublin
ISL | 1449 | Reykjavik
TMP | 1522 | Dili
JOR | 1786 | Amman
KHM | 1800 | Phnom Penh
CMR | 1804 | Yaounde
CAN | 1822 | Ottawa
CPV | 1859 | Praia
KEN | 1881 | Nairobi
CHN | 1891 | Peking
KGZ | 2253 | Bishkek
KIR | 2256 | Bairiki
COL | 2257 | Santafe de Bogota
PRK | 2318 | Pyongyang
KOR | 2331 | Seoul
CUB | 2413 | La Habana
KWT | 2429 | Kuwait
LAO | 2432 | Vientiane
LSO | 2437 | Maseru
LTU | 2447 | Vilnius
ESH | 2453 | El-Aaiun
MDG | 2455 | Antananarivo
MDV | 2463 | Male
MHL | 2507 | Dalap-Uliga-Darrit
MYT | 2514 | Mamoutzou
MEX | 2515 | Ciudad de Mexico
MDA | 2690 | Chisinau
MNG | 2696 | Ulan Bator
MMR | 2710 | Rangoon (Yangon)
NER | 2738 | Niamey
NOR | 2807 | Oslo
OMN | 2821 | Masqat
PAN | 2882 | Ciudad de Panama
MNP | 2913 | Garapan
PRT | 2914 | Lisboa
PRI | 2919 | San Juan
PYF | 3016 | Papeete
RWA | 3047 | Kigali
SWE | 3048 | Stockholm
KNA | 3064 | Basseterre
DEU | 3068 | Berlin
SLB | 3161 | Honiara
ZMB | 3162 | Lusaka
SYC | 3206 | Victoria
SLE | 3207 | Freetown
SOM | 3214 | Mogadishu
FIN | 3236 | Helsinki [Helsingfors]
TJK | 3261 | Dushanbe
TWN | 3263 | Taipei
TZA | 3306 | Dodoma
DNK | 3315 | Kobenhavn
UGA | 3425 | Kampala
HUN | 3483 | Budapest
UZB | 3503 | Toskent
BLR | 3520 | Minsk
USA | 3813 | Washington
VIR | 4067 | Charlotte Amalie
ZWE | 4068 | Harare
PSE | 4074 | Gaza
(232 rows)
--query 2
with lang_total(lang_count,code,countrycode,name) as
( select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
select * from lang_total;
ERROR: WITH query "lang_total" has 3 columns available but 4 columns specified
LINE 1: with lang_total(lang_count,code,countrycode,name) as
^
-- queries with CTEs using hash joins
set enable_nestloop=off;
set enable_hashjoin=on;
set enable_mergejoin=off;
--query1
with capitals as
(select country.code,id,city.name from city,country
where city.countrycode = country.code AND city.id = country.capital)
select * from
capitals,countrylanguage
where capitals.code = countrylanguage.countrycode and isofficial='true'
order by capitals.code,countrylanguage.language;
code | id | name | countrycode | language | isofficial | percentage
------+------+-----------------------------------+-------------+------------------+------------+------------
ABW | 129 | Oranjestad | ABW | Dutch | t | 5.3
AFG | 1 | Kabul | AFG | Dari | t | 32.1
AFG | 1 | Kabul | AFG | Pashto | t | 52.4
AIA | 62 | The Valley | AIA | English | t | 0
ALB | 34 | Tirana | ALB | Albaniana | t | 97.9
AND | 55 | Andorra la Vella | AND | Catalan | t | 32.3
ANT | 33 | Willemstad | ANT | Dutch | t | 0
ANT | 33 | Willemstad | ANT | Papiamento | t | 86.2
ARE | 65 | Abu Dhabi | ARE | Arabic | t | 42
ARG | 69 | Buenos Aires | ARG | Spanish | t | 96.8
ARM | 126 | Yerevan | ARM | Armenian | t | 93.4
ASM | 54 | Fagatogo | ASM | English | t | 3.1
ASM | 54 | Fagatogo | ASM | Samoan | t | 90.6
ATG | 63 | Saint Johns | ATG | English | t | 0
AUS | 135 | Canberra | AUS | English | t | 81.2
AUT | 1523 | Wien | AUT | German | t | 92
AZE | 144 | Baku | AZE | Azerbaijani | t | 89
BDI | 552 | Bujumbura | BDI | French | t | 0
BDI | 552 | Bujumbura | BDI | Kirundi | t | 98.1
BEL | 179 | Bruxelles [Brussel] | BEL | Dutch | t | 59.2
BEL | 179 | Bruxelles [Brussel] | BEL | French | t | 32.6
BEL | 179 | Bruxelles [Brussel] | BEL | German | t | 1
BGD | 150 | Dhaka | BGD | Bengali | t | 97.7
BGR | 539 | Sofija | BGR | Bulgariana | t | 83.2
BHR | 149 | al-Manama | BHR | Arabic | t | 67.7
BIH | 201 | Sarajevo | BIH | Serbo-Croatian | t | 99.2
BLR | 3520 | Minsk | BLR | Belorussian | t | 65.6
BLR | 3520 | Minsk | BLR | Russian | t | 32
BLZ | 185 | Belmopan | BLZ | English | t | 50.8
BMU | 191 | Hamilton | BMU | English | t | 100
BOL | 194 | La Paz | BOL | Aimara | t | 3.2
BOL | 194 | La Paz | BOL | Ketdua | t | 8.1
BOL | 194 | La Paz | BOL | Spanish | t | 87.7
BRA | 211 | Brasilia | BRA | Portuguese | t | 97.5
BRB | 174 | Bridgetown | BRB | English | t | 0
BRN | 538 | Bandar Seri Begawan | BRN | Malay | t | 45.5
BTN | 192 | Thimphu | BTN | Dzongkha | t | 50
CAN | 1822 | Ottawa | CAN | English | t | 60.4
CAN | 1822 | Ottawa | CAN | French | t | 23.4
CCK | 2317 | West Island | CCK | English | t | 0
CHE | 3248 | Bern | CHE | French | t | 19.2
CHE | 3248 | Bern | CHE | German | t | 63.6
CHE | 3248 | Bern | CHE | Italian | t | 7.7
CHE | 3248 | Bern | CHE | Romansh | t | 0.6
CHL | 554 | Santiago de Chile | CHL | Spanish | t | 89.7
CHN | 1891 | Peking | CHN | Chinese | t | 92
COK | 583 | Avarua | COK | Maori | t | 0
COL | 2257 | Santafe de Bogota | COL | Spanish | t | 99
COM | 2295 | Moroni | COM | Comorian | t | 75
CPV | 1859 | Praia | CPV | Portuguese | t | 0
CRI | 584 | San Jose | CRI | Spanish | t | 97.5
CUB | 2413 | La Habana | CUB | Spanish | t | 100
CXR | 1791 | Flying Fish Cove | CXR | English | t | 0
CYM | 553 | George Town | CYM | English | t | 0
CYP | 2430 | Nicosia | CYP | Greek | t | 74.1
CYP | 2430 | Nicosia | CYP | Turkish | t | 22.4
CZE | 3339 | Praha | CZE | Czech | t | 81.2
DEU | 3068 | Berlin | DEU | German | t | 91.3
DJI | 585 | Djibouti | DJI | Arabic | t | 10.6
DNK | 3315 | Kobenhavn | DNK | Danish | t | 93.5
DOM | 587 | Santo Domingo de Guzman | DOM | Spanish | t | 98
DZA | 35 | Alger | DZA | Arabic | t | 86
ECU | 594 | Quito | ECU | Spanish | t | 93
EGY | 608 | Cairo | EGY | Arabic | t | 98.8
ERI | 652 | Asmara | ERI | Tigrinja | t | 49.1
ESH | 2453 | El-Aaiun | ESH | Arabic | t | 100
ESP | 653 | Madrid | ESP | Spanish | t | 74.4
EST | 3791 | Tallinn | EST | Estonian | t | 65.3
FIN | 3236 | Helsinki [Helsingfors] | FIN | Finnish | t | 92.7
FIN | 3236 | Helsinki [Helsingfors] | FIN | Swedish | t | 5.7
FJI | 764 | Suva | FJI | Fijian | t | 50.8
FLK | 763 | Stanley | FLK | English | t | 0
FRA | 2974 | Paris | FRA | French | t | 93.6
FRO | 901 | Torshavn | FRO | Danish | t | 0
FRO | 901 | Torshavn | FRO | Faroese | t | 100
GBR | 456 | London | GBR | English | t | 97.3
GEO | 905 | Tbilisi | GEO | Georgiana | t | 71.7
GIB | 915 | Gibraltar | GIB | English | t | 88.9
GLP | 919 | Basse-Terre | GLP | French | t | 0
GNB | 927 | Bissau | GNB | Portuguese | t | 8.1
GRC | 2401 | Athenai | GRC | Greek | t | 98.5
GRL | 917 | Nuuk | GRL | Danish | t | 12.5
GRL | 917 | Nuuk | GRL | Greenlandic | t | 87.5
GTM | 922 | Ciudad de Guatemala | GTM | Spanish | t | 64.7
GUM | 921 | Agaaa | GUM | Chamorro | t | 29.6
GUM | 921 | Agaaa | GUM | English | t | 37.5
HKG | 937 | Victoria | HKG | English | t | 2.2
HND | 933 | Tegucigalpa | HND | Spanish | t | 97.2
HRV | 2409 | Zagreb | HRV | Serbo-Croatian | t | 95.9
HTI | 929 | Port-au-Prince | HTI | French | t | 0
HUN | 3483 | Budapest | HUN | Hungarian | t | 98.5
IDN | 939 | Jakarta | IDN | Malay | t | 12.1
IND | 1109 | New Delhi | IND | Hindi | t | 39.9
IRL | 1447 | Dublin | IRL | English | t | 98.4
IRL | 1447 | Dublin | IRL | Irish | t | 1.6
IRN | 1380 | Teheran | IRN | Persian | t | 45.7
IRQ | 1365 | Baghdad | IRQ | Arabic | t | 77.2
ISL | 1449 | Reykjavik | ISL | Icelandic | t | 95.7
ISR | 1450 | Jerusalem | ISR | Arabic | t | 18
ISR | 1450 | Jerusalem | ISR | Hebrew | t | 63.1
ITA | 1464 | Roma | ITA | Italian | t | 94.1
JOR | 1786 | Amman | JOR | Arabic | t | 97.9
JPN | 1532 | Tokyo | JPN | Japanese | t | 99.1
KAZ | 1864 | Astana | KAZ | Kazakh | t | 46
KGZ | 2253 | Bishkek | KGZ | Kirgiz | t | 59.7
KGZ | 2253 | Bishkek | KGZ | Russian | t | 16.2
KHM | 1800 | Phnom Penh | KHM | Khmer | t | 88.6
KIR | 2256 | Bairiki | KIR | Kiribati | t | 98.9
KNA | 3064 | Basseterre | KNA | English | t | 0
KOR | 2331 | Seoul | KOR | Korean | t | 99.9
KWT | 2429 | Kuwait | KWT | Arabic | t | 78.1
LAO | 2432 | Vientiane | LAO | Lao | t | 67.2
LBN | 2438 | Beirut | LBN | Arabic | t | 93
LBY | 2441 | Tripoli | LBY | Arabic | t | 96
LCA | 3065 | Castries | LCA | English | t | 20
LIE | 2446 | Vaduz | LIE | German | t | 89
LKA | 3217 | Colombo | LKA | Singali | t | 60.3
LKA | 3217 | Colombo | LKA | Tamil | t | 19.6
LSO | 2437 | Maseru | LSO | English | t | 0
LSO | 2437 | Maseru | LSO | Sotho | t | 85
LTU | 2447 | Vilnius | LTU | Lithuanian | t | 81.6
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | French | t | 4.2
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | German | t | 2.3
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | Luxembourgish | t | 64.4
LVA | 2434 | Riga | LVA | Latvian | t | 55.1
MAC | 2454 | Macao | MAC | Portuguese | t | 2.3
MAR | 2486 | Rabat | MAR | Arabic | t | 65
MCO | 2695 | Monaco-Ville | MCO | French | t | 41.9
MDA | 2690 | Chisinau | MDA | Romanian | t | 61.9
MDG | 2455 | Antananarivo | MDG | French | t | 0
MDG | 2455 | Antananarivo | MDG | Malagasy | t | 98.9
MDV | 2463 | Male | MDV | Dhivehi | t | 100
MEX | 2515 | Ciudad de Mexico | MEX | Spanish | t | 92.1
MHL | 2507 | Dalap-Uliga-Darrit | MHL | English | t | 0
MHL | 2507 | Dalap-Uliga-Darrit | MHL | Marshallese | t | 96.8
MKD | 2460 | Skopje | MKD | Macedonian | t | 66.5
MLT | 2484 | Valletta | MLT | English | t | 2.1
MLT | 2484 | Valletta | MLT | Maltese | t | 95.8
MMR | 2710 | Rangoon (Yangon) | MMR | Burmese | t | 69
MNG | 2696 | Ulan Bator | MNG | Mongolian | t | 78.8
MNP | 2913 | Garapan | MNP | English | t | 4.8
MSR | 2697 | Plymouth | MSR | English | t | 0
MTQ | 2508 | Fort-de-France | MTQ | French | t | 0
MWI | 2462 | Lilongwe | MWI | Chichewa | t | 58.3
MYS | 2464 | Kuala Lumpur | MYS | Malay | t | 58.4
MYT | 2514 | Mamoutzou | MYT | French | t | 20.3
NCL | 3493 | Noumea | NCL | French | t | 34.3
NFK | 2806 | Kingston | NFK | English | t | 0
NIC | 2734 | Managua | NIC | Spanish | t | 97.6
NIU | 2805 | Alofi | NIU | English | t | 0
NLD | 5 | Amsterdam | NLD | Dutch | t | 95.6
NOR | 2807 | Oslo | NOR | Norwegian | t | 96.6
NPL | 2729 | Kathmandu | NPL | Nepali | t | 50.4
NRU | 2728 | Yaren | NRU | English | t | 7.5
NRU | 2728 | Yaren | NRU | Nauru | t | 57.5
NZL | 3499 | Wellington | NZL | English | t | 87
OMN | 2821 | Masqat | OMN | Arabic | t | 76.7
PAK | 2831 | Islamabad | PAK | Urdu | t | 7.6
PAN | 2882 | Ciudad de Panama | PAN | Spanish | t | 76.8
PER | 2890 | Lima | PER | Aimara | t | 2.3
PER | 2890 | Lima | PER | Ketdua | t | 16.4
PER | 2890 | Lima | PER | Spanish | t | 79.8
PHL | 766 | Manila | PHL | Pilipino | t | 29.3
PLW | 2881 | Koror | PLW | English | t | 3.2
PLW | 2881 | Koror | PLW | Palau | t | 82.2
POL | 2928 | Warszawa | POL | Polish | t | 97.6
PRI | 2919 | San Juan | PRI | Spanish | t | 51.3
PRK | 2318 | Pyongyang | PRK | Korean | t | 99.9
PRT | 2914 | Lisboa | PRT | Portuguese | t | 99
PRY | 2885 | Asuncion | PRY | Guarani | t | 40.1
PRY | 2885 | Asuncion | PRY | Spanish | t | 55.1
PYF | 3016 | Papeete | PYF | French | t | 40.8
QAT | 2973 | Doha | QAT | Arabic | t | 40.7
ROM | 3018 | Bucuresti | ROM | Romani | t | 0.7
ROM | 3018 | Bucuresti | ROM | Romanian | t | 90.7
RUS | 3580 | Moscow | RUS | Russian | t | 86.6
RWA | 3047 | Kigali | RWA | French | t | 0
RWA | 3047 | Kigali | RWA | Rwanda | t | 100
SAU | 3173 | Riyadh | SAU | Arabic | t | 95
SDN | 3225 | Khartum | SDN | Arabic | t | 49.4
SEN | 3198 | Dakar | SEN | Wolof | t | 48.1
SGP | 3208 | Singapore | SGP | Chinese | t | 77.1
SGP | 3208 | Singapore | SGP | Malay | t | 14.1
SGP | 3208 | Singapore | SGP | Tamil | t | 7.4
SHN | 3063 | Jamestown | SHN | English | t | 0
SJM | 938 | Longyearbyen | SJM | Norwegian | t | 0
SLV | 645 | San Salvador | SLV | Spanish | t | 100
SMR | 3171 | San Marino | SMR | Italian | t | 100
SOM | 3214 | Mogadishu | SOM | Arabic | t | 0
SOM | 3214 | Mogadishu | SOM | Somali | t | 98.3
SPM | 3067 | Saint-Pierre | SPM | French | t | 0
SVK | 3209 | Bratislava | SVK | Slovak | t | 85.6
SVN | 3212 | Ljubljana | SVN | Slovene | t | 87.9
SWE | 3048 | Stockholm | SWE | Swedish | t | 89.5
SWZ | 3244 | Mbabane | SWZ | Swazi | t | 89.9
SYC | 3206 | Victoria | SYC | English | t | 3.8
SYC | 3206 | Victoria | SYC | French | t | 1.3
SYR | 3250 | Damascus | SYR | Arabic | t | 90
TCA | 3423 | Cockburn Town | TCA | English | t | 0
TCD | 3337 | NDjamena | TCD | Arabic | t | 12.3
TGO | 3332 | Lome | TGO | Ewe | t | 23.2
TGO | 3332 | Lome | TGO | Kabye | t | 13.8
THA | 3320 | Bangkok | THA | Thai | t | 52.6
TJK | 3261 | Dushanbe | TJK | Tadzhik | t | 62.2
TKL | 3333 | Fakaofo | TKL | English | t | 0
TKM | 3419 | Ashgabat | TKM | Turkmenian | t | 76.7
TMP | 1522 | Dili | TMP | Portuguese | t | 0
TON | 3334 | Nukualofa | TON | English | t | 0
TON | 3334 | Nukualofa | TON | Tongan | t | 98.3
TUN | 3349 | Tunis | TUN | Arabic | t | 69.9
TUR | 3358 | Ankara | TUR | Turkish | t | 87.6
TUV | 3424 | Funafuti | TUV | English | t | 0
TUV | 3424 | Funafuti | TUV | Tuvalu | t | 92.5
TWN | 3263 | Taipei | TWN | Mandarin Chinese | t | 20.1
TZA | 3306 | Dodoma | TZA | Swahili | t | 8.8
UKR | 3426 | Kyiv | UKR | Ukrainian | t | 64.7
URY | 3492 | Montevideo | URY | Spanish | t | 95.7
USA | 3813 | Washington | USA | English | t | 86.2
UZB | 3503 | Toskent | UZB | Uzbek | t | 72.6
VAT | 3538 | Citta del Vaticano | VAT | Italian | t | 0
VCT | 3066 | Kingstown | VCT | English | t | 0
VEN | 3539 | Caracas | VEN | Spanish | t | 96.9
VGB | 537 | Road Town | VGB | English | t | 0
VIR | 4067 | Charlotte Amalie | VIR | English | t | 81.7
VNM | 3770 | Hanoi | VNM | Vietnamese | t | 86.8
VUT | 3537 | Port-Vila | VUT | Bislama | t | 56.6
VUT | 3537 | Port-Vila | VUT | English | t | 28.3
VUT | 3537 | Port-Vila | VUT | French | t | 14.2
WSM | 3169 | Apia | WSM | English | t | 0.6
WSM | 3169 | Apia | WSM | Samoan | t | 47.5
YEM | 1780 | Sanaa | YEM | Arabic | t | 99.6
YUG | 1792 | Beograd | YUG | Serbo-Croatian | t | 75.2
ZAF | 716 | Pretoria | ZAF | Afrikaans | t | 14.3
ZAF | 716 | Pretoria | ZAF | English | t | 8.5
ZAF | 716 | Pretoria | ZAF | Xhosa | t | 17.7
ZAF | 716 | Pretoria | ZAF | Zulu | t | 22.7
ZWE | 4068 | Harare | ZWE | English | t | 2.2
(237 rows)
--query2
with lang_total as
( select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
select lang_count,country.code,country.name,country.continent,country.region,country.population
from country left outer join lang_total
on (lang_total.code = country.code)
where country.indepyear > 1970 and lang_total.lang_count >=1 and lang_total.lang_count >=1
order by lang_total.lang_count desc;
lang_count | code | name | continent | region | population
------------+------+---------------------------------+-----------+-----------------+------------
12 | RUS | Russian Federation | Europe | Eastern Europe | 146934000
6 | FSM | Micronesia, Federated States of | Oceania | Micronesia | 119000
4 | AZE | Azerbaijan | Asia | Middle East | 7734000
1 | BIH | Bosnia and Herzegovina | Europe | Southern Europe | 3972000
(4 rows)
-- Using CTE in the WHERE clause
--query3
with lang_total as
( select lang_count,countrycode from
(
(
select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
UNION ALL
(
select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Monarchy')
group by country.code,countrylanguage.countrycode order by country.code)
) FOO1
)
select * from
(
select count(*) as cnt,country.code,country.name
from
country,countrylanguage
where country.code=countrylanguage.countrycode group by country.code,country.name) AS FOO
where foo.cnt = (select max(lang_count) from lang_total) order by foo.code;
cnt | code | name
-----+------+--------------------
12 | CAN | Canada
12 | CHN | China
12 | IND | India
12 | RUS | Russian Federation
12 | USA | United States
(5 rows)
--query4
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select diversecountries.name,city.name,diversecountries.CNT
from diversecountries,city where city.id = diversecountries.capital
order by diversecountries.name;
name | name | cnt
--------------------+------------------+-----
Australia | Canberra | 8
Austria | Wien | 8
Canada | Ottawa | 12
China | Peking | 12
Denmark | Kobenhavn | 7
India | New Delhi | 12
Iran | Teheran | 10
Italy | Roma | 8
Myanmar | Rangoon (Yangon) | 8
Russian Federation | Moscow | 12
South Africa | Pretoria | 11
United States | Washington | 12
(12 rows)
-- query 5
with somecheapasiandiversecountries as
(
select FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,count(*) ASIAN_COUNT from
(
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Southeast Asia'
and country.continent = 'Asia'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Eastern Asia'
and country.continent = 'Asia'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Middle East'
and country.continent = 'Asia'
) FOO, countrylanguage
where FOO.code = countrylanguage.countrycode
group by FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,countrylanguage.countrycode
HAVING count(*) >=
(select min(CNT) FROM
(select count(*) CNT,country.code from countrylanguage,country
where countrylanguage.countrycode=country.code
and country.continent = 'Asia'
and country.region = 'Southern and Central Asia'
group by country.code
) FOO1
)
)
select FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,count(*) COMPARED_WITH_CHEAP_ASIAN_CNT
from
(
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.continent = 'North America'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.continent = 'South America'
) FOO,countrylanguage
where FOO.code = countrylanguage.countrycode
group by FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate
HAVING count(*) >=
(select min(ASIAN_COUNT) FROM
(select ASIAN_COUNT FROM somecheapasiandiversecountries,country
where somecheapasiandiversecountries.code = country.code
and country.gnp >= country.gnpold
) ASIANCOUNT
)
order by COUNTRY;
code | country | capital | headofstate | compared_with_cheap_asian_cnt
------+----------------------------------+-------------------------+-----------------------------------+-------------------------------
ATG | Antigua and Barbuda | Saint Johns | Elisabeth II | 2
ARG | Argentina | Buenos Aires | Fernando de la Rua | 3
ABW | Aruba | Oranjestad | Beatrix | 4
BHS | Bahamas | Nassau | Elisabeth II | 2
BRB | Barbados | Bridgetown | Elisabeth II | 2
BLZ | Belize | Belmopan | Elisabeth II | 4
BOL | Bolivia | La Paz | Hugo Banzer Suarez | 4
BRA | Brazil | Brasilia | Fernando Henrique Cardoso | 5
CAN | Canada | Ottawa | Elisabeth II | 12
CHL | Chile | Santiago de Chile | Ricardo Lagos Escobar | 4
COL | Colombia | Santafe de Bogota | Andres Pastrana Arango | 5
CRI | Costa Rica | San Jose | Miguel Angel Rodriguez Echeverria | 4
DMA | Dominica | Roseau | Vernon Shaw | 2
DOM | Dominican Republic | Santo Domingo de Guzman | Hipolito Mejia Dominguez | 2
ECU | Ecuador | Quito | Gustavo Noboa Bejarano | 2
SLV | El Salvador | San Salvador | Francisco Guillermo Flores Perez | 2
GUF | French Guiana | Cayenne | Jacques Chirac | 2
GRL | Greenland | Nuuk | Margrethe II | 2
GLP | Guadeloupe | Basse-Terre | Jacques Chirac | 2
GTM | Guatemala | Ciudad de Guatemala | Alfonso Portillo Cabrera | 5
GUY | Guyana | Georgetown | Bharrat Jagdeo | 3
HTI | Haiti | Port-au-Prince | Jean-Bertrand Aristide | 2
HND | Honduras | Tegucigalpa | Carlos Roberto Flores Facusse | 4
JAM | Jamaica | Kingston | Elisabeth II | 2
MTQ | Martinique | Fort-de-France | Jacques Chirac | 2
MEX | Mexico | Ciudad de Mexico | Vicente Fox Quesada | 6
ANT | Netherlands Antilles | Willemstad | Beatrix | 3
NIC | Nicaragua | Managua | Arnoldo Aleman Lacayo | 4
PAN | Panama | Ciudad de Panama | Mireya Elisa Moscoso Rodriguez | 6
PRY | Paraguay | Asuncion | Luis Angel Gonzalez Macchi | 4
PER | Peru | Lima | Valentin Paniagua Corazao | 3
PRI | Puerto Rico | San Juan | George W. Bush | 2
KNA | Saint Kitts and Nevis | Basseterre | Elisabeth II | 2
LCA | Saint Lucia | Castries | Elisabeth II | 2
VCT | Saint Vincent and the Grenadines | Kingstown | Elisabeth II | 2
SUR | Suriname | Paramaribo | Ronald Venetiaan | 2
TTO | Trinidad and Tobago | Port-of-Spain | Arthur N. R. Robinson | 3
USA | United States | Washington | George W. Bush | 12
VEN | Venezuela | Caracas | Hugo Chavez Frias | 3
VIR | Virgin Islands, U.S. | Charlotte Amalie | George W. Bush | 3
(40 rows)
-- query 6
select count(*) from
( select r.* from
( with fact as
(
select country.name as COUNTRY,country.code,city.name as CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,AGG1.region
from
(select
sum(case when (city.population >= 0.5 * country.population) then country.population else city.population end) as S_POPULATION,
sum(case when (gnp >= gnpold) then gnp else gnpold end) as S_GNP,
avg(case when (lifeexpectancy > 60) then 50 else lifeexpectancy end) as AVG_LIFE,country.region
from country,city
where governmentform != 'Constitutional Monarchy'
and country.capital = city.id
and indepyear > 0
group by country.region) AGG1
,country,city
where country.capital = city.id
and country.region = AGG1.region
)
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'South America'
UNION ALL
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'North America'
UNION ALL
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'Caribbean'
) as r
left join
(
select 'ARG' as CODE UNION ALL
select 'BOL' as CODE UNION ALL
select 'BRA' as CODE UNION ALL
select 'PER' as CODE UNION ALL
select 'URY' as CODE UNION ALL
select 'IND' as CODE UNION ALL
select 'LCA' as CODE UNION ALL
select 'VCT' as CODE
) as r1
on r.code = r1.code) AS FOO;
count
-------
43
(1 row)
-- query7
with alleuropeanlanguages as
(select country.code,country.name COUNTRY, city.name CAPITAL, language, isofficial, percentage
FROM country,city,countrylanguage
WHERE country.code = countrylanguage.countrycode
and country.capital = city.id
and country.continent = 'Europe')
select * from
(select * from alleuropeanlanguages where isofficial='True') e1,
(select * from alleuropeanlanguages where percentage > 50) e2
where e1.code = e2.code order by e2.COUNTRY,e1.language;
code | country | capital | language | isofficial | percentage | code | country | capital | language | isofficial | percentage
------+------------------------+-----------------------------------+----------------+------------+------------+------+------------------------+-----------------------------------+----------------+------------+------------
ALB | Albania | Tirana | Albaniana | t | 97.9 | ALB | Albania | Tirana | Albaniana | t | 97.9
AUT | Austria | Wien | German | t | 92 | AUT | Austria | Wien | German | t | 92
BLR | Belarus | Minsk | Belorussian | t | 65.6 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BLR | Belarus | Minsk | Russian | t | 32 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | French | t | 32.6 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | German | t | 1 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2 | BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2
BGR | Bulgaria | Sofija | Bulgariana | t | 83.2 | BGR | Bulgaria | Sofija | Bulgariana | t | 83.2
HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9 | HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Praha | Czech | t | 81.2
DNK | Denmark | Kobenhavn | Danish | t | 93.5 | DNK | Denmark | Kobenhavn | Danish | t | 93.5
EST | Estonia | Tallinn | Estonian | t | 65.3 | EST | Estonia | Tallinn | Estonian | t | 65.3
FRO | Faroe Islands | Torshavn | Danish | t | 0 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FRO | Faroe Islands | Torshavn | Faroese | t | 100 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FIN | Finland | Helsinki [Helsingfors] | Swedish | t | 5.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FRA | France | Paris | French | t | 93.6 | FRA | France | Paris | French | t | 93.6
DEU | Germany | Berlin | German | t | 91.3 | DEU | Germany | Berlin | German | t | 91.3
GIB | Gibraltar | Gibraltar | English | t | 88.9 | GIB | Gibraltar | Gibraltar | English | t | 88.9
GRC | Greece | Athenai | Greek | t | 98.5 | GRC | Greece | Athenai | Greek | t | 98.5
HUN | Hungary | Budapest | Hungarian | t | 98.5 | HUN | Hungary | Budapest | Hungarian | t | 98.5
ISL | Iceland | Reykjavik | Icelandic | t | 95.7 | ISL | Iceland | Reykjavik | Icelandic | t | 95.7
IRL | Ireland | Dublin | English | t | 98.4 | IRL | Ireland | Dublin | English | t | 98.4
IRL | Ireland | Dublin | Irish | t | 1.6 | IRL | Ireland | Dublin | English | t | 98.4
ITA | Italy | Roma | Italian | t | 94.1 | ITA | Italy | Roma | Italian | t | 94.1
LVA | Latvia | Riga | Latvian | t | 55.1 | LVA | Latvia | Riga | Latvian | t | 55.1
LIE | Liechtenstein | Vaduz | German | t | 89 | LIE | Liechtenstein | Vaduz | German | t | 89
LTU | Lithuania | Vilnius | Lithuanian | t | 81.6 | LTU | Lithuania | Vilnius | Lithuanian | t | 81.6
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | French | t | 4.2 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | German | t | 2.3 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
MKD | Macedonia | Skopje | Macedonian | t | 66.5 | MKD | Macedonia | Skopje | Macedonian | t | 66.5
MLT | Malta | Valletta | English | t | 2.1 | MLT | Malta | Valletta | Maltese | t | 95.8
MLT | Malta | Valletta | Maltese | t | 95.8 | MLT | Malta | Valletta | Maltese | t | 95.8
MDA | Moldova | Chisinau | Romanian | t | 61.9 | MDA | Moldova | Chisinau | Romanian | t | 61.9
NLD | Netherlands | Amsterdam | Dutch | t | 95.6 | NLD | Netherlands | Amsterdam | Dutch | t | 95.6
NOR | Norway | Oslo | Norwegian | t | 96.6 | NOR | Norway | Oslo | Norwegian | t | 96.6
POL | Poland | Warszawa | Polish | t | 97.6 | POL | Poland | Warszawa | Polish | t | 97.6
PRT | Portugal | Lisboa | Portuguese | t | 99 | PRT | Portugal | Lisboa | Portuguese | t | 99
ROM | Romania | Bucuresti | Romani | t | 0.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
ROM | Romania | Bucuresti | Romanian | t | 90.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
RUS | Russian Federation | Moscow | Russian | t | 86.6 | RUS | Russian Federation | Moscow | Russian | t | 86.6
SMR | San Marino | San Marino | Italian | t | 100 | SMR | San Marino | San Marino | Italian | t | 100
SVK | Slovakia | Bratislava | Slovak | t | 85.6 | SVK | Slovakia | Bratislava | Slovak | t | 85.6
SVN | Slovenia | Ljubljana | Slovene | t | 87.9 | SVN | Slovenia | Ljubljana | Slovene | t | 87.9
ESP | Spain | Madrid | Spanish | t | 74.4 | ESP | Spain | Madrid | Spanish | t | 74.4
SWE | Sweden | Stockholm | Swedish | t | 89.5 | SWE | Sweden | Stockholm | Swedish | t | 89.5
CHE | Switzerland | Bern | French | t | 19.2 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | German | t | 63.6 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Italian | t | 7.7 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Romansh | t | 0.6 | CHE | Switzerland | Bern | German | t | 63.6
UKR | Ukraine | Kyiv | Ukrainian | t | 64.7 | UKR | Ukraine | Kyiv | Ukrainian | t | 64.7
GBR | United Kingdom | London | English | t | 97.3 | GBR | United Kingdom | London | English | t | 97.3
YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2 | YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2
(55 rows)
-- query8
with allcountrystats as
( select country.code,country.name,count(distinct city.id) CITY_CNT,
count(distinct countrylanguage.language) LANG_CNT
from country,city,countrylanguage
where country.code = city.countrycode
and country.code = countrylanguage.countrycode
group by country.code,country.name
)
select sum(FOO.CITY_CNT) REGION_CITY_CNT,sum(FOO.LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > 1
and country.continent = 'Asia'
UNION ALL
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > 1
and country.continent = 'North America'
UNION ALL
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > (select max(CITY_CNT/LANG_CNT) from allcountrystats,country where allcountrystats.code = country.code AND country.continent='Europe')
) FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
region_city_cnt | region_lang_cnt | region
-----------------+-----------------+---------------------------
840 | 192 | Caribbean
2824 | 112 | Central America
11336 | 384 | Eastern Asia
2664 | 396 | Middle East
1625 | 125 | North America
3500 | 70 | South America
3179 | 528 | Southeast Asia
12278 | 896 | Southern and Central Asia
(8 rows)
--query 9
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select d1.code,d1.name,d1.capital,city.name CAPITAL_CITY,d1.CNT,d2.CNT
from
diversecountries d1 left join country
ON (d1.code = country.code AND d1.CNT < 8)
left join diversecountries d2
ON (country.code = d2.code AND d2.CNT > 8)
INNER JOIN city
ON(d1.capital = city.id)
ORDER BY d1.name;
code | name | capital | capital_city | cnt | cnt
------+--------------------+---------+------------------+-----+-----
AUS | Australia | 135 | Canberra | 8 |
AUT | Austria | 1523 | Wien | 8 |
CAN | Canada | 1822 | Ottawa | 12 |
CHN | China | 1891 | Peking | 12 |
DNK | Denmark | 3315 | Kobenhavn | 7 |
IND | India | 1109 | New Delhi | 12 |
IRN | Iran | 1380 | Teheran | 10 |
ITA | Italy | 1464 | Roma | 8 |
MMR | Myanmar | 2710 | Rangoon (Yangon) | 8 |
RUS | Russian Federation | 3580 | Moscow | 12 |
ZAF | South Africa | 716 | Pretoria | 11 |
USA | United States | 3813 | Washington | 12 |
(12 rows)
--query 10 , multiple ctes, joins
with longlivingregions as
(
select FOO.*,count(distinct language) as "lang_count"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
avg(lifeexpectancy) as "REGION_LIFETIME",region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP",foo."REGION_LIFETIME"),
denseregions as
(
select FOO.*,count(distinct language) as "lang_count",
sum(surfacearea) as "REGION_SURFACE_AREA"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
and FOO."REGION_POP" != 0
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP"
order by sum(surfacearea)/foo."REGION_POP" desc),
allcountrystats as
( select country.code,country.name,count(distinct city.id) CITY_CNT,
count(distinct countrylanguage.language) LANG_CNT
from country,city,countrylanguage
where country.code = city.countrycode
and country.code = countrylanguage.countrycode
group by country.code,country.name
)
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1800 and 1850
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1850 and 1900
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear > 1900
order by name
LIMIT 50;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
city_cnt | lang_cnt | name | REGION_LIFETIME | REGION_POP | lang_count | REGION_GNP | region
----------+----------+---------------------------------------+------------------+------------+------------+------------+---------------------------
4 | 5 | Afghanistan | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 3 | Albania | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
18 | 2 | Algeria | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
5 | 9 | Angola | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 2 | Antigua and Barbuda | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
57 | 3 | Argentina | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 2 | Armenia | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
14 | 8 | Australia | 78.8000030517578 | 22753100 | 11 | 405851.00 | Australia and New Zealand
6 | 8 | Austria | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
4 | 4 | Azerbaijan | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
1 | 2 | Bahamas | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
1 | 2 | Bahrain | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
24 | 7 | Bangladesh | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 2 | Barbados | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
16 | 4 | Belarus | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
9 | 6 | Belgium | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
2 | 4 | Belize | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
4 | 7 | Benin | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Bhutan | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
8 | 4 | Bolivia | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 1 | Bosnia and Herzegovina | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
2 | 5 | Botswana | 44.8199996948242 | 46886000 | 21 | 126931.00 | Southern Africa
250 | 5 | Brazil | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 4 | Brunei | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
10 | 4 | Bulgaria | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
3 | 6 | Burkina Faso | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Burundi | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
3 | 4 | Cambodia | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
7 | 8 | Cameroon | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
49 | 12 | Canada | 75.8199996948242 | 309632000 | 18 | 9111890.00 | North America
1 | 2 | Cape Verde | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 6 | Central African Republic | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
2 | 8 | Chad | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
29 | 4 | Chile | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
38 | 5 | Colombia | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 5 | Comoros | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
2 | 6 | Congo | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
18 | 10 | Congo, The Democratic Republic of the | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 4 | Costa Rica | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
5 | 5 | Cote deIvoire | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
4 | 2 | Croatia | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
14 | 1 | Cuba | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
2 | 2 | Cyprus | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
10 | 8 | Czech Republic | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
1 | 3 | Djibouti | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
1 | 2 | Dominica | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
6 | 2 | Dominican Republic | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
15 | 2 | Ecuador | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
37 | 2 | Egypt | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
7 | 2 | El Salvador | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
(50 rows)
--query 11
with allcitystats as
( select city.name CITY,city.id,country.name COUNTRY,city.district,city.population as CITY_POP
from
city,country
where city.countrycode = country.code
),
alldistrictstats as
( select allcitystats.district,allcitystats.COUNTRY,sum(CITY_POP) DISTRICT_POP,
count(CITY) as D_CITY_CNT
from allcitystats
group by allcitystats.district,allcitystats.COUNTRY
order by district,COUNTRY
),
allcountrystats as
( select alldistrictstats.COUNTRY,country.code,sum(D_CITY_CNT) C_CITY_CNT,
count(distinct countrylanguage.language) C_LANG_CNT
from alldistrictstats,country,countrylanguage
where alldistrictstats.COUNTRY = country.name
and country.code = countrylanguage.countrycode
group by COUNTRY,code
),
asian_region_stats as
(
select sum(FOO.C_CITY_CNT) REGION_CITY_CNT,sum(FOO.C_LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountrystats.code,allcountrystats.COUNTRY,C_CITY_CNT,C_LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and C_CITY_CNT/C_LANG_CNT > 1
and country.continent = 'Asia') FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region
)
select * from
(
select REGION_CITY_CNT as CITY_CNT,REGION_LANG_CNT as LANG_CNT, region as IDENTIFIER from asian_region_stats
UNION ALL
(
select sum(FOO.C_CITY_CNT) CITY_CNT,sum(FOO.C_LANG_CNT) LANG_CNT,FOO.region as IDENTIFIER
FROM
(
select allcountrystats.code,allcountrystats.COUNTRY,C_CITY_CNT,C_LANG_CNT,country.region,allcitystats.CITY CAPITAL
from allcountrystats,country,allcitystats
where allcountrystats.code = country.code
and country.capital = allcitystats.id
and C_CITY_CNT/C_LANG_CNT > 1
and country.continent = 'Europe') FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region
)
) FOO1
order by FOO1.lang_cnt,FOO1.identifier;
city_cnt | lang_cnt | identifier
----------+----------+---------------------------
494 | 10 | British Islands
159 | 48 | Baltic Countries
1295 | 161 | Nordic Countries
50176 | 264 | Eastern Asia
9414 | 369 | Western Europe
11880 | 450 | Southern Europe
32900 | 610 | Eastern Europe
29161 | 616 | Southeast Asia
8568 | 792 | Middle East
80388 | 1092 | Southern and Central Asia
(10 rows)
-- queries with CTEs using merge joins
set enable_nestloop=off;
set enable_hashjoin=off;
set enable_mergejoin=on;
--query1
with capitals as
(select country.code,id,city.name from city,country
where city.countrycode = country.code AND city.id = country.capital)
select * from
capitals,countrylanguage
where capitals.code = countrylanguage.countrycode and isofficial='true'
order by capitals.code,countrylanguage.language;
code | id | name | countrycode | language | isofficial | percentage
------+------+-----------------------------------+-------------+------------------+------------+------------
ABW | 129 | Oranjestad | ABW | Dutch | t | 5.3
AFG | 1 | Kabul | AFG | Dari | t | 32.1
AFG | 1 | Kabul | AFG | Pashto | t | 52.4
AIA | 62 | The Valley | AIA | English | t | 0
ALB | 34 | Tirana | ALB | Albaniana | t | 97.9
AND | 55 | Andorra la Vella | AND | Catalan | t | 32.3
ANT | 33 | Willemstad | ANT | Dutch | t | 0
ANT | 33 | Willemstad | ANT | Papiamento | t | 86.2
ARE | 65 | Abu Dhabi | ARE | Arabic | t | 42
ARG | 69 | Buenos Aires | ARG | Spanish | t | 96.8
ARM | 126 | Yerevan | ARM | Armenian | t | 93.4
ASM | 54 | Fagatogo | ASM | English | t | 3.1
ASM | 54 | Fagatogo | ASM | Samoan | t | 90.6
ATG | 63 | Saint Johns | ATG | English | t | 0
AUS | 135 | Canberra | AUS | English | t | 81.2
AUT | 1523 | Wien | AUT | German | t | 92
AZE | 144 | Baku | AZE | Azerbaijani | t | 89
BDI | 552 | Bujumbura | BDI | French | t | 0
BDI | 552 | Bujumbura | BDI | Kirundi | t | 98.1
BEL | 179 | Bruxelles [Brussel] | BEL | Dutch | t | 59.2
BEL | 179 | Bruxelles [Brussel] | BEL | French | t | 32.6
BEL | 179 | Bruxelles [Brussel] | BEL | German | t | 1
BGD | 150 | Dhaka | BGD | Bengali | t | 97.7
BGR | 539 | Sofija | BGR | Bulgariana | t | 83.2
BHR | 149 | al-Manama | BHR | Arabic | t | 67.7
BIH | 201 | Sarajevo | BIH | Serbo-Croatian | t | 99.2
BLR | 3520 | Minsk | BLR | Belorussian | t | 65.6
BLR | 3520 | Minsk | BLR | Russian | t | 32
BLZ | 185 | Belmopan | BLZ | English | t | 50.8
BMU | 191 | Hamilton | BMU | English | t | 100
BOL | 194 | La Paz | BOL | Aimara | t | 3.2
BOL | 194 | La Paz | BOL | Ketdua | t | 8.1
BOL | 194 | La Paz | BOL | Spanish | t | 87.7
BRA | 211 | Brasilia | BRA | Portuguese | t | 97.5
BRB | 174 | Bridgetown | BRB | English | t | 0
BRN | 538 | Bandar Seri Begawan | BRN | Malay | t | 45.5
BTN | 192 | Thimphu | BTN | Dzongkha | t | 50
CAN | 1822 | Ottawa | CAN | English | t | 60.4
CAN | 1822 | Ottawa | CAN | French | t | 23.4
CCK | 2317 | West Island | CCK | English | t | 0
CHE | 3248 | Bern | CHE | French | t | 19.2
CHE | 3248 | Bern | CHE | German | t | 63.6
CHE | 3248 | Bern | CHE | Italian | t | 7.7
CHE | 3248 | Bern | CHE | Romansh | t | 0.6
CHL | 554 | Santiago de Chile | CHL | Spanish | t | 89.7
CHN | 1891 | Peking | CHN | Chinese | t | 92
COK | 583 | Avarua | COK | Maori | t | 0
COL | 2257 | Santafe de Bogota | COL | Spanish | t | 99
COM | 2295 | Moroni | COM | Comorian | t | 75
CPV | 1859 | Praia | CPV | Portuguese | t | 0
CRI | 584 | San Jose | CRI | Spanish | t | 97.5
CUB | 2413 | La Habana | CUB | Spanish | t | 100
CXR | 1791 | Flying Fish Cove | CXR | English | t | 0
CYM | 553 | George Town | CYM | English | t | 0
CYP | 2430 | Nicosia | CYP | Greek | t | 74.1
CYP | 2430 | Nicosia | CYP | Turkish | t | 22.4
CZE | 3339 | Praha | CZE | Czech | t | 81.2
DEU | 3068 | Berlin | DEU | German | t | 91.3
DJI | 585 | Djibouti | DJI | Arabic | t | 10.6
DNK | 3315 | Kobenhavn | DNK | Danish | t | 93.5
DOM | 587 | Santo Domingo de Guzman | DOM | Spanish | t | 98
DZA | 35 | Alger | DZA | Arabic | t | 86
ECU | 594 | Quito | ECU | Spanish | t | 93
EGY | 608 | Cairo | EGY | Arabic | t | 98.8
ERI | 652 | Asmara | ERI | Tigrinja | t | 49.1
ESH | 2453 | El-Aaiun | ESH | Arabic | t | 100
ESP | 653 | Madrid | ESP | Spanish | t | 74.4
EST | 3791 | Tallinn | EST | Estonian | t | 65.3
FIN | 3236 | Helsinki [Helsingfors] | FIN | Finnish | t | 92.7
FIN | 3236 | Helsinki [Helsingfors] | FIN | Swedish | t | 5.7
FJI | 764 | Suva | FJI | Fijian | t | 50.8
FLK | 763 | Stanley | FLK | English | t | 0
FRA | 2974 | Paris | FRA | French | t | 93.6
FRO | 901 | Torshavn | FRO | Danish | t | 0
FRO | 901 | Torshavn | FRO | Faroese | t | 100
GBR | 456 | London | GBR | English | t | 97.3
GEO | 905 | Tbilisi | GEO | Georgiana | t | 71.7
GIB | 915 | Gibraltar | GIB | English | t | 88.9
GLP | 919 | Basse-Terre | GLP | French | t | 0
GNB | 927 | Bissau | GNB | Portuguese | t | 8.1
GRC | 2401 | Athenai | GRC | Greek | t | 98.5
GRL | 917 | Nuuk | GRL | Danish | t | 12.5
GRL | 917 | Nuuk | GRL | Greenlandic | t | 87.5
GTM | 922 | Ciudad de Guatemala | GTM | Spanish | t | 64.7
GUM | 921 | Agaaa | GUM | Chamorro | t | 29.6
GUM | 921 | Agaaa | GUM | English | t | 37.5
HKG | 937 | Victoria | HKG | English | t | 2.2
HND | 933 | Tegucigalpa | HND | Spanish | t | 97.2
HRV | 2409 | Zagreb | HRV | Serbo-Croatian | t | 95.9
HTI | 929 | Port-au-Prince | HTI | French | t | 0
HUN | 3483 | Budapest | HUN | Hungarian | t | 98.5
IDN | 939 | Jakarta | IDN | Malay | t | 12.1
IND | 1109 | New Delhi | IND | Hindi | t | 39.9
IRL | 1447 | Dublin | IRL | English | t | 98.4
IRL | 1447 | Dublin | IRL | Irish | t | 1.6
IRN | 1380 | Teheran | IRN | Persian | t | 45.7
IRQ | 1365 | Baghdad | IRQ | Arabic | t | 77.2
ISL | 1449 | Reykjavik | ISL | Icelandic | t | 95.7
ISR | 1450 | Jerusalem | ISR | Arabic | t | 18
ISR | 1450 | Jerusalem | ISR | Hebrew | t | 63.1
ITA | 1464 | Roma | ITA | Italian | t | 94.1
JOR | 1786 | Amman | JOR | Arabic | t | 97.9
JPN | 1532 | Tokyo | JPN | Japanese | t | 99.1
KAZ | 1864 | Astana | KAZ | Kazakh | t | 46
KGZ | 2253 | Bishkek | KGZ | Kirgiz | t | 59.7
KGZ | 2253 | Bishkek | KGZ | Russian | t | 16.2
KHM | 1800 | Phnom Penh | KHM | Khmer | t | 88.6
KIR | 2256 | Bairiki | KIR | Kiribati | t | 98.9
KNA | 3064 | Basseterre | KNA | English | t | 0
KOR | 2331 | Seoul | KOR | Korean | t | 99.9
KWT | 2429 | Kuwait | KWT | Arabic | t | 78.1
LAO | 2432 | Vientiane | LAO | Lao | t | 67.2
LBN | 2438 | Beirut | LBN | Arabic | t | 93
LBY | 2441 | Tripoli | LBY | Arabic | t | 96
LCA | 3065 | Castries | LCA | English | t | 20
LIE | 2446 | Vaduz | LIE | German | t | 89
LKA | 3217 | Colombo | LKA | Singali | t | 60.3
LKA | 3217 | Colombo | LKA | Tamil | t | 19.6
LSO | 2437 | Maseru | LSO | English | t | 0
LSO | 2437 | Maseru | LSO | Sotho | t | 85
LTU | 2447 | Vilnius | LTU | Lithuanian | t | 81.6
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | French | t | 4.2
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | German | t | 2.3
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | Luxembourgish | t | 64.4
LVA | 2434 | Riga | LVA | Latvian | t | 55.1
MAC | 2454 | Macao | MAC | Portuguese | t | 2.3
MAR | 2486 | Rabat | MAR | Arabic | t | 65
MCO | 2695 | Monaco-Ville | MCO | French | t | 41.9
MDA | 2690 | Chisinau | MDA | Romanian | t | 61.9
MDG | 2455 | Antananarivo | MDG | French | t | 0
MDG | 2455 | Antananarivo | MDG | Malagasy | t | 98.9
MDV | 2463 | Male | MDV | Dhivehi | t | 100
MEX | 2515 | Ciudad de Mexico | MEX | Spanish | t | 92.1
MHL | 2507 | Dalap-Uliga-Darrit | MHL | English | t | 0
MHL | 2507 | Dalap-Uliga-Darrit | MHL | Marshallese | t | 96.8
MKD | 2460 | Skopje | MKD | Macedonian | t | 66.5
MLT | 2484 | Valletta | MLT | English | t | 2.1
MLT | 2484 | Valletta | MLT | Maltese | t | 95.8
MMR | 2710 | Rangoon (Yangon) | MMR | Burmese | t | 69
MNG | 2696 | Ulan Bator | MNG | Mongolian | t | 78.8
MNP | 2913 | Garapan | MNP | English | t | 4.8
MSR | 2697 | Plymouth | MSR | English | t | 0
MTQ | 2508 | Fort-de-France | MTQ | French | t | 0
MWI | 2462 | Lilongwe | MWI | Chichewa | t | 58.3
MYS | 2464 | Kuala Lumpur | MYS | Malay | t | 58.4
MYT | 2514 | Mamoutzou | MYT | French | t | 20.3
NCL | 3493 | Noumea | NCL | French | t | 34.3
NFK | 2806 | Kingston | NFK | English | t | 0
NIC | 2734 | Managua | NIC | Spanish | t | 97.6
NIU | 2805 | Alofi | NIU | English | t | 0
NLD | 5 | Amsterdam | NLD | Dutch | t | 95.6
NOR | 2807 | Oslo | NOR | Norwegian | t | 96.6
NPL | 2729 | Kathmandu | NPL | Nepali | t | 50.4
NRU | 2728 | Yaren | NRU | English | t | 7.5
NRU | 2728 | Yaren | NRU | Nauru | t | 57.5
NZL | 3499 | Wellington | NZL | English | t | 87
OMN | 2821 | Masqat | OMN | Arabic | t | 76.7
PAK | 2831 | Islamabad | PAK | Urdu | t | 7.6
PAN | 2882 | Ciudad de Panama | PAN | Spanish | t | 76.8
PER | 2890 | Lima | PER | Aimara | t | 2.3
PER | 2890 | Lima | PER | Ketdua | t | 16.4
PER | 2890 | Lima | PER | Spanish | t | 79.8
PHL | 766 | Manila | PHL | Pilipino | t | 29.3
PLW | 2881 | Koror | PLW | English | t | 3.2
PLW | 2881 | Koror | PLW | Palau | t | 82.2
POL | 2928 | Warszawa | POL | Polish | t | 97.6
PRI | 2919 | San Juan | PRI | Spanish | t | 51.3
PRK | 2318 | Pyongyang | PRK | Korean | t | 99.9
PRT | 2914 | Lisboa | PRT | Portuguese | t | 99
PRY | 2885 | Asuncion | PRY | Guarani | t | 40.1
PRY | 2885 | Asuncion | PRY | Spanish | t | 55.1
PYF | 3016 | Papeete | PYF | French | t | 40.8
QAT | 2973 | Doha | QAT | Arabic | t | 40.7
ROM | 3018 | Bucuresti | ROM | Romani | t | 0.7
ROM | 3018 | Bucuresti | ROM | Romanian | t | 90.7
RUS | 3580 | Moscow | RUS | Russian | t | 86.6
RWA | 3047 | Kigali | RWA | French | t | 0
RWA | 3047 | Kigali | RWA | Rwanda | t | 100
SAU | 3173 | Riyadh | SAU | Arabic | t | 95
SDN | 3225 | Khartum | SDN | Arabic | t | 49.4
SEN | 3198 | Dakar | SEN | Wolof | t | 48.1
SGP | 3208 | Singapore | SGP | Chinese | t | 77.1
SGP | 3208 | Singapore | SGP | Malay | t | 14.1
SGP | 3208 | Singapore | SGP | Tamil | t | 7.4
SHN | 3063 | Jamestown | SHN | English | t | 0
SJM | 938 | Longyearbyen | SJM | Norwegian | t | 0
SLV | 645 | San Salvador | SLV | Spanish | t | 100
SMR | 3171 | San Marino | SMR | Italian | t | 100
SOM | 3214 | Mogadishu | SOM | Arabic | t | 0
SOM | 3214 | Mogadishu | SOM | Somali | t | 98.3
SPM | 3067 | Saint-Pierre | SPM | French | t | 0
SVK | 3209 | Bratislava | SVK | Slovak | t | 85.6
SVN | 3212 | Ljubljana | SVN | Slovene | t | 87.9
SWE | 3048 | Stockholm | SWE | Swedish | t | 89.5
SWZ | 3244 | Mbabane | SWZ | Swazi | t | 89.9
SYC | 3206 | Victoria | SYC | English | t | 3.8
SYC | 3206 | Victoria | SYC | French | t | 1.3
SYR | 3250 | Damascus | SYR | Arabic | t | 90
TCA | 3423 | Cockburn Town | TCA | English | t | 0
TCD | 3337 | NDjamena | TCD | Arabic | t | 12.3
TGO | 3332 | Lome | TGO | Ewe | t | 23.2
TGO | 3332 | Lome | TGO | Kabye | t | 13.8
THA | 3320 | Bangkok | THA | Thai | t | 52.6
TJK | 3261 | Dushanbe | TJK | Tadzhik | t | 62.2
TKL | 3333 | Fakaofo | TKL | English | t | 0
TKM | 3419 | Ashgabat | TKM | Turkmenian | t | 76.7
TMP | 1522 | Dili | TMP | Portuguese | t | 0
TON | 3334 | Nukualofa | TON | English | t | 0
TON | 3334 | Nukualofa | TON | Tongan | t | 98.3
TUN | 3349 | Tunis | TUN | Arabic | t | 69.9
TUR | 3358 | Ankara | TUR | Turkish | t | 87.6
TUV | 3424 | Funafuti | TUV | English | t | 0
TUV | 3424 | Funafuti | TUV | Tuvalu | t | 92.5
TWN | 3263 | Taipei | TWN | Mandarin Chinese | t | 20.1
TZA | 3306 | Dodoma | TZA | Swahili | t | 8.8
UKR | 3426 | Kyiv | UKR | Ukrainian | t | 64.7
URY | 3492 | Montevideo | URY | Spanish | t | 95.7
USA | 3813 | Washington | USA | English | t | 86.2
UZB | 3503 | Toskent | UZB | Uzbek | t | 72.6
VAT | 3538 | Citta del Vaticano | VAT | Italian | t | 0
VCT | 3066 | Kingstown | VCT | English | t | 0
VEN | 3539 | Caracas | VEN | Spanish | t | 96.9
VGB | 537 | Road Town | VGB | English | t | 0
VIR | 4067 | Charlotte Amalie | VIR | English | t | 81.7
VNM | 3770 | Hanoi | VNM | Vietnamese | t | 86.8
VUT | 3537 | Port-Vila | VUT | Bislama | t | 56.6
VUT | 3537 | Port-Vila | VUT | English | t | 28.3
VUT | 3537 | Port-Vila | VUT | French | t | 14.2
WSM | 3169 | Apia | WSM | English | t | 0.6
WSM | 3169 | Apia | WSM | Samoan | t | 47.5
YEM | 1780 | Sanaa | YEM | Arabic | t | 99.6
YUG | 1792 | Beograd | YUG | Serbo-Croatian | t | 75.2
ZAF | 716 | Pretoria | ZAF | Afrikaans | t | 14.3
ZAF | 716 | Pretoria | ZAF | English | t | 8.5
ZAF | 716 | Pretoria | ZAF | Xhosa | t | 17.7
ZAF | 716 | Pretoria | ZAF | Zulu | t | 22.7
ZWE | 4068 | Harare | ZWE | English | t | 2.2
(237 rows)
--query2
with lang_total as
( select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
select lang_count,country.code,country.name,country.continent,country.region,country.population
from country left outer join lang_total
on (lang_total.code = country.code)
where country.indepyear > 1970 and lang_total.lang_count >=1 and lang_total.lang_count >=1
order by lang_total.lang_count desc;
lang_count | code | name | continent | region | population
------------+------+---------------------------------+-----------+-----------------+------------
12 | RUS | Russian Federation | Europe | Eastern Europe | 146934000
6 | FSM | Micronesia, Federated States of | Oceania | Micronesia | 119000
4 | AZE | Azerbaijan | Asia | Middle East | 7734000
1 | BIH | Bosnia and Herzegovina | Europe | Southern Europe | 3972000
(4 rows)
-- Using CTE in the WHERE clause
--query3
with lang_total as
( select lang_count,countrycode from
(
(
select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
UNION ALL
(
select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Monarchy')
group by country.code,countrylanguage.countrycode order by country.code)
) FOO1
)
select * from
(
select count(*) as cnt,country.code,country.name
from
country,countrylanguage
where country.code=countrylanguage.countrycode group by country.code,country.name) AS FOO
where foo.cnt = (select max(lang_count) from lang_total) order by foo.code;
cnt | code | name
-----+------+--------------------
12 | CAN | Canada
12 | CHN | China
12 | IND | India
12 | RUS | Russian Federation
12 | USA | United States
(5 rows)
--query4
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select diversecountries.name,city.name,diversecountries.CNT
from diversecountries,city where city.id = diversecountries.capital
order by diversecountries.name;
name | name | cnt
--------------------+------------------+-----
Australia | Canberra | 8
Austria | Wien | 8
Canada | Ottawa | 12
China | Peking | 12
Denmark | Kobenhavn | 7
India | New Delhi | 12
Iran | Teheran | 10
Italy | Roma | 8
Myanmar | Rangoon (Yangon) | 8
Russian Federation | Moscow | 12
South Africa | Pretoria | 11
United States | Washington | 12
(12 rows)
-- query 5
with somecheapasiandiversecountries as
(
select FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,count(*) ASIAN_COUNT from
(
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Southeast Asia'
and country.continent = 'Asia'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Eastern Asia'
and country.continent = 'Asia'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Middle East'
and country.continent = 'Asia'
) FOO, countrylanguage
where FOO.code = countrylanguage.countrycode
group by FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,countrylanguage.countrycode
HAVING count(*) >=
(select min(CNT) FROM
(select count(*) CNT,country.code from countrylanguage,country
where countrylanguage.countrycode=country.code
and country.continent = 'Asia'
and country.region = 'Southern and Central Asia'
group by country.code
) FOO1
)
)
select FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,count(*) COMPARED_WITH_CHEAP_ASIAN_CNT
from
(
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.continent = 'North America'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.continent = 'South America'
) FOO,countrylanguage
where FOO.code = countrylanguage.countrycode
group by FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate
HAVING count(*) >=
(select min(ASIAN_COUNT) FROM
(select ASIAN_COUNT FROM somecheapasiandiversecountries,country
where somecheapasiandiversecountries.code = country.code
and country.gnp >= country.gnpold
) ASIANCOUNT
)
order by COUNTRY;
code | country | capital | headofstate | compared_with_cheap_asian_cnt
------+----------------------------------+-------------------------+-----------------------------------+-------------------------------
ATG | Antigua and Barbuda | Saint Johns | Elisabeth II | 2
ARG | Argentina | Buenos Aires | Fernando de la Rua | 3
ABW | Aruba | Oranjestad | Beatrix | 4
BHS | Bahamas | Nassau | Elisabeth II | 2
BRB | Barbados | Bridgetown | Elisabeth II | 2
BLZ | Belize | Belmopan | Elisabeth II | 4
BOL | Bolivia | La Paz | Hugo Banzer Suarez | 4
BRA | Brazil | Brasilia | Fernando Henrique Cardoso | 5
CAN | Canada | Ottawa | Elisabeth II | 12
CHL | Chile | Santiago de Chile | Ricardo Lagos Escobar | 4
COL | Colombia | Santafe de Bogota | Andres Pastrana Arango | 5
CRI | Costa Rica | San Jose | Miguel Angel Rodriguez Echeverria | 4
DMA | Dominica | Roseau | Vernon Shaw | 2
DOM | Dominican Republic | Santo Domingo de Guzman | Hipolito Mejia Dominguez | 2
ECU | Ecuador | Quito | Gustavo Noboa Bejarano | 2
SLV | El Salvador | San Salvador | Francisco Guillermo Flores Perez | 2
GUF | French Guiana | Cayenne | Jacques Chirac | 2
GRL | Greenland | Nuuk | Margrethe II | 2
GLP | Guadeloupe | Basse-Terre | Jacques Chirac | 2
GTM | Guatemala | Ciudad de Guatemala | Alfonso Portillo Cabrera | 5
GUY | Guyana | Georgetown | Bharrat Jagdeo | 3
HTI | Haiti | Port-au-Prince | Jean-Bertrand Aristide | 2
HND | Honduras | Tegucigalpa | Carlos Roberto Flores Facusse | 4
JAM | Jamaica | Kingston | Elisabeth II | 2
MTQ | Martinique | Fort-de-France | Jacques Chirac | 2
MEX | Mexico | Ciudad de Mexico | Vicente Fox Quesada | 6
ANT | Netherlands Antilles | Willemstad | Beatrix | 3
NIC | Nicaragua | Managua | Arnoldo Aleman Lacayo | 4
PAN | Panama | Ciudad de Panama | Mireya Elisa Moscoso Rodriguez | 6
PRY | Paraguay | Asuncion | Luis Angel Gonzalez Macchi | 4
PER | Peru | Lima | Valentin Paniagua Corazao | 3
PRI | Puerto Rico | San Juan | George W. Bush | 2
KNA | Saint Kitts and Nevis | Basseterre | Elisabeth II | 2
LCA | Saint Lucia | Castries | Elisabeth II | 2
VCT | Saint Vincent and the Grenadines | Kingstown | Elisabeth II | 2
SUR | Suriname | Paramaribo | Ronald Venetiaan | 2
TTO | Trinidad and Tobago | Port-of-Spain | Arthur N. R. Robinson | 3
USA | United States | Washington | George W. Bush | 12
VEN | Venezuela | Caracas | Hugo Chavez Frias | 3
VIR | Virgin Islands, U.S. | Charlotte Amalie | George W. Bush | 3
(40 rows)
-- query 6
select count(*) from
( select r.* from
( with fact as
(
select country.name as COUNTRY,country.code,city.name as CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,AGG1.region
from
(select
sum(case when (city.population >= 0.5 * country.population) then country.population else city.population end) as S_POPULATION,
sum(case when (gnp >= gnpold) then gnp else gnpold end) as S_GNP,
avg(case when (lifeexpectancy > 60) then 50 else lifeexpectancy end) as AVG_LIFE,country.region
from country,city
where governmentform != 'Constitutional Monarchy'
and country.capital = city.id
and indepyear > 0
group by country.region) AGG1
,country,city
where country.capital = city.id
and country.region = AGG1.region
)
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'South America'
UNION ALL
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'North America'
UNION ALL
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'Caribbean'
) as r
left join
(
select 'ARG' as CODE UNION ALL
select 'BOL' as CODE UNION ALL
select 'BRA' as CODE UNION ALL
select 'PER' as CODE UNION ALL
select 'URY' as CODE UNION ALL
select 'IND' as CODE UNION ALL
select 'LCA' as CODE UNION ALL
select 'VCT' as CODE
) as r1
on r.code = r1.code) AS FOO;
count
-------
43
(1 row)
-- query7
with alleuropeanlanguages as
(select country.code,country.name COUNTRY, city.name CAPITAL, language, isofficial, percentage
FROM country,city,countrylanguage
WHERE country.code = countrylanguage.countrycode
and country.capital = city.id
and country.continent = 'Europe')
select * from
(select * from alleuropeanlanguages where isofficial='True') e1,
(select * from alleuropeanlanguages where percentage > 50) e2
where e1.code = e2.code order by e2.COUNTRY,e1.language;
code | country | capital | language | isofficial | percentage | code | country | capital | language | isofficial | percentage
------+------------------------+-----------------------------------+----------------+------------+------------+------+------------------------+-----------------------------------+----------------+------------+------------
ALB | Albania | Tirana | Albaniana | t | 97.9 | ALB | Albania | Tirana | Albaniana | t | 97.9
AUT | Austria | Wien | German | t | 92 | AUT | Austria | Wien | German | t | 92
BLR | Belarus | Minsk | Belorussian | t | 65.6 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BLR | Belarus | Minsk | Russian | t | 32 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | French | t | 32.6 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | German | t | 1 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2 | BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2
BGR | Bulgaria | Sofija | Bulgariana | t | 83.2 | BGR | Bulgaria | Sofija | Bulgariana | t | 83.2
HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9 | HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Praha | Czech | t | 81.2
DNK | Denmark | Kobenhavn | Danish | t | 93.5 | DNK | Denmark | Kobenhavn | Danish | t | 93.5
EST | Estonia | Tallinn | Estonian | t | 65.3 | EST | Estonia | Tallinn | Estonian | t | 65.3
FRO | Faroe Islands | Torshavn | Danish | t | 0 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FRO | Faroe Islands | Torshavn | Faroese | t | 100 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FIN | Finland | Helsinki [Helsingfors] | Swedish | t | 5.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FRA | France | Paris | French | t | 93.6 | FRA | France | Paris | French | t | 93.6
DEU | Germany | Berlin | German | t | 91.3 | DEU | Germany | Berlin | German | t | 91.3
GIB | Gibraltar | Gibraltar | English | t | 88.9 | GIB | Gibraltar | Gibraltar | English | t | 88.9
GRC | Greece | Athenai | Greek | t | 98.5 | GRC | Greece | Athenai | Greek | t | 98.5
HUN | Hungary | Budapest | Hungarian | t | 98.5 | HUN | Hungary | Budapest | Hungarian | t | 98.5
ISL | Iceland | Reykjavik | Icelandic | t | 95.7 | ISL | Iceland | Reykjavik | Icelandic | t | 95.7
IRL | Ireland | Dublin | English | t | 98.4 | IRL | Ireland | Dublin | English | t | 98.4
IRL | Ireland | Dublin | Irish | t | 1.6 | IRL | Ireland | Dublin | English | t | 98.4
ITA | Italy | Roma | Italian | t | 94.1 | ITA | Italy | Roma | Italian | t | 94.1
LVA | Latvia | Riga | Latvian | t | 55.1 | LVA | Latvia | Riga | Latvian | t | 55.1
LIE | Liechtenstein | Vaduz | German | t | 89 | LIE | Liechtenstein | Vaduz | German | t | 89
LTU | Lithuania | Vilnius | Lithuanian | t | 81.6 | LTU | Lithuania | Vilnius | Lithuanian | t | 81.6
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | French | t | 4.2 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | German | t | 2.3 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
MKD | Macedonia | Skopje | Macedonian | t | 66.5 | MKD | Macedonia | Skopje | Macedonian | t | 66.5
MLT | Malta | Valletta | English | t | 2.1 | MLT | Malta | Valletta | Maltese | t | 95.8
MLT | Malta | Valletta | Maltese | t | 95.8 | MLT | Malta | Valletta | Maltese | t | 95.8
MDA | Moldova | Chisinau | Romanian | t | 61.9 | MDA | Moldova | Chisinau | Romanian | t | 61.9
NLD | Netherlands | Amsterdam | Dutch | t | 95.6 | NLD | Netherlands | Amsterdam | Dutch | t | 95.6
NOR | Norway | Oslo | Norwegian | t | 96.6 | NOR | Norway | Oslo | Norwegian | t | 96.6
POL | Poland | Warszawa | Polish | t | 97.6 | POL | Poland | Warszawa | Polish | t | 97.6
PRT | Portugal | Lisboa | Portuguese | t | 99 | PRT | Portugal | Lisboa | Portuguese | t | 99
ROM | Romania | Bucuresti | Romani | t | 0.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
ROM | Romania | Bucuresti | Romanian | t | 90.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
RUS | Russian Federation | Moscow | Russian | t | 86.6 | RUS | Russian Federation | Moscow | Russian | t | 86.6
SMR | San Marino | San Marino | Italian | t | 100 | SMR | San Marino | San Marino | Italian | t | 100
SVK | Slovakia | Bratislava | Slovak | t | 85.6 | SVK | Slovakia | Bratislava | Slovak | t | 85.6
SVN | Slovenia | Ljubljana | Slovene | t | 87.9 | SVN | Slovenia | Ljubljana | Slovene | t | 87.9
ESP | Spain | Madrid | Spanish | t | 74.4 | ESP | Spain | Madrid | Spanish | t | 74.4
SWE | Sweden | Stockholm | Swedish | t | 89.5 | SWE | Sweden | Stockholm | Swedish | t | 89.5
CHE | Switzerland | Bern | French | t | 19.2 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | German | t | 63.6 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Italian | t | 7.7 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Romansh | t | 0.6 | CHE | Switzerland | Bern | German | t | 63.6
UKR | Ukraine | Kyiv | Ukrainian | t | 64.7 | UKR | Ukraine | Kyiv | Ukrainian | t | 64.7
GBR | United Kingdom | London | English | t | 97.3 | GBR | United Kingdom | London | English | t | 97.3
YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2 | YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2
(55 rows)
-- query8
with allcountrystats as
( select country.code,country.name,count(distinct city.id) CITY_CNT,
count(distinct countrylanguage.language) LANG_CNT
from country,city,countrylanguage
where country.code = city.countrycode
and country.code = countrylanguage.countrycode
group by country.code,country.name
)
select sum(FOO.CITY_CNT) REGION_CITY_CNT,sum(FOO.LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > 1
and country.continent = 'Asia'
UNION ALL
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > 1
and country.continent = 'North America'
UNION ALL
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > (select max(CITY_CNT/LANG_CNT) from allcountrystats,country where allcountrystats.code = country.code AND country.continent='Europe')
) FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
region_city_cnt | region_lang_cnt | region
-----------------+-----------------+---------------------------
840 | 192 | Caribbean
2824 | 112 | Central America
11336 | 384 | Eastern Asia
2664 | 396 | Middle East
1625 | 125 | North America
3500 | 70 | South America
3179 | 528 | Southeast Asia
12278 | 896 | Southern and Central Asia
(8 rows)
--query 9
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select d1.code,d1.name,d1.capital,city.name CAPITAL_CITY,d1.CNT,d2.CNT
from
diversecountries d1 left join country
ON (d1.code = country.code AND d1.CNT < 8)
left join diversecountries d2
ON (country.code = d2.code AND d2.CNT > 8)
INNER JOIN city
ON(d1.capital = city.id)
ORDER BY d1.name;
code | name | capital | capital_city | cnt | cnt
------+--------------------+---------+------------------+-----+-----
AUS | Australia | 135 | Canberra | 8 |
AUT | Austria | 1523 | Wien | 8 |
CAN | Canada | 1822 | Ottawa | 12 |
CHN | China | 1891 | Peking | 12 |
DNK | Denmark | 3315 | Kobenhavn | 7 |
IND | India | 1109 | New Delhi | 12 |
IRN | Iran | 1380 | Teheran | 10 |
ITA | Italy | 1464 | Roma | 8 |
MMR | Myanmar | 2710 | Rangoon (Yangon) | 8 |
RUS | Russian Federation | 3580 | Moscow | 12 |
ZAF | South Africa | 716 | Pretoria | 11 |
USA | United States | 3813 | Washington | 12 |
(12 rows)
--query 10 , multiple ctes, joins
with longlivingregions as
(
select FOO.*,count(distinct language) as "lang_count"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
avg(lifeexpectancy) as "REGION_LIFETIME",region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP",foo."REGION_LIFETIME"),
denseregions as
(
select FOO.*,count(distinct language) as "lang_count",
sum(surfacearea) as "REGION_SURFACE_AREA"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
and FOO."REGION_POP" != 0
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP"
order by sum(surfacearea)/foo."REGION_POP" desc),
allcountrystats as
( select country.code,country.name,count(distinct city.id) CITY_CNT,
count(distinct countrylanguage.language) LANG_CNT
from country,city,countrylanguage
where country.code = city.countrycode
and country.code = countrylanguage.countrycode
group by country.code,country.name
)
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1800 and 1850
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1850 and 1900
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear > 1900
order by name
LIMIT 50;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
city_cnt | lang_cnt | name | REGION_SURFACE_AREA | REGION_LIFETIME | REGION_POP | lang_count | REGION_GNP | region
----------+----------+---------------------------------------+---------------------+------------------+------------+------------+------------+---------------------------
4 | 5 | Afghanistan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 3 | Albania | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
18 | 2 | Algeria | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
5 | 9 | Angola | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 2 | Antigua and Barbuda | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
57 | 3 | Argentina | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 2 | Armenia | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
14 | 8 | Australia | 62471162.00 | 78.8000030517578 | 22753100 | 11 | 405851.00 | Australia and New Zealand
6 | 8 | Austria | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
4 | 4 | Azerbaijan | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
1 | 2 | Bahamas | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
1 | 2 | Bahrain | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
24 | 7 | Bangladesh | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 2 | Barbados | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
16 | 4 | Belarus | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
9 | 6 | Belgium | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
2 | 4 | Belize | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
4 | 7 | Benin | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Bhutan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
8 | 4 | Bolivia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 1 | Bosnia and Herzegovina | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
2 | 5 | Botswana | 23060186.00 | 44.8199996948242 | 46886000 | 21 | 126931.00 | Southern Africa
250 | 5 | Brazil | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 4 | Brunei | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
10 | 4 | Bulgaria | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
3 | 6 | Burkina Faso | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Burundi | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
3 | 4 | Cambodia | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
7 | 8 | Cameroon | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
49 | 12 | Canada | 236342035.00 | 75.8199996948242 | 309632000 | 18 | 9111890.00 | North America
1 | 2 | Cape Verde | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 6 | Central African Republic | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
2 | 8 | Chad | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
29 | 4 | Chile | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
38 | 5 | Colombia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 5 | Comoros | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
2 | 6 | Congo | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
18 | 10 | Congo, The Democratic Republic of the | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 4 | Costa Rica | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
5 | 5 | Cote deIvoire | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
4 | 2 | Croatia | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
14 | 1 | Cuba | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
2 | 2 | Cyprus | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
10 | 8 | Czech Republic | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
1 | 3 | Djibouti | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
1 | 2 | Dominica | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
6 | 2 | Dominican Republic | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
15 | 2 | Ecuador | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
37 | 2 | Egypt | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
7 | 2 | El Salvador | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
(50 rows)
--query 11
with allcitystats as
( select city.name CITY,city.id,country.name COUNTRY,city.district,city.population as CITY_POP
from
city,country
where city.countrycode = country.code
),
alldistrictstats as
( select allcitystats.district,allcitystats.COUNTRY,sum(CITY_POP) DISTRICT_POP,
count(CITY) as D_CITY_CNT
from allcitystats
group by allcitystats.district,allcitystats.COUNTRY
order by district,COUNTRY
),
allcountrystats as
( select alldistrictstats.COUNTRY,country.code,sum(D_CITY_CNT) C_CITY_CNT,
count(distinct countrylanguage.language) C_LANG_CNT
from alldistrictstats,country,countrylanguage
where alldistrictstats.COUNTRY = country.name
and country.code = countrylanguage.countrycode
group by COUNTRY,code
),
asian_region_stats as
(
select sum(FOO.C_CITY_CNT) REGION_CITY_CNT,sum(FOO.C_LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountrystats.code,allcountrystats.COUNTRY,C_CITY_CNT,C_LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and C_CITY_CNT/C_LANG_CNT > 1
and country.continent = 'Asia') FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region
)
select * from
(
select REGION_CITY_CNT as CITY_CNT,REGION_LANG_CNT as LANG_CNT, region as IDENTIFIER from asian_region_stats
UNION ALL
(
select sum(FOO.C_CITY_CNT) CITY_CNT,sum(FOO.C_LANG_CNT) LANG_CNT,FOO.region as IDENTIFIER
FROM
(
select allcountrystats.code,allcountrystats.COUNTRY,C_CITY_CNT,C_LANG_CNT,country.region,allcitystats.CITY CAPITAL
from allcountrystats,country,allcitystats
where allcountrystats.code = country.code
and country.capital = allcitystats.id
and C_CITY_CNT/C_LANG_CNT > 1
and country.continent = 'Europe') FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region
)
) FOO1
order by FOO1.lang_cnt,FOO1.identifier;
city_cnt | lang_cnt | identifier
----------+----------+---------------------------
494 | 10 | British Islands
159 | 48 | Baltic Countries
1295 | 161 | Nordic Countries
50176 | 264 | Eastern Asia
9414 | 369 | Western Europe
11880 | 450 | Southern Europe
32900 | 610 | Eastern Europe
29161 | 616 | Southeast Asia
8568 | 792 | Middle East
80388 | 1092 | Southern and Central Asia
(10 rows)
-- queries with a single CTE used more than once in different parts of the main query
-- query1
-- This kind of query is their only use case for CTE. We don't error, we give correct
-- results,use shared scan here and we are good!
select count(*) from
( select r.* from
( with fact as
(
select country.name as COUNTRY,country.code,city.name as CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,AGG1.region
from
(select
sum(case when (city.population >= 0.5 * country.population) then country.population else city.population end) as S_POPULATION,
sum(case when (gnp >= gnpold) then gnp else gnpold end) as S_GNP,
avg(case when (lifeexpectancy > 60) then 50 else lifeexpectancy end) as AVG_LIFE,country.region
from country,city
where governmentform != 'Constitutional Monarchy'
and country.capital = city.id
and indepyear > 0
group by country.region) AGG1
,country,city
where country.capital = city.id
and country.region = AGG1.region
)
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'South America'
UNION ALL
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'North America'
UNION ALL
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'Caribbean'
) as r
left join
(
select 'ARG' as CODE UNION ALL
select 'BOL' as CODE UNION ALL
select 'BRA' as CODE UNION ALL
select 'PER' as CODE UNION ALL
select 'URY' as CODE UNION ALL
select 'IND' as CODE UNION ALL
select 'LCA' as CODE UNION ALL
select 'VCT' as CODE
) as r1
on r.code = r1.code) AS FOO;
count
-------
43
(1 row)
-- query2
with alleuropeanlanguages as
(select country.code,country.name COUNTRY, city.name CAPITAL, language, isofficial, percentage
FROM country,city,countrylanguage
WHERE country.code = countrylanguage.countrycode
and country.capital = city.id
and country.continent = 'Europe')
select * from
(select * from alleuropeanlanguages where isofficial='True') e1,
(select * from alleuropeanlanguages where percentage > 50) e2
where e1.code = e2.code order by e2.COUNTRY,e1.language;
code | country | capital | language | isofficial | percentage | code | country | capital | language | isofficial | percentage
------+------------------------+-----------------------------------+----------------+------------+------------+------+------------------------+-----------------------------------+----------------+------------+------------
ALB | Albania | Tirana | Albaniana | t | 97.9 | ALB | Albania | Tirana | Albaniana | t | 97.9
AUT | Austria | Wien | German | t | 92 | AUT | Austria | Wien | German | t | 92
BLR | Belarus | Minsk | Belorussian | t | 65.6 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BLR | Belarus | Minsk | Russian | t | 32 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | French | t | 32.6 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | German | t | 1 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2 | BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2
BGR | Bulgaria | Sofija | Bulgariana | t | 83.2 | BGR | Bulgaria | Sofija | Bulgariana | t | 83.2
HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9 | HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Praha | Czech | t | 81.2
DNK | Denmark | Kobenhavn | Danish | t | 93.5 | DNK | Denmark | Kobenhavn | Danish | t | 93.5
EST | Estonia | Tallinn | Estonian | t | 65.3 | EST | Estonia | Tallinn | Estonian | t | 65.3
FRO | Faroe Islands | Torshavn | Danish | t | 0 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FRO | Faroe Islands | Torshavn | Faroese | t | 100 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FIN | Finland | Helsinki [Helsingfors] | Swedish | t | 5.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FRA | France | Paris | French | t | 93.6 | FRA | France | Paris | French | t | 93.6
DEU | Germany | Berlin | German | t | 91.3 | DEU | Germany | Berlin | German | t | 91.3
GIB | Gibraltar | Gibraltar | English | t | 88.9 | GIB | Gibraltar | Gibraltar | English | t | 88.9
GRC | Greece | Athenai | Greek | t | 98.5 | GRC | Greece | Athenai | Greek | t | 98.5
HUN | Hungary | Budapest | Hungarian | t | 98.5 | HUN | Hungary | Budapest | Hungarian | t | 98.5
ISL | Iceland | Reykjavik | Icelandic | t | 95.7 | ISL | Iceland | Reykjavik | Icelandic | t | 95.7
IRL | Ireland | Dublin | English | t | 98.4 | IRL | Ireland | Dublin | English | t | 98.4
IRL | Ireland | Dublin | Irish | t | 1.6 | IRL | Ireland | Dublin | English | t | 98.4
ITA | Italy | Roma | Italian | t | 94.1 | ITA | Italy | Roma | Italian | t | 94.1
LVA | Latvia | Riga | Latvian | t | 55.1 | LVA | Latvia | Riga | Latvian | t | 55.1
LIE | Liechtenstein | Vaduz | German | t | 89 | LIE | Liechtenstein | Vaduz | German | t | 89
LTU | Lithuania | Vilnius | Lithuanian | t | 81.6 | LTU | Lithuania | Vilnius | Lithuanian | t | 81.6
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | French | t | 4.2 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | German | t | 2.3 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
MKD | Macedonia | Skopje | Macedonian | t | 66.5 | MKD | Macedonia | Skopje | Macedonian | t | 66.5
MLT | Malta | Valletta | English | t | 2.1 | MLT | Malta | Valletta | Maltese | t | 95.8
MLT | Malta | Valletta | Maltese | t | 95.8 | MLT | Malta | Valletta | Maltese | t | 95.8
MDA | Moldova | Chisinau | Romanian | t | 61.9 | MDA | Moldova | Chisinau | Romanian | t | 61.9
NLD | Netherlands | Amsterdam | Dutch | t | 95.6 | NLD | Netherlands | Amsterdam | Dutch | t | 95.6
NOR | Norway | Oslo | Norwegian | t | 96.6 | NOR | Norway | Oslo | Norwegian | t | 96.6
POL | Poland | Warszawa | Polish | t | 97.6 | POL | Poland | Warszawa | Polish | t | 97.6
PRT | Portugal | Lisboa | Portuguese | t | 99 | PRT | Portugal | Lisboa | Portuguese | t | 99
ROM | Romania | Bucuresti | Romani | t | 0.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
ROM | Romania | Bucuresti | Romanian | t | 90.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
RUS | Russian Federation | Moscow | Russian | t | 86.6 | RUS | Russian Federation | Moscow | Russian | t | 86.6
SMR | San Marino | San Marino | Italian | t | 100 | SMR | San Marino | San Marino | Italian | t | 100
SVK | Slovakia | Bratislava | Slovak | t | 85.6 | SVK | Slovakia | Bratislava | Slovak | t | 85.6
SVN | Slovenia | Ljubljana | Slovene | t | 87.9 | SVN | Slovenia | Ljubljana | Slovene | t | 87.9
ESP | Spain | Madrid | Spanish | t | 74.4 | ESP | Spain | Madrid | Spanish | t | 74.4
SWE | Sweden | Stockholm | Swedish | t | 89.5 | SWE | Sweden | Stockholm | Swedish | t | 89.5
CHE | Switzerland | Bern | French | t | 19.2 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | German | t | 63.6 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Italian | t | 7.7 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Romansh | t | 0.6 | CHE | Switzerland | Bern | German | t | 63.6
UKR | Ukraine | Kyiv | Ukrainian | t | 64.7 | UKR | Ukraine | Kyiv | Ukrainian | t | 64.7
GBR | United Kingdom | London | English | t | 97.3 | GBR | United Kingdom | London | English | t | 97.3
YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2 | YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2
(55 rows)
-- query3
with allcountrystats as
( select country.code,country.name,count(distinct city.id) CITY_CNT,
count(distinct countrylanguage.language) LANG_CNT
from country,city,countrylanguage
where country.code = city.countrycode
and country.code = countrylanguage.countrycode
group by country.code,country.name
)
select sum(FOO.CITY_CNT) REGION_CITY_CNT,sum(FOO.LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > 1
and country.continent = 'Asia'
UNION ALL
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > 1
and country.continent = 'North America'
UNION ALL
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > (select max(CITY_CNT/LANG_CNT) from allcountrystats,country where allcountrystats.code = country.code AND country.continent='Europe')
) FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
region_city_cnt | region_lang_cnt | region
-----------------+-----------------+---------------------------
840 | 192 | Caribbean
2824 | 112 | Central America
11336 | 384 | Eastern Asia
2664 | 396 | Middle East
1625 | 125 | North America
3500 | 70 | South America
3179 | 528 | Southeast Asia
12278 | 896 | Southern and Central Asia
(8 rows)
--query 4
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select d1.code,d1.name,d1.capital,city.name CAPITAL_CITY,d1.CNT,d2.CNT
from
diversecountries d1 left join country
ON (d1.code = country.code AND d1.CNT < 8)
left join diversecountries d2
ON (country.code = d2.code AND d2.CNT > 8)
INNER JOIN city
ON(d1.capital = city.id)
ORDER BY d1.name;
code | name | capital | capital_city | cnt | cnt
------+--------------------+---------+------------------+-----+-----
AUS | Australia | 135 | Canberra | 8 |
AUT | Austria | 1523 | Wien | 8 |
CAN | Canada | 1822 | Ottawa | 12 |
CHN | China | 1891 | Peking | 12 |
DNK | Denmark | 3315 | Kobenhavn | 7 |
IND | India | 1109 | New Delhi | 12 |
IRN | Iran | 1380 | Teheran | 10 |
ITA | Italy | 1464 | Roma | 8 |
MMR | Myanmar | 2710 | Rangoon (Yangon) | 8 |
RUS | Russian Federation | 3580 | Moscow | 12 |
ZAF | South Africa | 716 | Pretoria | 11 |
USA | United States | 3813 | Washington | 12 |
(12 rows)
--query 5 Use CTE more than once in select list , from clause and where clause without correlation
with official_languages as
(
select country.code,country.name,countrylanguage.language
from
country,countrylanguage
where country.code = countrylanguage.countrycode and isofficial = 'True'
and country.governmentform NOT IN (select 'Commonwealth of the US' UNION ALL select 'Monarchy (Sultanate)' UNION ALL select 'Monarchy')
and country.gnp > (select min(gnpold) from country where country.region = 'Western Europe')
)
select
( select max(CNT) from (select count(*) CNT,o1.name from official_languages o1, official_languages o2
where o1.code = o2.code group by o1.name) FOO
),* from official_languages;
max | code | name | language
-----+------+------------------------+------------------
16 | AFG | Afghanistan | Dari
16 | ARM | Armenia | Armenian
16 | AUT | Austria | German
16 | BMU | Bermuda | English
16 | BRB | Barbados | English
16 | CHE | Switzerland | Romansh
16 | CHE | Switzerland | German
16 | COL | Colombia | Spanish
16 | CRI | Costa Rica | Spanish
16 | CZE | Czech Republic | Czech
16 | ECU | Ecuador | Spanish
16 | EGY | Egypt | Arabic
16 | FIN | Finland | Finnish
16 | GTM | Guatemala | Spanish
16 | GUM | Guam | Chamorro
16 | HND | Honduras | Spanish
16 | HRV | Croatia | Serbo-Croatian
16 | IDN | Indonesia | Malay
16 | IND | India | Hindi
16 | IRQ | Iraq | Arabic
16 | ISR | Israel | Arabic
16 | KAZ | Kazakstan | Kazakh
16 | KOR | South Korea | Korean
16 | LIE | Liechtenstein | German
16 | LKA | Sri Lanka | Tamil
16 | LTU | Lithuania | Lithuanian
16 | MDG | Madagascar | French
16 | MKD | Macedonia | Macedonian
16 | MLT | Malta | English
16 | MTQ | Martinique | French
16 | NCL | New Caledonia | French
16 | NIC | Nicaragua | Spanish
16 | PAN | Panama | Spanish
16 | PHL | Philippines | Pilipino
16 | PRY | Paraguay | Guarani
16 | ROM | Romania | Romanian
16 | ROM | Romania | Romani
16 | RWA | Rwanda | French
16 | SDN | Sudan | Arabic
16 | SEN | Senegal | Wolof
16 | SGP | Singapore | Tamil
16 | SVK | Slovakia | Slovak
16 | SYR | Syria | Arabic
16 | TUR | Turkey | Turkish
16 | TWN | Taiwan | Mandarin Chinese
16 | TZA | Tanzania | Swahili
16 | URY | Uruguay | Spanish
16 | VNM | Vietnam | Vietnamese
16 | ZAF | South Africa | Afrikaans
16 | ANT | Netherlands Antilles | Papiamento
16 | ARE | United Arab Emirates | Arabic
16 | AZE | Azerbaijan | Azerbaijani
16 | BEL | Belgium | French
16 | BIH | Bosnia and Herzegovina | Serbo-Croatian
16 | BLR | Belarus | Belorussian
16 | BLR | Belarus | Russian
16 | BOL | Bolivia | Spanish
16 | BOL | Bolivia | Aimara
16 | BRA | Brazil | Portuguese
16 | CAN | Canada | French
16 | CAN | Canada | English
16 | CHE | Switzerland | French
16 | CHL | Chile | Spanish
16 | COM | Comoros | Comorian
16 | CYP | Cyprus | Greek
16 | DEU | Germany | German
16 | DOM | Dominican Republic | Spanish
16 | DZA | Algeria | Arabic
16 | ESP | Spain | Spanish
16 | HKG | Hong Kong | English
16 | HTI | Haiti | French
16 | HUN | Hungary | Hungarian
16 | IRL | Ireland | English
16 | IRL | Ireland | Irish
16 | IRN | Iran | Persian
16 | ISL | Iceland | Icelandic
16 | ITA | Italy | Italian
16 | JOR | Jordan | Arabic
16 | JPN | Japan | Japanese
16 | KGZ | Kyrgyzstan | Russian
16 | LBY | Libyan Arab Jamahiriya | Arabic
16 | LUX | Luxembourg | Luxembourgish
16 | LUX | Luxembourg | German
16 | LUX | Luxembourg | French
16 | MAR | Morocco | Arabic
16 | MDG | Madagascar | Malagasy
16 | MWI | Malawi | Chichewa
16 | MYS | Malaysia | Malay
16 | NLD | Netherlands | Dutch
16 | NZL | New Zealand | English
16 | PER | Peru | Ketdua
16 | PER | Peru | Aimara
16 | PRT | Portugal | Portuguese
16 | PRY | Paraguay | Spanish
16 | RUS | Russian Federation | Russian
16 | RWA | Rwanda | Rwanda
16 | SGP | Singapore | Malay
16 | SGP | Singapore | Chinese
16 | SLV | El Salvador | Spanish
16 | SWE | Sweden | Swedish
16 | TCD | Chad | Arabic
16 | TGO | Togo | Kabye
16 | TGO | Togo | Ewe
16 | TUN | Tunisia | Arabic
16 | USA | United States | English
16 | UZB | Uzbekistan | Uzbek
16 | ZAF | South Africa | English
16 | ZAF | South Africa | Zulu
16 | AFG | Afghanistan | Pashto
16 | ALB | Albania | Albaniana
16 | AND | Andorra | Catalan
16 | ANT | Netherlands Antilles | Dutch
16 | ARG | Argentina | Spanish
16 | AUS | Australia | English
16 | BEL | Belgium | German
16 | BEL | Belgium | Dutch
16 | BGD | Bangladesh | Bengali
16 | BGR | Bulgaria | Bulgariana
16 | BHR | Bahrain | Arabic
16 | BOL | Bolivia | Ketdua
16 | CHE | Switzerland | Italian
16 | CHN | China | Chinese
16 | CUB | Cuba | Spanish
16 | CYM | Cayman Islands | English
16 | CYP | Cyprus | Turkish
16 | DNK | Denmark | Danish
16 | EST | Estonia | Estonian
16 | FIN | Finland | Swedish
16 | FJI | Fiji Islands | Fijian
16 | FRA | France | French
16 | GBR | United Kingdom | English
16 | GEO | Georgia | Georgiana
16 | GLP | Guadeloupe | French
16 | GRC | Greece | Greek
16 | GUM | Guam | English
16 | ISR | Israel | Hebrew
16 | KGZ | Kyrgyzstan | Kirgiz
16 | KHM | Cambodia | Khmer
16 | KWT | Kuwait | Arabic
16 | LAO | Laos | Lao
16 | LBN | Lebanon | Arabic
16 | LKA | Sri Lanka | Singali
16 | LVA | Latvia | Latvian
16 | MAC | Macao | Portuguese
16 | MDA | Moldova | Romanian
16 | MEX | Mexico | Spanish
16 | MLT | Malta | Maltese
16 | MMR | Myanmar | Burmese
16 | NOR | Norway | Norwegian
16 | NPL | Nepal | Nepali
16 | PAK | Pakistan | Urdu
16 | PER | Peru | Spanish
16 | POL | Poland | Polish
16 | PRK | North Korea | Korean
16 | SVN | Slovenia | Slovene
16 | THA | Thailand | Thai
16 | TJK | Tajikistan | Tadzhik
16 | TKM | Turkmenistan | Turkmenian
16 | UKR | Ukraine | Ukrainian
16 | VEN | Venezuela | Spanish
16 | YEM | Yemen | Arabic
16 | YUG | Yugoslavia | Serbo-Croatian
16 | ZAF | South Africa | Xhosa
16 | ZWE | Zimbabwe | English
(164 rows)
--query 6 Use CTE in the main query and subqueries within the main query
with bad_headofstates as
(
select country.code,country.name,country.headofstate,countrylanguage.language
from
country,countrylanguage
where country.code = countrylanguage.countrycode and countrylanguage.isofficial=true
and (country.gnp < country.gnpold or country.gnp < 3000)
)
select OUTERMOST_FOO.*,bad_headofstates.headofstate from (
select avg(population),region from
(
select FOO.*,bad_headofstates.headofstate,city.name
from
(select bad_headofstates.code,country.capital,country.region,country.population from
bad_headofstates,country where bad_headofstates.code = country.code) FOO, bad_headofstates,city
where FOO.code = bad_headofstates.code and FOO.capital = city.id) OUTER_FOO
group by region ) OUTERMOST_FOO,bad_headofstates,country
where country.code = bad_headofstates.code and country.region = OUTERMOST_FOO.region
order by OUTERMOST_FOO.region,bad_headofstates.headofstate LIMIT 40;
avg | region | headofstate
-----------------------+---------------------------+----------------------------------
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
744388.888888888889 | Caribbean | Beatrix
744388.888888888889 | Caribbean | Beatrix
744388.888888888889 | Caribbean | Beatrix
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Fidel Castro Ruz
744388.888888888889 | Caribbean | George W. Bush
744388.888888888889 | Caribbean | Jacques Chirac
7651000.000000000000 | Central Africa | Idriss Deby
2657500.000000000000 | Central America | Arnoldo Aleman Lacayo
2657500.000000000000 | Central America | Elisabeth II
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Bakili Muluzi
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | Isayas Afewerki [Isaias Afwerki]
5982809.523809523810 | Eastern Africa | Ismail Omar Guelleh
5982809.523809523810 | Eastern Africa | Jacques Chirac
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Robert G. Mugabe
34288500.000000000000 | Eastern Asia | Akihito
34288500.000000000000 | Eastern Asia | Chen Shui-bian
34288500.000000000000 | Eastern Asia | Jiang Zemin
(40 rows)
-- query 7 Use CTE in the main query, where clause and having clause
with district_population as
(select sum(city.population) DISTRICT_POP,count(*) NUM_CITIES,district,countrycode,country.name COUNTRY
from city,country
where city.countrycode = country.code
group by district,countrycode,country.name
HAVING (sum(city.population)/count(*)) > ( select avg(population) from city where countrycode = 'CHN'))
select sum(FOO.DISTRICT_POP),sum(FOO.NUM_CITIES),COUNTRY,CAPITAL,CAPITAL_POP
from
(
(select district_population.*,city.name CAPITAL,city.population CAPITAL_POP from
district_population,country,city
where district_population.countrycode = country.code AND city.id = country.capital
AND DISTRICT_POP >= ( select avg(DISTRICT_POP) FROM district_population where district IN (select district from city where countrycode = 'USA') )
order by COUNTRY,district)
UNION ALL
(select district_population.*,city.name CAPITAL,city.population CAPITAL_POP from
district_population,country,city
where district_population.countrycode = country.code AND city.id = country.capital
AND DISTRICT_POP >= ( select avg(DISTRICT_POP) FROM district_population where district IN (select district from city where countrycode = 'IND') )
order by COUNTRY,district)
UNION ALL
(select district_population.*,city.name CAPITAL,city.population CAPITAL_POP from
district_population,country,city
where district_population.countrycode = country.code AND city.id = country.capital
AND DISTRICT_POP >= ( select avg(DISTRICT_POP) FROM district_population where district IN (select district from city where countrycode = 'CHN') )
order by COUNTRY,district)
) FOO
WHERE FOO.CAPITAL_POP > (select min(DISTRICT_POP) from district_population)
group by FOO.COUNTRY,FOO.CAPITAL,FOO.CAPITAL_POP
HAVING sum(FOO.DISTRICT_POP) >= ( select avg(DISTRICT_POP) FROM district_population where district IN (select district from city where countrycode = 'AUS') )
AND (sum(FOO.DISTRICT_POP)/sum(FOO.NUM_CITIES)) <=
( select avg(DISTRICT_POP) FROM district_population where district IN (select district from city where countrycode = 'USA' or countrycode = 'IND' or countrycode = 'CHN'))
order by FOO.country;
sum | sum | country | capital | capital_pop
-----------+-----+---------------------------------------+-------------------+-------------
4577018 | 8 | Bangladesh | Dhaka | 3612850
35875413 | 66 | Brazil | Brasilia | 1969868
5423156 | 4 | Chile | Santiago de Chile | 4703954
146743375 | 155 | China | Peking | 7472000
6260862 | 1 | Colombia | Santafe de Bogota | 6260862
5064000 | 1 | Congo, The Democratic Republic of the | Kinshasa | 5064000
10117675 | 2 | Egypt | Cairo | 6789479
3386667 | 1 | Germany | Berlin | 3386667
25060392 | 18 | Iran | Teheran | 6758845
4336000 | 1 | Iraq | Baghdad | 4336000
48546381 | 84 | Japan | Tokyo | 7980230
29789325 | 11 | Mexico | Ciudad de Mexico | 8591309
3095329 | 2 | Morocco | Rabat | 623457
3361700 | 1 | Myanmar | Rangoon (Yangon) | 3361700
33060243 | 12 | Pakistan | Islamabad | 524500
29545923 | 48 | Philippines | Manila | 1581082
30717000 | 9 | Russian Federation | Moscow | 8389200
6846000 | 5 | Saudi Arabia | Riyadh | 3324000
4017733 | 1 | Singapore | Singapore | 4017733
3962883 | 8 | Spain | Madrid | 2879052
6320174 | 1 | Thailand | Bangkok | 6320174
26997078 | 6 | Turkey | Ankara | 3038159
26874255 | 18 | United States | Washington | 572059
3980000 | 1 | Vietnam | Hanoi | 1410000
(24 rows)
-- query8 Use CTE in the select list and the from clause
with official_languages as
(
select country.code,country.name,countrylanguage.language
from
country,countrylanguage
where country.code = countrylanguage.countrycode and isofficial = 'True'
)
select
( select max(CNT) from (select count(*) CNT from official_languages) FOO
)
,* from official_languages order by official_languages.code,official_languages.language;
max | code | name | language
-----+------+--------------------------------------+------------------
238 | ABW | Aruba | Dutch
238 | AFG | Afghanistan | Dari
238 | AFG | Afghanistan | Pashto
238 | AIA | Anguilla | English
238 | ALB | Albania | Albaniana
238 | AND | Andorra | Catalan
238 | ANT | Netherlands Antilles | Dutch
238 | ANT | Netherlands Antilles | Papiamento
238 | ARE | United Arab Emirates | Arabic
238 | ARG | Argentina | Spanish
238 | ARM | Armenia | Armenian
238 | ASM | American Samoa | English
238 | ASM | American Samoa | Samoan
238 | ATG | Antigua and Barbuda | English
238 | AUS | Australia | English
238 | AUT | Austria | German
238 | AZE | Azerbaijan | Azerbaijani
238 | BDI | Burundi | French
238 | BDI | Burundi | Kirundi
238 | BEL | Belgium | Dutch
238 | BEL | Belgium | French
238 | BEL | Belgium | German
238 | BGD | Bangladesh | Bengali
238 | BGR | Bulgaria | Bulgariana
238 | BHR | Bahrain | Arabic
238 | BIH | Bosnia and Herzegovina | Serbo-Croatian
238 | BLR | Belarus | Belorussian
238 | BLR | Belarus | Russian
238 | BLZ | Belize | English
238 | BMU | Bermuda | English
238 | BOL | Bolivia | Aimara
238 | BOL | Bolivia | Ketdua
238 | BOL | Bolivia | Spanish
238 | BRA | Brazil | Portuguese
238 | BRB | Barbados | English
238 | BRN | Brunei | Malay
238 | BTN | Bhutan | Dzongkha
238 | CAN | Canada | English
238 | CAN | Canada | French
238 | CCK | Cocos (Keeling) Islands | English
238 | CHE | Switzerland | French
238 | CHE | Switzerland | German
238 | CHE | Switzerland | Italian
238 | CHE | Switzerland | Romansh
238 | CHL | Chile | Spanish
238 | CHN | China | Chinese
238 | COK | Cook Islands | Maori
238 | COL | Colombia | Spanish
238 | COM | Comoros | Comorian
238 | CPV | Cape Verde | Portuguese
238 | CRI | Costa Rica | Spanish
238 | CUB | Cuba | Spanish
238 | CXR | Christmas Island | English
238 | CYM | Cayman Islands | English
238 | CYP | Cyprus | Greek
238 | CYP | Cyprus | Turkish
238 | CZE | Czech Republic | Czech
238 | DEU | Germany | German
238 | DJI | Djibouti | Arabic
238 | DNK | Denmark | Danish
238 | DOM | Dominican Republic | Spanish
238 | DZA | Algeria | Arabic
238 | ECU | Ecuador | Spanish
238 | EGY | Egypt | Arabic
238 | ERI | Eritrea | Tigrinja
238 | ESH | Western Sahara | Arabic
238 | ESP | Spain | Spanish
238 | EST | Estonia | Estonian
238 | FIN | Finland | Finnish
238 | FIN | Finland | Swedish
238 | FJI | Fiji Islands | Fijian
238 | FLK | Falkland Islands | English
238 | FRA | France | French
238 | FRO | Faroe Islands | Danish
238 | FRO | Faroe Islands | Faroese
238 | GBR | United Kingdom | English
238 | GEO | Georgia | Georgiana
238 | GIB | Gibraltar | English
238 | GLP | Guadeloupe | French
238 | GNB | Guinea-Bissau | Portuguese
238 | GRC | Greece | Greek
238 | GRL | Greenland | Danish
238 | GRL | Greenland | Greenlandic
238 | GTM | Guatemala | Spanish
238 | GUM | Guam | Chamorro
238 | GUM | Guam | English
238 | HKG | Hong Kong | English
238 | HND | Honduras | Spanish
238 | HRV | Croatia | Serbo-Croatian
238 | HTI | Haiti | French
238 | HUN | Hungary | Hungarian
238 | IDN | Indonesia | Malay
238 | IND | India | Hindi
238 | IRL | Ireland | English
238 | IRL | Ireland | Irish
238 | IRN | Iran | Persian
238 | IRQ | Iraq | Arabic
238 | ISL | Iceland | Icelandic
238 | ISR | Israel | Arabic
238 | ISR | Israel | Hebrew
238 | ITA | Italy | Italian
238 | JOR | Jordan | Arabic
238 | JPN | Japan | Japanese
238 | KAZ | Kazakstan | Kazakh
238 | KGZ | Kyrgyzstan | Kirgiz
238 | KGZ | Kyrgyzstan | Russian
238 | KHM | Cambodia | Khmer
238 | KIR | Kiribati | Kiribati
238 | KNA | Saint Kitts and Nevis | English
238 | KOR | South Korea | Korean
238 | KWT | Kuwait | Arabic
238 | LAO | Laos | Lao
238 | LBN | Lebanon | Arabic
238 | LBY | Libyan Arab Jamahiriya | Arabic
238 | LCA | Saint Lucia | English
238 | LIE | Liechtenstein | German
238 | LKA | Sri Lanka | Singali
238 | LKA | Sri Lanka | Tamil
238 | LSO | Lesotho | English
238 | LSO | Lesotho | Sotho
238 | LTU | Lithuania | Lithuanian
238 | LUX | Luxembourg | French
238 | LUX | Luxembourg | German
238 | LUX | Luxembourg | Luxembourgish
238 | LVA | Latvia | Latvian
238 | MAC | Macao | Portuguese
238 | MAR | Morocco | Arabic
238 | MCO | Monaco | French
238 | MDA | Moldova | Romanian
238 | MDG | Madagascar | French
238 | MDG | Madagascar | Malagasy
238 | MDV | Maldives | Dhivehi
238 | MEX | Mexico | Spanish
238 | MHL | Marshall Islands | English
238 | MHL | Marshall Islands | Marshallese
238 | MKD | Macedonia | Macedonian
238 | MLT | Malta | English
238 | MLT | Malta | Maltese
238 | MMR | Myanmar | Burmese
238 | MNG | Mongolia | Mongolian
238 | MNP | Northern Mariana Islands | English
238 | MSR | Montserrat | English
238 | MTQ | Martinique | French
238 | MWI | Malawi | Chichewa
238 | MYS | Malaysia | Malay
238 | MYT | Mayotte | French
238 | NCL | New Caledonia | French
238 | NFK | Norfolk Island | English
238 | NIC | Nicaragua | Spanish
238 | NIU | Niue | English
238 | NLD | Netherlands | Dutch
238 | NOR | Norway | Norwegian
238 | NPL | Nepal | Nepali
238 | NRU | Nauru | English
238 | NRU | Nauru | Nauru
238 | NZL | New Zealand | English
238 | OMN | Oman | Arabic
238 | PAK | Pakistan | Urdu
238 | PAN | Panama | Spanish
238 | PER | Peru | Aimara
238 | PER | Peru | Ketdua
238 | PER | Peru | Spanish
238 | PHL | Philippines | Pilipino
238 | PLW | Palau | English
238 | PLW | Palau | Palau
238 | POL | Poland | Polish
238 | PRI | Puerto Rico | Spanish
238 | PRK | North Korea | Korean
238 | PRT | Portugal | Portuguese
238 | PRY | Paraguay | Guarani
238 | PRY | Paraguay | Spanish
238 | PYF | French Polynesia | French
238 | QAT | Qatar | Arabic
238 | ROM | Romania | Romani
238 | ROM | Romania | Romanian
238 | RUS | Russian Federation | Russian
238 | RWA | Rwanda | French
238 | RWA | Rwanda | Rwanda
238 | SAU | Saudi Arabia | Arabic
238 | SDN | Sudan | Arabic
238 | SEN | Senegal | Wolof
238 | SGP | Singapore | Chinese
238 | SGP | Singapore | Malay
238 | SGP | Singapore | Tamil
238 | SHN | Saint Helena | English
238 | SJM | Svalbard and Jan Mayen | Norwegian
238 | SLV | El Salvador | Spanish
238 | SMR | San Marino | Italian
238 | SOM | Somalia | Arabic
238 | SOM | Somalia | Somali
238 | SPM | Saint Pierre and Miquelon | French
238 | SVK | Slovakia | Slovak
238 | SVN | Slovenia | Slovene
238 | SWE | Sweden | Swedish
238 | SWZ | Swaziland | Swazi
238 | SYC | Seychelles | English
238 | SYC | Seychelles | French
238 | SYR | Syria | Arabic
238 | TCA | Turks and Caicos Islands | English
238 | TCD | Chad | Arabic
238 | TGO | Togo | Ewe
238 | TGO | Togo | Kabye
238 | THA | Thailand | Thai
238 | TJK | Tajikistan | Tadzhik
238 | TKL | Tokelau | English
238 | TKM | Turkmenistan | Turkmenian
238 | TMP | East Timor | Portuguese
238 | TON | Tonga | English
238 | TON | Tonga | Tongan
238 | TUN | Tunisia | Arabic
238 | TUR | Turkey | Turkish
238 | TUV | Tuvalu | English
238 | TUV | Tuvalu | Tuvalu
238 | TWN | Taiwan | Mandarin Chinese
238 | TZA | Tanzania | Swahili
238 | UKR | Ukraine | Ukrainian
238 | UMI | United States Minor Outlying Islands | English
238 | URY | Uruguay | Spanish
238 | USA | United States | English
238 | UZB | Uzbekistan | Uzbek
238 | VAT | Holy See (Vatican City State) | Italian
238 | VCT | Saint Vincent and the Grenadines | English
238 | VEN | Venezuela | Spanish
238 | VGB | Virgin Islands, British | English
238 | VIR | Virgin Islands, U.S. | English
238 | VNM | Vietnam | Vietnamese
238 | VUT | Vanuatu | Bislama
238 | VUT | Vanuatu | English
238 | VUT | Vanuatu | French
238 | WSM | Samoa | English
238 | WSM | Samoa | Samoan
238 | YEM | Yemen | Arabic
238 | YUG | Yugoslavia | Serbo-Croatian
238 | ZAF | South Africa | Afrikaans
238 | ZAF | South Africa | English
238 | ZAF | South Africa | Xhosa
238 | ZAF | South Africa | Zulu
238 | ZWE | Zimbabwe | English
(238 rows)
-- queries with CTEs using index scans
set enable_seqscan=off;
set enable_indexscan=on;
--query1
with capitals as
(select country.code,id,city.name from city,country
where city.countrycode = country.code AND city.id = country.capital)
select * from
capitals,countrylanguage
where capitals.code = countrylanguage.countrycode and isofficial='true'
order by capitals.code,countrylanguage.language;
code | id | name | countrycode | language | isofficial | percentage
------+------+-----------------------------------+-------------+------------------+------------+------------
ABW | 129 | Oranjestad | ABW | Dutch | t | 5.3
AFG | 1 | Kabul | AFG | Dari | t | 32.1
AFG | 1 | Kabul | AFG | Pashto | t | 52.4
AIA | 62 | The Valley | AIA | English | t | 0
ALB | 34 | Tirana | ALB | Albaniana | t | 97.9
AND | 55 | Andorra la Vella | AND | Catalan | t | 32.3
ANT | 33 | Willemstad | ANT | Dutch | t | 0
ANT | 33 | Willemstad | ANT | Papiamento | t | 86.2
ARE | 65 | Abu Dhabi | ARE | Arabic | t | 42
ARG | 69 | Buenos Aires | ARG | Spanish | t | 96.8
ARM | 126 | Yerevan | ARM | Armenian | t | 93.4
ASM | 54 | Fagatogo | ASM | English | t | 3.1
ASM | 54 | Fagatogo | ASM | Samoan | t | 90.6
ATG | 63 | Saint Johns | ATG | English | t | 0
AUS | 135 | Canberra | AUS | English | t | 81.2
AUT | 1523 | Wien | AUT | German | t | 92
AZE | 144 | Baku | AZE | Azerbaijani | t | 89
BDI | 552 | Bujumbura | BDI | French | t | 0
BDI | 552 | Bujumbura | BDI | Kirundi | t | 98.1
BEL | 179 | Bruxelles [Brussel] | BEL | Dutch | t | 59.2
BEL | 179 | Bruxelles [Brussel] | BEL | French | t | 32.6
BEL | 179 | Bruxelles [Brussel] | BEL | German | t | 1
BGD | 150 | Dhaka | BGD | Bengali | t | 97.7
BGR | 539 | Sofija | BGR | Bulgariana | t | 83.2
BHR | 149 | al-Manama | BHR | Arabic | t | 67.7
BIH | 201 | Sarajevo | BIH | Serbo-Croatian | t | 99.2
BLR | 3520 | Minsk | BLR | Belorussian | t | 65.6
BLR | 3520 | Minsk | BLR | Russian | t | 32
BLZ | 185 | Belmopan | BLZ | English | t | 50.8
BMU | 191 | Hamilton | BMU | English | t | 100
BOL | 194 | La Paz | BOL | Aimara | t | 3.2
BOL | 194 | La Paz | BOL | Ketdua | t | 8.1
BOL | 194 | La Paz | BOL | Spanish | t | 87.7
BRA | 211 | Brasilia | BRA | Portuguese | t | 97.5
BRB | 174 | Bridgetown | BRB | English | t | 0
BRN | 538 | Bandar Seri Begawan | BRN | Malay | t | 45.5
BTN | 192 | Thimphu | BTN | Dzongkha | t | 50
CAN | 1822 | Ottawa | CAN | English | t | 60.4
CAN | 1822 | Ottawa | CAN | French | t | 23.4
CCK | 2317 | West Island | CCK | English | t | 0
CHE | 3248 | Bern | CHE | French | t | 19.2
CHE | 3248 | Bern | CHE | German | t | 63.6
CHE | 3248 | Bern | CHE | Italian | t | 7.7
CHE | 3248 | Bern | CHE | Romansh | t | 0.6
CHL | 554 | Santiago de Chile | CHL | Spanish | t | 89.7
CHN | 1891 | Peking | CHN | Chinese | t | 92
COK | 583 | Avarua | COK | Maori | t | 0
COL | 2257 | Santafe de Bogota | COL | Spanish | t | 99
COM | 2295 | Moroni | COM | Comorian | t | 75
CPV | 1859 | Praia | CPV | Portuguese | t | 0
CRI | 584 | San Jose | CRI | Spanish | t | 97.5
CUB | 2413 | La Habana | CUB | Spanish | t | 100
CXR | 1791 | Flying Fish Cove | CXR | English | t | 0
CYM | 553 | George Town | CYM | English | t | 0
CYP | 2430 | Nicosia | CYP | Greek | t | 74.1
CYP | 2430 | Nicosia | CYP | Turkish | t | 22.4
CZE | 3339 | Praha | CZE | Czech | t | 81.2
DEU | 3068 | Berlin | DEU | German | t | 91.3
DJI | 585 | Djibouti | DJI | Arabic | t | 10.6
DNK | 3315 | Kobenhavn | DNK | Danish | t | 93.5
DOM | 587 | Santo Domingo de Guzman | DOM | Spanish | t | 98
DZA | 35 | Alger | DZA | Arabic | t | 86
ECU | 594 | Quito | ECU | Spanish | t | 93
EGY | 608 | Cairo | EGY | Arabic | t | 98.8
ERI | 652 | Asmara | ERI | Tigrinja | t | 49.1
ESH | 2453 | El-Aaiun | ESH | Arabic | t | 100
ESP | 653 | Madrid | ESP | Spanish | t | 74.4
EST | 3791 | Tallinn | EST | Estonian | t | 65.3
FIN | 3236 | Helsinki [Helsingfors] | FIN | Finnish | t | 92.7
FIN | 3236 | Helsinki [Helsingfors] | FIN | Swedish | t | 5.7
FJI | 764 | Suva | FJI | Fijian | t | 50.8
FLK | 763 | Stanley | FLK | English | t | 0
FRA | 2974 | Paris | FRA | French | t | 93.6
FRO | 901 | Torshavn | FRO | Danish | t | 0
FRO | 901 | Torshavn | FRO | Faroese | t | 100
GBR | 456 | London | GBR | English | t | 97.3
GEO | 905 | Tbilisi | GEO | Georgiana | t | 71.7
GIB | 915 | Gibraltar | GIB | English | t | 88.9
GLP | 919 | Basse-Terre | GLP | French | t | 0
GNB | 927 | Bissau | GNB | Portuguese | t | 8.1
GRC | 2401 | Athenai | GRC | Greek | t | 98.5
GRL | 917 | Nuuk | GRL | Danish | t | 12.5
GRL | 917 | Nuuk | GRL | Greenlandic | t | 87.5
GTM | 922 | Ciudad de Guatemala | GTM | Spanish | t | 64.7
GUM | 921 | Agaaa | GUM | Chamorro | t | 29.6
GUM | 921 | Agaaa | GUM | English | t | 37.5
HKG | 937 | Victoria | HKG | English | t | 2.2
HND | 933 | Tegucigalpa | HND | Spanish | t | 97.2
HRV | 2409 | Zagreb | HRV | Serbo-Croatian | t | 95.9
HTI | 929 | Port-au-Prince | HTI | French | t | 0
HUN | 3483 | Budapest | HUN | Hungarian | t | 98.5
IDN | 939 | Jakarta | IDN | Malay | t | 12.1
IND | 1109 | New Delhi | IND | Hindi | t | 39.9
IRL | 1447 | Dublin | IRL | English | t | 98.4
IRL | 1447 | Dublin | IRL | Irish | t | 1.6
IRN | 1380 | Teheran | IRN | Persian | t | 45.7
IRQ | 1365 | Baghdad | IRQ | Arabic | t | 77.2
ISL | 1449 | Reykjavik | ISL | Icelandic | t | 95.7
ISR | 1450 | Jerusalem | ISR | Arabic | t | 18
ISR | 1450 | Jerusalem | ISR | Hebrew | t | 63.1
ITA | 1464 | Roma | ITA | Italian | t | 94.1
JOR | 1786 | Amman | JOR | Arabic | t | 97.9
JPN | 1532 | Tokyo | JPN | Japanese | t | 99.1
KAZ | 1864 | Astana | KAZ | Kazakh | t | 46
KGZ | 2253 | Bishkek | KGZ | Kirgiz | t | 59.7
KGZ | 2253 | Bishkek | KGZ | Russian | t | 16.2
KHM | 1800 | Phnom Penh | KHM | Khmer | t | 88.6
KIR | 2256 | Bairiki | KIR | Kiribati | t | 98.9
KNA | 3064 | Basseterre | KNA | English | t | 0
KOR | 2331 | Seoul | KOR | Korean | t | 99.9
KWT | 2429 | Kuwait | KWT | Arabic | t | 78.1
LAO | 2432 | Vientiane | LAO | Lao | t | 67.2
LBN | 2438 | Beirut | LBN | Arabic | t | 93
LBY | 2441 | Tripoli | LBY | Arabic | t | 96
LCA | 3065 | Castries | LCA | English | t | 20
LIE | 2446 | Vaduz | LIE | German | t | 89
LKA | 3217 | Colombo | LKA | Singali | t | 60.3
LKA | 3217 | Colombo | LKA | Tamil | t | 19.6
LSO | 2437 | Maseru | LSO | English | t | 0
LSO | 2437 | Maseru | LSO | Sotho | t | 85
LTU | 2447 | Vilnius | LTU | Lithuanian | t | 81.6
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | French | t | 4.2
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | German | t | 2.3
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | Luxembourgish | t | 64.4
LVA | 2434 | Riga | LVA | Latvian | t | 55.1
MAC | 2454 | Macao | MAC | Portuguese | t | 2.3
MAR | 2486 | Rabat | MAR | Arabic | t | 65
MCO | 2695 | Monaco-Ville | MCO | French | t | 41.9
MDA | 2690 | Chisinau | MDA | Romanian | t | 61.9
MDG | 2455 | Antananarivo | MDG | French | t | 0
MDG | 2455 | Antananarivo | MDG | Malagasy | t | 98.9
MDV | 2463 | Male | MDV | Dhivehi | t | 100
MEX | 2515 | Ciudad de Mexico | MEX | Spanish | t | 92.1
MHL | 2507 | Dalap-Uliga-Darrit | MHL | English | t | 0
MHL | 2507 | Dalap-Uliga-Darrit | MHL | Marshallese | t | 96.8
MKD | 2460 | Skopje | MKD | Macedonian | t | 66.5
MLT | 2484 | Valletta | MLT | English | t | 2.1
MLT | 2484 | Valletta | MLT | Maltese | t | 95.8
MMR | 2710 | Rangoon (Yangon) | MMR | Burmese | t | 69
MNG | 2696 | Ulan Bator | MNG | Mongolian | t | 78.8
MNP | 2913 | Garapan | MNP | English | t | 4.8
MSR | 2697 | Plymouth | MSR | English | t | 0
MTQ | 2508 | Fort-de-France | MTQ | French | t | 0
MWI | 2462 | Lilongwe | MWI | Chichewa | t | 58.3
MYS | 2464 | Kuala Lumpur | MYS | Malay | t | 58.4
MYT | 2514 | Mamoutzou | MYT | French | t | 20.3
NCL | 3493 | Noumea | NCL | French | t | 34.3
NFK | 2806 | Kingston | NFK | English | t | 0
NIC | 2734 | Managua | NIC | Spanish | t | 97.6
NIU | 2805 | Alofi | NIU | English | t | 0
NLD | 5 | Amsterdam | NLD | Dutch | t | 95.6
NOR | 2807 | Oslo | NOR | Norwegian | t | 96.6
NPL | 2729 | Kathmandu | NPL | Nepali | t | 50.4
NRU | 2728 | Yaren | NRU | English | t | 7.5
NRU | 2728 | Yaren | NRU | Nauru | t | 57.5
NZL | 3499 | Wellington | NZL | English | t | 87
OMN | 2821 | Masqat | OMN | Arabic | t | 76.7
PAK | 2831 | Islamabad | PAK | Urdu | t | 7.6
PAN | 2882 | Ciudad de Panama | PAN | Spanish | t | 76.8
PER | 2890 | Lima | PER | Aimara | t | 2.3
PER | 2890 | Lima | PER | Ketdua | t | 16.4
PER | 2890 | Lima | PER | Spanish | t | 79.8
PHL | 766 | Manila | PHL | Pilipino | t | 29.3
PLW | 2881 | Koror | PLW | English | t | 3.2
PLW | 2881 | Koror | PLW | Palau | t | 82.2
POL | 2928 | Warszawa | POL | Polish | t | 97.6
PRI | 2919 | San Juan | PRI | Spanish | t | 51.3
PRK | 2318 | Pyongyang | PRK | Korean | t | 99.9
PRT | 2914 | Lisboa | PRT | Portuguese | t | 99
PRY | 2885 | Asuncion | PRY | Guarani | t | 40.1
PRY | 2885 | Asuncion | PRY | Spanish | t | 55.1
PYF | 3016 | Papeete | PYF | French | t | 40.8
QAT | 2973 | Doha | QAT | Arabic | t | 40.7
ROM | 3018 | Bucuresti | ROM | Romani | t | 0.7
ROM | 3018 | Bucuresti | ROM | Romanian | t | 90.7
RUS | 3580 | Moscow | RUS | Russian | t | 86.6
RWA | 3047 | Kigali | RWA | French | t | 0
RWA | 3047 | Kigali | RWA | Rwanda | t | 100
SAU | 3173 | Riyadh | SAU | Arabic | t | 95
SDN | 3225 | Khartum | SDN | Arabic | t | 49.4
SEN | 3198 | Dakar | SEN | Wolof | t | 48.1
SGP | 3208 | Singapore | SGP | Chinese | t | 77.1
SGP | 3208 | Singapore | SGP | Malay | t | 14.1
SGP | 3208 | Singapore | SGP | Tamil | t | 7.4
SHN | 3063 | Jamestown | SHN | English | t | 0
SJM | 938 | Longyearbyen | SJM | Norwegian | t | 0
SLV | 645 | San Salvador | SLV | Spanish | t | 100
SMR | 3171 | San Marino | SMR | Italian | t | 100
SOM | 3214 | Mogadishu | SOM | Arabic | t | 0
SOM | 3214 | Mogadishu | SOM | Somali | t | 98.3
SPM | 3067 | Saint-Pierre | SPM | French | t | 0
SVK | 3209 | Bratislava | SVK | Slovak | t | 85.6
SVN | 3212 | Ljubljana | SVN | Slovene | t | 87.9
SWE | 3048 | Stockholm | SWE | Swedish | t | 89.5
SWZ | 3244 | Mbabane | SWZ | Swazi | t | 89.9
SYC | 3206 | Victoria | SYC | English | t | 3.8
SYC | 3206 | Victoria | SYC | French | t | 1.3
SYR | 3250 | Damascus | SYR | Arabic | t | 90
TCA | 3423 | Cockburn Town | TCA | English | t | 0
TCD | 3337 | NDjamena | TCD | Arabic | t | 12.3
TGO | 3332 | Lome | TGO | Ewe | t | 23.2
TGO | 3332 | Lome | TGO | Kabye | t | 13.8
THA | 3320 | Bangkok | THA | Thai | t | 52.6
TJK | 3261 | Dushanbe | TJK | Tadzhik | t | 62.2
TKL | 3333 | Fakaofo | TKL | English | t | 0
TKM | 3419 | Ashgabat | TKM | Turkmenian | t | 76.7
TMP | 1522 | Dili | TMP | Portuguese | t | 0
TON | 3334 | Nukualofa | TON | English | t | 0
TON | 3334 | Nukualofa | TON | Tongan | t | 98.3
TUN | 3349 | Tunis | TUN | Arabic | t | 69.9
TUR | 3358 | Ankara | TUR | Turkish | t | 87.6
TUV | 3424 | Funafuti | TUV | English | t | 0
TUV | 3424 | Funafuti | TUV | Tuvalu | t | 92.5
TWN | 3263 | Taipei | TWN | Mandarin Chinese | t | 20.1
TZA | 3306 | Dodoma | TZA | Swahili | t | 8.8
UKR | 3426 | Kyiv | UKR | Ukrainian | t | 64.7
URY | 3492 | Montevideo | URY | Spanish | t | 95.7
USA | 3813 | Washington | USA | English | t | 86.2
UZB | 3503 | Toskent | UZB | Uzbek | t | 72.6
VAT | 3538 | Citta del Vaticano | VAT | Italian | t | 0
VCT | 3066 | Kingstown | VCT | English | t | 0
VEN | 3539 | Caracas | VEN | Spanish | t | 96.9
VGB | 537 | Road Town | VGB | English | t | 0
VIR | 4067 | Charlotte Amalie | VIR | English | t | 81.7
VNM | 3770 | Hanoi | VNM | Vietnamese | t | 86.8
VUT | 3537 | Port-Vila | VUT | Bislama | t | 56.6
VUT | 3537 | Port-Vila | VUT | English | t | 28.3
VUT | 3537 | Port-Vila | VUT | French | t | 14.2
WSM | 3169 | Apia | WSM | English | t | 0.6
WSM | 3169 | Apia | WSM | Samoan | t | 47.5
YEM | 1780 | Sanaa | YEM | Arabic | t | 99.6
YUG | 1792 | Beograd | YUG | Serbo-Croatian | t | 75.2
ZAF | 716 | Pretoria | ZAF | Afrikaans | t | 14.3
ZAF | 716 | Pretoria | ZAF | English | t | 8.5
ZAF | 716 | Pretoria | ZAF | Xhosa | t | 17.7
ZAF | 716 | Pretoria | ZAF | Zulu | t | 22.7
ZWE | 4068 | Harare | ZWE | English | t | 2.2
(237 rows)
--query2
with lang_total as
( select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
select lang_count,country.code,country.name,country.continent,country.region,country.population
from country left outer join lang_total
on (lang_total.code = country.code)
where country.indepyear > 1970 and lang_total.lang_count >=1 and lang_total.lang_count >=1
order by lang_total.lang_count desc;
lang_count | code | name | continent | region | population
------------+------+---------------------------------+-----------+-----------------+------------
12 | RUS | Russian Federation | Europe | Eastern Europe | 146934000
6 | FSM | Micronesia, Federated States of | Oceania | Micronesia | 119000
4 | AZE | Azerbaijan | Asia | Middle East | 7734000
1 | BIH | Bosnia and Herzegovina | Europe | Southern Europe | 3972000
(4 rows)
-- Using CTE in the WHERE clause
--query3
with lang_total as
( select lang_count,countrycode from
(
(
select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
UNION ALL
(
select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Monarchy')
group by country.code,countrylanguage.countrycode order by country.code)
) FOO1
)
select * from
(
select count(*) as cnt,country.code,country.name
from
country,countrylanguage
where country.code=countrylanguage.countrycode group by country.code,country.name) AS FOO
where foo.cnt = (select max(lang_count) from lang_total) order by foo.code;
cnt | code | name
-----+------+--------------------
12 | CAN | Canada
12 | CHN | China
12 | IND | India
12 | RUS | Russian Federation
12 | USA | United States
(5 rows)
--query4
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select diversecountries.name,city.name,diversecountries.CNT
from diversecountries,city where city.id = diversecountries.capital
order by diversecountries.name;
name | name | cnt
--------------------+------------------+-----
Australia | Canberra | 8
Austria | Wien | 8
Canada | Ottawa | 12
China | Peking | 12
Denmark | Kobenhavn | 7
India | New Delhi | 12
Iran | Teheran | 10
Italy | Roma | 8
Myanmar | Rangoon (Yangon) | 8
Russian Federation | Moscow | 12
South Africa | Pretoria | 11
United States | Washington | 12
(12 rows)
-- some queries with merge joins and index scans
set enable_nestloop=off;
set enable_hashjoin=off;
set enable_mergejoin=on;
-- query 5
with somecheapasiandiversecountries as
(
select FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,count(*) ASIAN_COUNT from
(
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Southeast Asia'
and country.continent = 'Asia'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Eastern Asia'
and country.continent = 'Asia'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Middle East'
and country.continent = 'Asia'
) FOO, countrylanguage
where FOO.code = countrylanguage.countrycode
group by FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,countrylanguage.countrycode
HAVING count(*) >=
(select min(CNT) FROM
(select count(*) CNT,country.code from countrylanguage,country
where countrylanguage.countrycode=country.code
and country.continent = 'Asia'
and country.region = 'Southern and Central Asia'
group by country.code
) FOO1
)
)
select FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,count(*) COMPARED_WITH_CHEAP_ASIAN_CNT
from
(
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.continent = 'North America'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.continent = 'South America'
) FOO,countrylanguage
where FOO.code = countrylanguage.countrycode
group by FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate
HAVING count(*) >=
(select min(ASIAN_COUNT) FROM
(select ASIAN_COUNT FROM somecheapasiandiversecountries,country
where somecheapasiandiversecountries.code = country.code
and country.gnp >= country.gnpold
) ASIANCOUNT
)
order by COUNTRY;
code | country | capital | headofstate | compared_with_cheap_asian_cnt
------+----------------------------------+-------------------------+-----------------------------------+-------------------------------
ATG | Antigua and Barbuda | Saint Johns | Elisabeth II | 2
ARG | Argentina | Buenos Aires | Fernando de la Rua | 3
ABW | Aruba | Oranjestad | Beatrix | 4
BHS | Bahamas | Nassau | Elisabeth II | 2
BRB | Barbados | Bridgetown | Elisabeth II | 2
BLZ | Belize | Belmopan | Elisabeth II | 4
BOL | Bolivia | La Paz | Hugo Banzer Suarez | 4
BRA | Brazil | Brasilia | Fernando Henrique Cardoso | 5
CAN | Canada | Ottawa | Elisabeth II | 12
CHL | Chile | Santiago de Chile | Ricardo Lagos Escobar | 4
COL | Colombia | Santafe de Bogota | Andres Pastrana Arango | 5
CRI | Costa Rica | San Jose | Miguel Angel Rodriguez Echeverria | 4
DMA | Dominica | Roseau | Vernon Shaw | 2
DOM | Dominican Republic | Santo Domingo de Guzman | Hipolito Mejia Dominguez | 2
ECU | Ecuador | Quito | Gustavo Noboa Bejarano | 2
SLV | El Salvador | San Salvador | Francisco Guillermo Flores Perez | 2
GUF | French Guiana | Cayenne | Jacques Chirac | 2
GRL | Greenland | Nuuk | Margrethe II | 2
GLP | Guadeloupe | Basse-Terre | Jacques Chirac | 2
GTM | Guatemala | Ciudad de Guatemala | Alfonso Portillo Cabrera | 5
GUY | Guyana | Georgetown | Bharrat Jagdeo | 3
HTI | Haiti | Port-au-Prince | Jean-Bertrand Aristide | 2
HND | Honduras | Tegucigalpa | Carlos Roberto Flores Facusse | 4
JAM | Jamaica | Kingston | Elisabeth II | 2
MTQ | Martinique | Fort-de-France | Jacques Chirac | 2
MEX | Mexico | Ciudad de Mexico | Vicente Fox Quesada | 6
ANT | Netherlands Antilles | Willemstad | Beatrix | 3
NIC | Nicaragua | Managua | Arnoldo Aleman Lacayo | 4
PAN | Panama | Ciudad de Panama | Mireya Elisa Moscoso Rodriguez | 6
PRY | Paraguay | Asuncion | Luis Angel Gonzalez Macchi | 4
PER | Peru | Lima | Valentin Paniagua Corazao | 3
PRI | Puerto Rico | San Juan | George W. Bush | 2
KNA | Saint Kitts and Nevis | Basseterre | Elisabeth II | 2
LCA | Saint Lucia | Castries | Elisabeth II | 2
VCT | Saint Vincent and the Grenadines | Kingstown | Elisabeth II | 2
SUR | Suriname | Paramaribo | Ronald Venetiaan | 2
TTO | Trinidad and Tobago | Port-of-Spain | Arthur N. R. Robinson | 3
USA | United States | Washington | George W. Bush | 12
VEN | Venezuela | Caracas | Hugo Chavez Frias | 3
VIR | Virgin Islands, U.S. | Charlotte Amalie | George W. Bush | 3
(40 rows)
-- query 6
select count(*) from
( select r.* from
( with fact as
(
select country.name as COUNTRY,country.code,city.name as CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,AGG1.region
from
(select
sum(case when (city.population >= 0.5 * country.population) then country.population else city.population end) as S_POPULATION,
sum(case when (gnp >= gnpold) then gnp else gnpold end) as S_GNP,
avg(case when (lifeexpectancy > 60) then 50 else lifeexpectancy end) as AVG_LIFE,country.region
from country,city
where governmentform != 'Constitutional Monarchy'
and country.capital = city.id
and indepyear > 0
group by country.region) AGG1
,country,city
where country.capital = city.id
and country.region = AGG1.region
)
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'South America'
UNION ALL
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'North America'
UNION ALL
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'Caribbean'
) as r
left join
(
select 'ARG' as CODE UNION ALL
select 'BOL' as CODE UNION ALL
select 'BRA' as CODE UNION ALL
select 'PER' as CODE UNION ALL
select 'URY' as CODE UNION ALL
select 'IND' as CODE UNION ALL
select 'LCA' as CODE UNION ALL
select 'VCT' as CODE
) as r1
on r.code = r1.code) AS FOO;
count
-------
43
(1 row)
-- query7
with alleuropeanlanguages as
(select country.code,country.name COUNTRY, city.name CAPITAL, language, isofficial, percentage
FROM country,city,countrylanguage
WHERE country.code = countrylanguage.countrycode
and country.capital = city.id
and country.continent = 'Europe')
select * from
(select * from alleuropeanlanguages where isofficial='True') e1,
(select * from alleuropeanlanguages where percentage > 50) e2
where e1.code = e2.code order by e2.COUNTRY,e1.language;
code | country | capital | language | isofficial | percentage | code | country | capital | language | isofficial | percentage
------+------------------------+-----------------------------------+----------------+------------+------------+------+------------------------+-----------------------------------+----------------+------------+------------
ALB | Albania | Tirana | Albaniana | t | 97.9 | ALB | Albania | Tirana | Albaniana | t | 97.9
AUT | Austria | Wien | German | t | 92 | AUT | Austria | Wien | German | t | 92
BLR | Belarus | Minsk | Belorussian | t | 65.6 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BLR | Belarus | Minsk | Russian | t | 32 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | French | t | 32.6 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | German | t | 1 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2 | BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2
BGR | Bulgaria | Sofija | Bulgariana | t | 83.2 | BGR | Bulgaria | Sofija | Bulgariana | t | 83.2
HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9 | HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Praha | Czech | t | 81.2
DNK | Denmark | Kobenhavn | Danish | t | 93.5 | DNK | Denmark | Kobenhavn | Danish | t | 93.5
EST | Estonia | Tallinn | Estonian | t | 65.3 | EST | Estonia | Tallinn | Estonian | t | 65.3
FRO | Faroe Islands | Torshavn | Danish | t | 0 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FRO | Faroe Islands | Torshavn | Faroese | t | 100 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FIN | Finland | Helsinki [Helsingfors] | Swedish | t | 5.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FRA | France | Paris | French | t | 93.6 | FRA | France | Paris | French | t | 93.6
DEU | Germany | Berlin | German | t | 91.3 | DEU | Germany | Berlin | German | t | 91.3
GIB | Gibraltar | Gibraltar | English | t | 88.9 | GIB | Gibraltar | Gibraltar | English | t | 88.9
GRC | Greece | Athenai | Greek | t | 98.5 | GRC | Greece | Athenai | Greek | t | 98.5
HUN | Hungary | Budapest | Hungarian | t | 98.5 | HUN | Hungary | Budapest | Hungarian | t | 98.5
ISL | Iceland | Reykjavik | Icelandic | t | 95.7 | ISL | Iceland | Reykjavik | Icelandic | t | 95.7
IRL | Ireland | Dublin | English | t | 98.4 | IRL | Ireland | Dublin | English | t | 98.4
IRL | Ireland | Dublin | Irish | t | 1.6 | IRL | Ireland | Dublin | English | t | 98.4
ITA | Italy | Roma | Italian | t | 94.1 | ITA | Italy | Roma | Italian | t | 94.1
LVA | Latvia | Riga | Latvian | t | 55.1 | LVA | Latvia | Riga | Latvian | t | 55.1
LIE | Liechtenstein | Vaduz | German | t | 89 | LIE | Liechtenstein | Vaduz | German | t | 89
LTU | Lithuania | Vilnius | Lithuanian | t | 81.6 | LTU | Lithuania | Vilnius | Lithuanian | t | 81.6
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | French | t | 4.2 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | German | t | 2.3 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
MKD | Macedonia | Skopje | Macedonian | t | 66.5 | MKD | Macedonia | Skopje | Macedonian | t | 66.5
MLT | Malta | Valletta | English | t | 2.1 | MLT | Malta | Valletta | Maltese | t | 95.8
MLT | Malta | Valletta | Maltese | t | 95.8 | MLT | Malta | Valletta | Maltese | t | 95.8
MDA | Moldova | Chisinau | Romanian | t | 61.9 | MDA | Moldova | Chisinau | Romanian | t | 61.9
NLD | Netherlands | Amsterdam | Dutch | t | 95.6 | NLD | Netherlands | Amsterdam | Dutch | t | 95.6
NOR | Norway | Oslo | Norwegian | t | 96.6 | NOR | Norway | Oslo | Norwegian | t | 96.6
POL | Poland | Warszawa | Polish | t | 97.6 | POL | Poland | Warszawa | Polish | t | 97.6
PRT | Portugal | Lisboa | Portuguese | t | 99 | PRT | Portugal | Lisboa | Portuguese | t | 99
ROM | Romania | Bucuresti | Romani | t | 0.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
ROM | Romania | Bucuresti | Romanian | t | 90.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
RUS | Russian Federation | Moscow | Russian | t | 86.6 | RUS | Russian Federation | Moscow | Russian | t | 86.6
SMR | San Marino | San Marino | Italian | t | 100 | SMR | San Marino | San Marino | Italian | t | 100
SVK | Slovakia | Bratislava | Slovak | t | 85.6 | SVK | Slovakia | Bratislava | Slovak | t | 85.6
SVN | Slovenia | Ljubljana | Slovene | t | 87.9 | SVN | Slovenia | Ljubljana | Slovene | t | 87.9
ESP | Spain | Madrid | Spanish | t | 74.4 | ESP | Spain | Madrid | Spanish | t | 74.4
SWE | Sweden | Stockholm | Swedish | t | 89.5 | SWE | Sweden | Stockholm | Swedish | t | 89.5
CHE | Switzerland | Bern | French | t | 19.2 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | German | t | 63.6 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Italian | t | 7.7 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Romansh | t | 0.6 | CHE | Switzerland | Bern | German | t | 63.6
UKR | Ukraine | Kyiv | Ukrainian | t | 64.7 | UKR | Ukraine | Kyiv | Ukrainian | t | 64.7
GBR | United Kingdom | London | English | t | 97.3 | GBR | United Kingdom | London | English | t | 97.3
YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2 | YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2
(55 rows)
-- query8
with allcountrystats as
( select country.code,country.name,count(distinct city.id) CITY_CNT,
count(distinct countrylanguage.language) LANG_CNT
from country,city,countrylanguage
where country.code = city.countrycode
and country.code = countrylanguage.countrycode
group by country.code,country.name
)
select sum(FOO.CITY_CNT) REGION_CITY_CNT,sum(FOO.LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > 1
and country.continent = 'Asia'
UNION ALL
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > 1
and country.continent = 'North America'
UNION ALL
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > (select max(CITY_CNT/LANG_CNT) from allcountrystats,country where allcountrystats.code = country.code AND country.continent='Europe')
) FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
region_city_cnt | region_lang_cnt | region
-----------------+-----------------+---------------------------
840 | 192 | Caribbean
2824 | 112 | Central America
11336 | 384 | Eastern Asia
2664 | 396 | Middle East
1625 | 125 | North America
3500 | 70 | South America
3179 | 528 | Southeast Asia
12278 | 896 | Southern and Central Asia
(8 rows)
-- some queries with hash joins and index scans
set enable_nestloop=off;
set enable_hashjoin=on;
set enable_mergejoin=off;
--query 9
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select d1.code,d1.name,d1.capital,city.name CAPITAL_CITY,d1.CNT,d2.CNT
from
diversecountries d1 left join country
ON (d1.code = country.code AND d1.CNT < 8)
left join diversecountries d2
ON (country.code = d2.code AND d2.CNT > 8)
INNER JOIN city
ON(d1.capital = city.id)
ORDER BY d1.name;
code | name | capital | capital_city | cnt | cnt
------+--------------------+---------+------------------+-----+-----
AUS | Australia | 135 | Canberra | 8 |
AUT | Austria | 1523 | Wien | 8 |
CAN | Canada | 1822 | Ottawa | 12 |
CHN | China | 1891 | Peking | 12 |
DNK | Denmark | 3315 | Kobenhavn | 7 |
IND | India | 1109 | New Delhi | 12 |
IRN | Iran | 1380 | Teheran | 10 |
ITA | Italy | 1464 | Roma | 8 |
MMR | Myanmar | 2710 | Rangoon (Yangon) | 8 |
RUS | Russian Federation | 3580 | Moscow | 12 |
ZAF | South Africa | 716 | Pretoria | 11 |
USA | United States | 3813 | Washington | 12 |
(12 rows)
--query 10 , multiple ctes, joins
with longlivingregions as
(
select FOO.*,count(distinct language) as "lang_count"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
avg(lifeexpectancy) as "REGION_LIFETIME",region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP",foo."REGION_LIFETIME"),
denseregions as
(
select FOO.*,count(distinct language) as "lang_count",
sum(surfacearea) as "REGION_SURFACE_AREA"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
and FOO."REGION_POP" != 0
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP"
order by sum(surfacearea)/foo."REGION_POP" desc),
allcountrystats as
( select country.code,country.name,count(distinct city.id) CITY_CNT,
count(distinct countrylanguage.language) LANG_CNT
from country,city,countrylanguage
where country.code = city.countrycode
and country.code = countrylanguage.countrycode
group by country.code,country.name
)
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1800 and 1850
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1850 and 1900
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear > 1900
order by name
LIMIT 50;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
city_cnt | lang_cnt | name | REGION_SURFACE_AREA | REGION_LIFETIME | REGION_POP | lang_count | REGION_GNP | region
----------+----------+---------------------------------------+---------------------+------------------+------------+------------+------------+---------------------------
4 | 5 | Afghanistan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 3 | Albania | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
18 | 2 | Algeria | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
5 | 9 | Angola | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 2 | Antigua and Barbuda | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
57 | 3 | Argentina | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 2 | Armenia | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
14 | 8 | Australia | 62471162.00 | 78.8000030517578 | 22753100 | 11 | 405851.00 | Australia and New Zealand
6 | 8 | Austria | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
4 | 4 | Azerbaijan | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
1 | 2 | Bahamas | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
1 | 2 | Bahrain | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
24 | 7 | Bangladesh | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 2 | Barbados | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
16 | 4 | Belarus | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
9 | 6 | Belgium | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
2 | 4 | Belize | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
4 | 7 | Benin | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Bhutan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
8 | 4 | Bolivia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 1 | Bosnia and Herzegovina | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
2 | 5 | Botswana | 23060186.00 | 44.8199996948242 | 46886000 | 21 | 126931.00 | Southern Africa
250 | 5 | Brazil | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 4 | Brunei | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
10 | 4 | Bulgaria | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
3 | 6 | Burkina Faso | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Burundi | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
3 | 4 | Cambodia | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
7 | 8 | Cameroon | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
49 | 12 | Canada | 236342035.00 | 75.8199996948242 | 309632000 | 18 | 9111890.00 | North America
1 | 2 | Cape Verde | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 6 | Central African Republic | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
2 | 8 | Chad | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
29 | 4 | Chile | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
38 | 5 | Colombia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 5 | Comoros | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
2 | 6 | Congo | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
18 | 10 | Congo, The Democratic Republic of the | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 4 | Costa Rica | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
5 | 5 | Cote deIvoire | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
4 | 2 | Croatia | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
14 | 1 | Cuba | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
2 | 2 | Cyprus | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
10 | 8 | Czech Republic | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
1 | 3 | Djibouti | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
1 | 2 | Dominica | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
6 | 2 | Dominican Republic | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
15 | 2 | Ecuador | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
37 | 2 | Egypt | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
7 | 2 | El Salvador | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
(50 rows)
--query 11
with allcitystats as
( select city.name CITY,city.id,country.name COUNTRY,city.district,city.population as CITY_POP
from
city,country
where city.countrycode = country.code
),
alldistrictstats as
( select allcitystats.district,allcitystats.COUNTRY,sum(CITY_POP) DISTRICT_POP,
count(CITY) as D_CITY_CNT
from allcitystats
group by allcitystats.district,allcitystats.COUNTRY
order by district,COUNTRY
),
allcountrystats as
( select alldistrictstats.COUNTRY,country.code,sum(D_CITY_CNT) C_CITY_CNT,
count(distinct countrylanguage.language) C_LANG_CNT
from alldistrictstats,country,countrylanguage
where alldistrictstats.COUNTRY = country.name
and country.code = countrylanguage.countrycode
group by COUNTRY,code
),
asian_region_stats as
(
select sum(FOO.C_CITY_CNT) REGION_CITY_CNT,sum(FOO.C_LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountrystats.code,allcountrystats.COUNTRY,C_CITY_CNT,C_LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and C_CITY_CNT/C_LANG_CNT > 1
and country.continent = 'Asia') FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region
)
select * from
(
select REGION_CITY_CNT as CITY_CNT,REGION_LANG_CNT as LANG_CNT, region as IDENTIFIER from asian_region_stats
UNION ALL
(
select sum(FOO.C_CITY_CNT) CITY_CNT,sum(FOO.C_LANG_CNT) LANG_CNT,FOO.region as IDENTIFIER
FROM
(
select allcountrystats.code,allcountrystats.COUNTRY,C_CITY_CNT,C_LANG_CNT,country.region,allcitystats.CITY CAPITAL
from allcountrystats,country,allcitystats
where allcountrystats.code = country.code
and country.capital = allcitystats.id
and C_CITY_CNT/C_LANG_CNT > 1
and country.continent = 'Europe') FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region
)
) FOO1
order by FOO1.lang_cnt,FOO1.identifier;
city_cnt | lang_cnt | identifier
----------+----------+---------------------------
494 | 10 | British Islands
159 | 48 | Baltic Countries
1295 | 161 | Nordic Countries
50176 | 264 | Eastern Asia
9414 | 369 | Western Europe
11880 | 450 | Southern Europe
32900 | 610 | Eastern Europe
29161 | 616 | Southeast Asia
8568 | 792 | Middle East
80388 | 1092 | Southern and Central Asia
(10 rows)
-- queries with CTEs using index scans on bitmap indexes
\echo --start_ignore
--start_ignore
Drop index bitmap_city_countrycode;
ERROR: index "bitmap_city_countrycode" does not exist
Drop index bitmap_country_gf;
ERROR: index "bitmap_country_gf" does not exist
Drop index bitmap_country_region;
ERROR: index "bitmap_country_region" does not exist
Drop index bitmap_country_continent;
ERROR: index "bitmap_country_continent" does not exist
Drop index bitmap_countrylanguage_countrycode;
ERROR: index "bitmap_countrylanguage_countrycode" does not exist
\echo --end_ignore
--end_ignore
create index bitmap_city_countrycode on city using bitmap(countrycode);
create index bitmap_country_gf on country using bitmap(governmentform);
create index bitmap_country_region on country using bitmap(region);
create index bitmap_country_continent on country using bitmap(continent);
create index bitmap_countrylanguage_countrycode on countrylanguage using bitmap(countrycode);
set enable_seqscan=off;
set enable_indexscan=on;
--query1
with capitals as
(select country.code,id,city.name from city,country
where city.countrycode = country.code AND city.id = country.capital)
select * from
capitals,countrylanguage
where capitals.code = countrylanguage.countrycode and isofficial='true'
order by capitals.code,countrylanguage.language;
code | id | name | countrycode | language | isofficial | percentage
------+------+-----------------------------------+-------------+------------------+------------+------------
ABW | 129 | Oranjestad | ABW | Dutch | t | 5.3
AFG | 1 | Kabul | AFG | Dari | t | 32.1
AFG | 1 | Kabul | AFG | Pashto | t | 52.4
AIA | 62 | The Valley | AIA | English | t | 0
ALB | 34 | Tirana | ALB | Albaniana | t | 97.9
AND | 55 | Andorra la Vella | AND | Catalan | t | 32.3
ANT | 33 | Willemstad | ANT | Dutch | t | 0
ANT | 33 | Willemstad | ANT | Papiamento | t | 86.2
ARE | 65 | Abu Dhabi | ARE | Arabic | t | 42
ARG | 69 | Buenos Aires | ARG | Spanish | t | 96.8
ARM | 126 | Yerevan | ARM | Armenian | t | 93.4
ASM | 54 | Fagatogo | ASM | English | t | 3.1
ASM | 54 | Fagatogo | ASM | Samoan | t | 90.6
ATG | 63 | Saint Johns | ATG | English | t | 0
AUS | 135 | Canberra | AUS | English | t | 81.2
AUT | 1523 | Wien | AUT | German | t | 92
AZE | 144 | Baku | AZE | Azerbaijani | t | 89
BDI | 552 | Bujumbura | BDI | French | t | 0
BDI | 552 | Bujumbura | BDI | Kirundi | t | 98.1
BEL | 179 | Bruxelles [Brussel] | BEL | Dutch | t | 59.2
BEL | 179 | Bruxelles [Brussel] | BEL | French | t | 32.6
BEL | 179 | Bruxelles [Brussel] | BEL | German | t | 1
BGD | 150 | Dhaka | BGD | Bengali | t | 97.7
BGR | 539 | Sofija | BGR | Bulgariana | t | 83.2
BHR | 149 | al-Manama | BHR | Arabic | t | 67.7
BIH | 201 | Sarajevo | BIH | Serbo-Croatian | t | 99.2
BLR | 3520 | Minsk | BLR | Belorussian | t | 65.6
BLR | 3520 | Minsk | BLR | Russian | t | 32
BLZ | 185 | Belmopan | BLZ | English | t | 50.8
BMU | 191 | Hamilton | BMU | English | t | 100
BOL | 194 | La Paz | BOL | Aimara | t | 3.2
BOL | 194 | La Paz | BOL | Ketdua | t | 8.1
BOL | 194 | La Paz | BOL | Spanish | t | 87.7
BRA | 211 | Brasilia | BRA | Portuguese | t | 97.5
BRB | 174 | Bridgetown | BRB | English | t | 0
BRN | 538 | Bandar Seri Begawan | BRN | Malay | t | 45.5
BTN | 192 | Thimphu | BTN | Dzongkha | t | 50
CAN | 1822 | Ottawa | CAN | English | t | 60.4
CAN | 1822 | Ottawa | CAN | French | t | 23.4
CCK | 2317 | West Island | CCK | English | t | 0
CHE | 3248 | Bern | CHE | French | t | 19.2
CHE | 3248 | Bern | CHE | German | t | 63.6
CHE | 3248 | Bern | CHE | Italian | t | 7.7
CHE | 3248 | Bern | CHE | Romansh | t | 0.6
CHL | 554 | Santiago de Chile | CHL | Spanish | t | 89.7
CHN | 1891 | Peking | CHN | Chinese | t | 92
COK | 583 | Avarua | COK | Maori | t | 0
COL | 2257 | Santafe de Bogota | COL | Spanish | t | 99
COM | 2295 | Moroni | COM | Comorian | t | 75
CPV | 1859 | Praia | CPV | Portuguese | t | 0
CRI | 584 | San Jose | CRI | Spanish | t | 97.5
CUB | 2413 | La Habana | CUB | Spanish | t | 100
CXR | 1791 | Flying Fish Cove | CXR | English | t | 0
CYM | 553 | George Town | CYM | English | t | 0
CYP | 2430 | Nicosia | CYP | Greek | t | 74.1
CYP | 2430 | Nicosia | CYP | Turkish | t | 22.4
CZE | 3339 | Praha | CZE | Czech | t | 81.2
DEU | 3068 | Berlin | DEU | German | t | 91.3
DJI | 585 | Djibouti | DJI | Arabic | t | 10.6
DNK | 3315 | Kobenhavn | DNK | Danish | t | 93.5
DOM | 587 | Santo Domingo de Guzman | DOM | Spanish | t | 98
DZA | 35 | Alger | DZA | Arabic | t | 86
ECU | 594 | Quito | ECU | Spanish | t | 93
EGY | 608 | Cairo | EGY | Arabic | t | 98.8
ERI | 652 | Asmara | ERI | Tigrinja | t | 49.1
ESH | 2453 | El-Aaiun | ESH | Arabic | t | 100
ESP | 653 | Madrid | ESP | Spanish | t | 74.4
EST | 3791 | Tallinn | EST | Estonian | t | 65.3
FIN | 3236 | Helsinki [Helsingfors] | FIN | Finnish | t | 92.7
FIN | 3236 | Helsinki [Helsingfors] | FIN | Swedish | t | 5.7
FJI | 764 | Suva | FJI | Fijian | t | 50.8
FLK | 763 | Stanley | FLK | English | t | 0
FRA | 2974 | Paris | FRA | French | t | 93.6
FRO | 901 | Torshavn | FRO | Danish | t | 0
FRO | 901 | Torshavn | FRO | Faroese | t | 100
GBR | 456 | London | GBR | English | t | 97.3
GEO | 905 | Tbilisi | GEO | Georgiana | t | 71.7
GIB | 915 | Gibraltar | GIB | English | t | 88.9
GLP | 919 | Basse-Terre | GLP | French | t | 0
GNB | 927 | Bissau | GNB | Portuguese | t | 8.1
GRC | 2401 | Athenai | GRC | Greek | t | 98.5
GRL | 917 | Nuuk | GRL | Danish | t | 12.5
GRL | 917 | Nuuk | GRL | Greenlandic | t | 87.5
GTM | 922 | Ciudad de Guatemala | GTM | Spanish | t | 64.7
GUM | 921 | Agaaa | GUM | Chamorro | t | 29.6
GUM | 921 | Agaaa | GUM | English | t | 37.5
HKG | 937 | Victoria | HKG | English | t | 2.2
HND | 933 | Tegucigalpa | HND | Spanish | t | 97.2
HRV | 2409 | Zagreb | HRV | Serbo-Croatian | t | 95.9
HTI | 929 | Port-au-Prince | HTI | French | t | 0
HUN | 3483 | Budapest | HUN | Hungarian | t | 98.5
IDN | 939 | Jakarta | IDN | Malay | t | 12.1
IND | 1109 | New Delhi | IND | Hindi | t | 39.9
IRL | 1447 | Dublin | IRL | English | t | 98.4
IRL | 1447 | Dublin | IRL | Irish | t | 1.6
IRN | 1380 | Teheran | IRN | Persian | t | 45.7
IRQ | 1365 | Baghdad | IRQ | Arabic | t | 77.2
ISL | 1449 | Reykjavik | ISL | Icelandic | t | 95.7
ISR | 1450 | Jerusalem | ISR | Arabic | t | 18
ISR | 1450 | Jerusalem | ISR | Hebrew | t | 63.1
ITA | 1464 | Roma | ITA | Italian | t | 94.1
JOR | 1786 | Amman | JOR | Arabic | t | 97.9
JPN | 1532 | Tokyo | JPN | Japanese | t | 99.1
KAZ | 1864 | Astana | KAZ | Kazakh | t | 46
KGZ | 2253 | Bishkek | KGZ | Kirgiz | t | 59.7
KGZ | 2253 | Bishkek | KGZ | Russian | t | 16.2
KHM | 1800 | Phnom Penh | KHM | Khmer | t | 88.6
KIR | 2256 | Bairiki | KIR | Kiribati | t | 98.9
KNA | 3064 | Basseterre | KNA | English | t | 0
KOR | 2331 | Seoul | KOR | Korean | t | 99.9
KWT | 2429 | Kuwait | KWT | Arabic | t | 78.1
LAO | 2432 | Vientiane | LAO | Lao | t | 67.2
LBN | 2438 | Beirut | LBN | Arabic | t | 93
LBY | 2441 | Tripoli | LBY | Arabic | t | 96
LCA | 3065 | Castries | LCA | English | t | 20
LIE | 2446 | Vaduz | LIE | German | t | 89
LKA | 3217 | Colombo | LKA | Singali | t | 60.3
LKA | 3217 | Colombo | LKA | Tamil | t | 19.6
LSO | 2437 | Maseru | LSO | English | t | 0
LSO | 2437 | Maseru | LSO | Sotho | t | 85
LTU | 2447 | Vilnius | LTU | Lithuanian | t | 81.6
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | French | t | 4.2
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | German | t | 2.3
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | Luxembourgish | t | 64.4
LVA | 2434 | Riga | LVA | Latvian | t | 55.1
MAC | 2454 | Macao | MAC | Portuguese | t | 2.3
MAR | 2486 | Rabat | MAR | Arabic | t | 65
MCO | 2695 | Monaco-Ville | MCO | French | t | 41.9
MDA | 2690 | Chisinau | MDA | Romanian | t | 61.9
MDG | 2455 | Antananarivo | MDG | French | t | 0
MDG | 2455 | Antananarivo | MDG | Malagasy | t | 98.9
MDV | 2463 | Male | MDV | Dhivehi | t | 100
MEX | 2515 | Ciudad de Mexico | MEX | Spanish | t | 92.1
MHL | 2507 | Dalap-Uliga-Darrit | MHL | English | t | 0
MHL | 2507 | Dalap-Uliga-Darrit | MHL | Marshallese | t | 96.8
MKD | 2460 | Skopje | MKD | Macedonian | t | 66.5
MLT | 2484 | Valletta | MLT | English | t | 2.1
MLT | 2484 | Valletta | MLT | Maltese | t | 95.8
MMR | 2710 | Rangoon (Yangon) | MMR | Burmese | t | 69
MNG | 2696 | Ulan Bator | MNG | Mongolian | t | 78.8
MNP | 2913 | Garapan | MNP | English | t | 4.8
MSR | 2697 | Plymouth | MSR | English | t | 0
MTQ | 2508 | Fort-de-France | MTQ | French | t | 0
MWI | 2462 | Lilongwe | MWI | Chichewa | t | 58.3
MYS | 2464 | Kuala Lumpur | MYS | Malay | t | 58.4
MYT | 2514 | Mamoutzou | MYT | French | t | 20.3
NCL | 3493 | Noumea | NCL | French | t | 34.3
NFK | 2806 | Kingston | NFK | English | t | 0
NIC | 2734 | Managua | NIC | Spanish | t | 97.6
NIU | 2805 | Alofi | NIU | English | t | 0
NLD | 5 | Amsterdam | NLD | Dutch | t | 95.6
NOR | 2807 | Oslo | NOR | Norwegian | t | 96.6
NPL | 2729 | Kathmandu | NPL | Nepali | t | 50.4
NRU | 2728 | Yaren | NRU | English | t | 7.5
NRU | 2728 | Yaren | NRU | Nauru | t | 57.5
NZL | 3499 | Wellington | NZL | English | t | 87
OMN | 2821 | Masqat | OMN | Arabic | t | 76.7
PAK | 2831 | Islamabad | PAK | Urdu | t | 7.6
PAN | 2882 | Ciudad de Panama | PAN | Spanish | t | 76.8
PER | 2890 | Lima | PER | Aimara | t | 2.3
PER | 2890 | Lima | PER | Ketdua | t | 16.4
PER | 2890 | Lima | PER | Spanish | t | 79.8
PHL | 766 | Manila | PHL | Pilipino | t | 29.3
PLW | 2881 | Koror | PLW | English | t | 3.2
PLW | 2881 | Koror | PLW | Palau | t | 82.2
POL | 2928 | Warszawa | POL | Polish | t | 97.6
PRI | 2919 | San Juan | PRI | Spanish | t | 51.3
PRK | 2318 | Pyongyang | PRK | Korean | t | 99.9
PRT | 2914 | Lisboa | PRT | Portuguese | t | 99
PRY | 2885 | Asuncion | PRY | Guarani | t | 40.1
PRY | 2885 | Asuncion | PRY | Spanish | t | 55.1
PYF | 3016 | Papeete | PYF | French | t | 40.8
QAT | 2973 | Doha | QAT | Arabic | t | 40.7
ROM | 3018 | Bucuresti | ROM | Romani | t | 0.7
ROM | 3018 | Bucuresti | ROM | Romanian | t | 90.7
RUS | 3580 | Moscow | RUS | Russian | t | 86.6
RWA | 3047 | Kigali | RWA | French | t | 0
RWA | 3047 | Kigali | RWA | Rwanda | t | 100
SAU | 3173 | Riyadh | SAU | Arabic | t | 95
SDN | 3225 | Khartum | SDN | Arabic | t | 49.4
SEN | 3198 | Dakar | SEN | Wolof | t | 48.1
SGP | 3208 | Singapore | SGP | Chinese | t | 77.1
SGP | 3208 | Singapore | SGP | Malay | t | 14.1
SGP | 3208 | Singapore | SGP | Tamil | t | 7.4
SHN | 3063 | Jamestown | SHN | English | t | 0
SJM | 938 | Longyearbyen | SJM | Norwegian | t | 0
SLV | 645 | San Salvador | SLV | Spanish | t | 100
SMR | 3171 | San Marino | SMR | Italian | t | 100
SOM | 3214 | Mogadishu | SOM | Arabic | t | 0
SOM | 3214 | Mogadishu | SOM | Somali | t | 98.3
SPM | 3067 | Saint-Pierre | SPM | French | t | 0
SVK | 3209 | Bratislava | SVK | Slovak | t | 85.6
SVN | 3212 | Ljubljana | SVN | Slovene | t | 87.9
SWE | 3048 | Stockholm | SWE | Swedish | t | 89.5
SWZ | 3244 | Mbabane | SWZ | Swazi | t | 89.9
SYC | 3206 | Victoria | SYC | English | t | 3.8
SYC | 3206 | Victoria | SYC | French | t | 1.3
SYR | 3250 | Damascus | SYR | Arabic | t | 90
TCA | 3423 | Cockburn Town | TCA | English | t | 0
TCD | 3337 | NDjamena | TCD | Arabic | t | 12.3
TGO | 3332 | Lome | TGO | Ewe | t | 23.2
TGO | 3332 | Lome | TGO | Kabye | t | 13.8
THA | 3320 | Bangkok | THA | Thai | t | 52.6
TJK | 3261 | Dushanbe | TJK | Tadzhik | t | 62.2
TKL | 3333 | Fakaofo | TKL | English | t | 0
TKM | 3419 | Ashgabat | TKM | Turkmenian | t | 76.7
TMP | 1522 | Dili | TMP | Portuguese | t | 0
TON | 3334 | Nukualofa | TON | English | t | 0
TON | 3334 | Nukualofa | TON | Tongan | t | 98.3
TUN | 3349 | Tunis | TUN | Arabic | t | 69.9
TUR | 3358 | Ankara | TUR | Turkish | t | 87.6
TUV | 3424 | Funafuti | TUV | English | t | 0
TUV | 3424 | Funafuti | TUV | Tuvalu | t | 92.5
TWN | 3263 | Taipei | TWN | Mandarin Chinese | t | 20.1
TZA | 3306 | Dodoma | TZA | Swahili | t | 8.8
UKR | 3426 | Kyiv | UKR | Ukrainian | t | 64.7
URY | 3492 | Montevideo | URY | Spanish | t | 95.7
USA | 3813 | Washington | USA | English | t | 86.2
UZB | 3503 | Toskent | UZB | Uzbek | t | 72.6
VAT | 3538 | Citta del Vaticano | VAT | Italian | t | 0
VCT | 3066 | Kingstown | VCT | English | t | 0
VEN | 3539 | Caracas | VEN | Spanish | t | 96.9
VGB | 537 | Road Town | VGB | English | t | 0
VIR | 4067 | Charlotte Amalie | VIR | English | t | 81.7
VNM | 3770 | Hanoi | VNM | Vietnamese | t | 86.8
VUT | 3537 | Port-Vila | VUT | Bislama | t | 56.6
VUT | 3537 | Port-Vila | VUT | English | t | 28.3
VUT | 3537 | Port-Vila | VUT | French | t | 14.2
WSM | 3169 | Apia | WSM | English | t | 0.6
WSM | 3169 | Apia | WSM | Samoan | t | 47.5
YEM | 1780 | Sanaa | YEM | Arabic | t | 99.6
YUG | 1792 | Beograd | YUG | Serbo-Croatian | t | 75.2
ZAF | 716 | Pretoria | ZAF | Afrikaans | t | 14.3
ZAF | 716 | Pretoria | ZAF | English | t | 8.5
ZAF | 716 | Pretoria | ZAF | Xhosa | t | 17.7
ZAF | 716 | Pretoria | ZAF | Zulu | t | 22.7
ZWE | 4068 | Harare | ZWE | English | t | 2.2
(237 rows)
--query2
with lang_total as
( select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
select lang_count,country.code,country.name,country.continent,country.region,country.population
from country left outer join lang_total
on (lang_total.code = country.code)
where country.indepyear > 1970 and lang_total.lang_count >=1 and lang_total.lang_count >=1
order by lang_total.lang_count desc;
lang_count | code | name | continent | region | population
------------+------+---------------------------------+-----------+-----------------+------------
12 | RUS | Russian Federation | Europe | Eastern Europe | 146934000
6 | FSM | Micronesia, Federated States of | Oceania | Micronesia | 119000
4 | AZE | Azerbaijan | Asia | Middle East | 7734000
1 | BIH | Bosnia and Herzegovina | Europe | Southern Europe | 3972000
(4 rows)
-- Using CTE in the WHERE clause
--query3
with lang_total as
( select lang_count,countrycode from
(
(
select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
UNION ALL
(
select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Monarchy')
group by country.code,countrylanguage.countrycode order by country.code)
) FOO1
)
select * from
(
select count(*) as cnt,country.code,country.name
from
country,countrylanguage
where country.code=countrylanguage.countrycode group by country.code,country.name) AS FOO
where foo.cnt = (select max(lang_count) from lang_total) order by foo.code;
cnt | code | name
-----+------+--------------------
12 | CAN | Canada
12 | CHN | China
12 | IND | India
12 | RUS | Russian Federation
12 | USA | United States
(5 rows)
--query4
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select diversecountries.name,city.name,diversecountries.CNT
from diversecountries,city where city.id = diversecountries.capital
order by diversecountries.name;
name | name | cnt
--------------------+------------------+-----
Australia | Canberra | 8
Austria | Wien | 8
Canada | Ottawa | 12
China | Peking | 12
Denmark | Kobenhavn | 7
India | New Delhi | 12
Iran | Teheran | 10
Italy | Roma | 8
Myanmar | Rangoon (Yangon) | 8
Russian Federation | Moscow | 12
South Africa | Pretoria | 11
United States | Washington | 12
(12 rows)
-- some queries with merge joins and index scans
set enable_nestloop=off;
set enable_hashjoin=off;
set enable_mergejoin=on;
-- query 5
with somecheapasiandiversecountries as
(
select FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,count(*) ASIAN_COUNT from
(
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Southeast Asia'
and country.continent = 'Asia'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Eastern Asia'
and country.continent = 'Asia'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Middle East'
and country.continent = 'Asia'
) FOO, countrylanguage
where FOO.code = countrylanguage.countrycode
group by FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,countrylanguage.countrycode
HAVING count(*) >=
(select min(CNT) FROM
(select count(*) CNT,country.code from countrylanguage,country
where countrylanguage.countrycode=country.code
and country.continent = 'Asia'
and country.region = 'Southern and Central Asia'
group by country.code
) FOO1
)
)
select FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,count(*) COMPARED_WITH_CHEAP_ASIAN_CNT
from
(
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.continent = 'North America'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.continent = 'South America'
) FOO,countrylanguage
where FOO.code = countrylanguage.countrycode
group by FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate
HAVING count(*) >=
(select min(ASIAN_COUNT) FROM
(select ASIAN_COUNT FROM somecheapasiandiversecountries,country
where somecheapasiandiversecountries.code = country.code
and country.gnp >= country.gnpold
) ASIANCOUNT
)
order by COUNTRY;
code | country | capital | headofstate | compared_with_cheap_asian_cnt
------+----------------------------------+-------------------------+-----------------------------------+-------------------------------
ATG | Antigua and Barbuda | Saint Johns | Elisabeth II | 2
ARG | Argentina | Buenos Aires | Fernando de la Rua | 3
ABW | Aruba | Oranjestad | Beatrix | 4
BHS | Bahamas | Nassau | Elisabeth II | 2
BRB | Barbados | Bridgetown | Elisabeth II | 2
BLZ | Belize | Belmopan | Elisabeth II | 4
BOL | Bolivia | La Paz | Hugo Banzer Suarez | 4
BRA | Brazil | Brasilia | Fernando Henrique Cardoso | 5
CAN | Canada | Ottawa | Elisabeth II | 12
CHL | Chile | Santiago de Chile | Ricardo Lagos Escobar | 4
COL | Colombia | Santafe de Bogota | Andres Pastrana Arango | 5
CRI | Costa Rica | San Jose | Miguel Angel Rodriguez Echeverria | 4
DMA | Dominica | Roseau | Vernon Shaw | 2
DOM | Dominican Republic | Santo Domingo de Guzman | Hipolito Mejia Dominguez | 2
ECU | Ecuador | Quito | Gustavo Noboa Bejarano | 2
SLV | El Salvador | San Salvador | Francisco Guillermo Flores Perez | 2
GUF | French Guiana | Cayenne | Jacques Chirac | 2
GRL | Greenland | Nuuk | Margrethe II | 2
GLP | Guadeloupe | Basse-Terre | Jacques Chirac | 2
GTM | Guatemala | Ciudad de Guatemala | Alfonso Portillo Cabrera | 5
GUY | Guyana | Georgetown | Bharrat Jagdeo | 3
HTI | Haiti | Port-au-Prince | Jean-Bertrand Aristide | 2
HND | Honduras | Tegucigalpa | Carlos Roberto Flores Facusse | 4
JAM | Jamaica | Kingston | Elisabeth II | 2
MTQ | Martinique | Fort-de-France | Jacques Chirac | 2
MEX | Mexico | Ciudad de Mexico | Vicente Fox Quesada | 6
ANT | Netherlands Antilles | Willemstad | Beatrix | 3
NIC | Nicaragua | Managua | Arnoldo Aleman Lacayo | 4
PAN | Panama | Ciudad de Panama | Mireya Elisa Moscoso Rodriguez | 6
PRY | Paraguay | Asuncion | Luis Angel Gonzalez Macchi | 4
PER | Peru | Lima | Valentin Paniagua Corazao | 3
PRI | Puerto Rico | San Juan | George W. Bush | 2
KNA | Saint Kitts and Nevis | Basseterre | Elisabeth II | 2
LCA | Saint Lucia | Castries | Elisabeth II | 2
VCT | Saint Vincent and the Grenadines | Kingstown | Elisabeth II | 2
SUR | Suriname | Paramaribo | Ronald Venetiaan | 2
TTO | Trinidad and Tobago | Port-of-Spain | Arthur N. R. Robinson | 3
USA | United States | Washington | George W. Bush | 12
VEN | Venezuela | Caracas | Hugo Chavez Frias | 3
VIR | Virgin Islands, U.S. | Charlotte Amalie | George W. Bush | 3
(40 rows)
-- query 6
select count(*) from
( select r.* from
( with fact as
(
select country.name as COUNTRY,country.code,city.name as CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,AGG1.region
from
(select
sum(case when (city.population >= 0.5 * country.population) then country.population else city.population end) as S_POPULATION,
sum(case when (gnp >= gnpold) then gnp else gnpold end) as S_GNP,
avg(case when (lifeexpectancy > 60) then 50 else lifeexpectancy end) as AVG_LIFE,country.region
from country,city
where governmentform != 'Constitutional Monarchy'
and country.capital = city.id
and indepyear > 0
group by country.region) AGG1
,country,city
where country.capital = city.id
and country.region = AGG1.region
)
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'South America'
UNION ALL
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'North America'
UNION ALL
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'Caribbean'
) as r
left join
(
select 'ARG' as CODE UNION ALL
select 'BOL' as CODE UNION ALL
select 'BRA' as CODE UNION ALL
select 'PER' as CODE UNION ALL
select 'URY' as CODE UNION ALL
select 'IND' as CODE UNION ALL
select 'LCA' as CODE UNION ALL
select 'VCT' as CODE
) as r1
on r.code = r1.code) AS FOO;
count
-------
43
(1 row)
-- query8
with allcountrystats as
( select country.code,country.name,count(distinct city.id) CITY_CNT,
count(distinct countrylanguage.language) LANG_CNT
from country,city,countrylanguage
where country.code = city.countrycode
and country.code = countrylanguage.countrycode
group by country.code,country.name
)
select sum(FOO.CITY_CNT) REGION_CITY_CNT,sum(FOO.LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > 1
and country.continent = 'Asia'
UNION ALL
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > 1
and country.continent = 'North America'
UNION ALL
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > (select max(CITY_CNT/LANG_CNT) from allcountrystats,country where allcountrystats.code = country.code AND country.continent='Europe')
) FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
region_city_cnt | region_lang_cnt | region
-----------------+-----------------+---------------------------
840 | 192 | Caribbean
2824 | 112 | Central America
11336 | 384 | Eastern Asia
2664 | 396 | Middle East
1625 | 125 | North America
3500 | 70 | South America
3179 | 528 | Southeast Asia
12278 | 896 | Southern and Central Asia
(8 rows)
-- some queries with hash joins and index scans
set enable_nestloop=off;
set enable_hashjoin=on;
set enable_mergejoin=off;
--query 9
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select d1.code,d1.name,d1.capital,city.name CAPITAL_CITY,d1.CNT,d2.CNT
from
diversecountries d1 left join country
ON (d1.code = country.code AND d1.CNT < 8)
left join diversecountries d2
ON (country.code = d2.code AND d2.CNT > 8)
INNER JOIN city
ON(d1.capital = city.id)
ORDER BY d1.name;
code | name | capital | capital_city | cnt | cnt
------+--------------------+---------+------------------+-----+-----
AUS | Australia | 135 | Canberra | 8 |
AUT | Austria | 1523 | Wien | 8 |
CAN | Canada | 1822 | Ottawa | 12 |
CHN | China | 1891 | Peking | 12 |
DNK | Denmark | 3315 | Kobenhavn | 7 |
IND | India | 1109 | New Delhi | 12 |
IRN | Iran | 1380 | Teheran | 10 |
ITA | Italy | 1464 | Roma | 8 |
MMR | Myanmar | 2710 | Rangoon (Yangon) | 8 |
RUS | Russian Federation | 3580 | Moscow | 12 |
ZAF | South Africa | 716 | Pretoria | 11 |
USA | United States | 3813 | Washington | 12 |
(12 rows)
--query 10 , multiple ctes, joins
with longlivingregions as
(
select FOO.*,count(distinct language) as "lang_count"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
avg(lifeexpectancy) as "REGION_LIFETIME",region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP",foo."REGION_LIFETIME"),
denseregions as
(
select FOO.*,count(distinct language) as "lang_count",
sum(surfacearea) as "REGION_SURFACE_AREA"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
and FOO."REGION_POP" != 0
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP"
order by sum(surfacearea)/foo."REGION_POP" desc),
allcountrystats as
( select country.code,country.name,count(distinct city.id) CITY_CNT,
count(distinct countrylanguage.language) LANG_CNT
from country,city,countrylanguage
where country.code = city.countrycode
and country.code = countrylanguage.countrycode
group by country.code,country.name
)
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1800 and 1850
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1850 and 1900
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear > 1900
order by name
LIMIT 50;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
city_cnt | lang_cnt | name | REGION_SURFACE_AREA | REGION_LIFETIME | REGION_POP | lang_count | REGION_GNP | region
----------+----------+---------------------------------------+---------------------+------------------+------------+------------+------------+---------------------------
4 | 5 | Afghanistan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 3 | Albania | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
18 | 2 | Algeria | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
5 | 9 | Angola | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 2 | Antigua and Barbuda | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
57 | 3 | Argentina | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 2 | Armenia | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
14 | 8 | Australia | 62471162.00 | 78.8000030517578 | 22753100 | 11 | 405851.00 | Australia and New Zealand
6 | 8 | Austria | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
4 | 4 | Azerbaijan | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
1 | 2 | Bahamas | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
1 | 2 | Bahrain | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
24 | 7 | Bangladesh | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 2 | Barbados | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
16 | 4 | Belarus | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
9 | 6 | Belgium | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
2 | 4 | Belize | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
4 | 7 | Benin | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Bhutan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
8 | 4 | Bolivia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 1 | Bosnia and Herzegovina | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
2 | 5 | Botswana | 23060186.00 | 44.8199996948242 | 46886000 | 21 | 126931.00 | Southern Africa
250 | 5 | Brazil | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 4 | Brunei | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
10 | 4 | Bulgaria | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
3 | 6 | Burkina Faso | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Burundi | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
3 | 4 | Cambodia | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
7 | 8 | Cameroon | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
49 | 12 | Canada | 236342035.00 | 75.8199996948242 | 309632000 | 18 | 9111890.00 | North America
1 | 2 | Cape Verde | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 6 | Central African Republic | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
2 | 8 | Chad | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
29 | 4 | Chile | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
38 | 5 | Colombia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 5 | Comoros | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
2 | 6 | Congo | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
18 | 10 | Congo, The Democratic Republic of the | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 4 | Costa Rica | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
5 | 5 | Cote deIvoire | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
4 | 2 | Croatia | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
14 | 1 | Cuba | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
2 | 2 | Cyprus | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
10 | 8 | Czech Republic | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
1 | 3 | Djibouti | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
1 | 2 | Dominica | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
6 | 2 | Dominican Republic | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
15 | 2 | Ecuador | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
37 | 2 | Egypt | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
7 | 2 | El Salvador | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
(50 rows)
--query 11
with allcitystats as
( select city.name CITY,city.id,country.name COUNTRY,city.district,city.population as CITY_POP
from
city,country
where city.countrycode = country.code
),
alldistrictstats as
( select allcitystats.district,allcitystats.COUNTRY,sum(CITY_POP) DISTRICT_POP,
count(CITY) as D_CITY_CNT
from allcitystats
group by allcitystats.district,allcitystats.COUNTRY
order by district,COUNTRY
),
allcountrystats as
( select alldistrictstats.COUNTRY,country.code,sum(D_CITY_CNT) C_CITY_CNT,
count(distinct countrylanguage.language) C_LANG_CNT
from alldistrictstats,country,countrylanguage
where alldistrictstats.COUNTRY = country.name
and country.code = countrylanguage.countrycode
group by COUNTRY,code
),
asian_region_stats as
(
select sum(FOO.C_CITY_CNT) REGION_CITY_CNT,sum(FOO.C_LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountrystats.code,allcountrystats.COUNTRY,C_CITY_CNT,C_LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and C_CITY_CNT/C_LANG_CNT > 1
and country.continent = 'Asia') FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region
)
select * from
(
select REGION_CITY_CNT as CITY_CNT,REGION_LANG_CNT as LANG_CNT, region as IDENTIFIER from asian_region_stats
UNION ALL
(
select sum(FOO.C_CITY_CNT) CITY_CNT,sum(FOO.C_LANG_CNT) LANG_CNT,FOO.region as IDENTIFIER
FROM
(
select allcountrystats.code,allcountrystats.COUNTRY,C_CITY_CNT,C_LANG_CNT,country.region,allcitystats.CITY CAPITAL
from allcountrystats,country,allcitystats
where allcountrystats.code = country.code
and country.capital = allcitystats.id
and C_CITY_CNT/C_LANG_CNT > 1
and country.continent = 'Europe') FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region
)
) FOO1
order by FOO1.lang_cnt,FOO1.identifier;
city_cnt | lang_cnt | identifier
----------+----------+---------------------------
494 | 10 | British Islands
159 | 48 | Baltic Countries
1295 | 161 | Nordic Countries
50176 | 264 | Eastern Asia
9414 | 369 | Western Europe
11880 | 450 | Southern Europe
32900 | 610 | Eastern Europe
29161 | 616 | Southeast Asia
8568 | 792 | Middle East
80388 | 1092 | Southern and Central Asia
(10 rows)
-- queries with CTEs using hash aggs
set enable_groupagg=off;
set enable_hashagg=on;
--query1
with capitals as
(select country.code,id,city.name from city,country
where city.countrycode = country.code AND city.id = country.capital)
select * from
capitals,countrylanguage
where capitals.code = countrylanguage.countrycode and isofficial='true'
order by capitals.code,countrylanguage.language;
code | id | name | countrycode | language | isofficial | percentage
------+------+-----------------------------------+-------------+------------------+------------+------------
ABW | 129 | Oranjestad | ABW | Dutch | t | 5.3
AFG | 1 | Kabul | AFG | Dari | t | 32.1
AFG | 1 | Kabul | AFG | Pashto | t | 52.4
AIA | 62 | The Valley | AIA | English | t | 0
ALB | 34 | Tirana | ALB | Albaniana | t | 97.9
AND | 55 | Andorra la Vella | AND | Catalan | t | 32.3
ANT | 33 | Willemstad | ANT | Dutch | t | 0
ANT | 33 | Willemstad | ANT | Papiamento | t | 86.2
ARE | 65 | Abu Dhabi | ARE | Arabic | t | 42
ARG | 69 | Buenos Aires | ARG | Spanish | t | 96.8
ARM | 126 | Yerevan | ARM | Armenian | t | 93.4
ASM | 54 | Fagatogo | ASM | English | t | 3.1
ASM | 54 | Fagatogo | ASM | Samoan | t | 90.6
ATG | 63 | Saint Johns | ATG | English | t | 0
AUS | 135 | Canberra | AUS | English | t | 81.2
AUT | 1523 | Wien | AUT | German | t | 92
AZE | 144 | Baku | AZE | Azerbaijani | t | 89
BDI | 552 | Bujumbura | BDI | French | t | 0
BDI | 552 | Bujumbura | BDI | Kirundi | t | 98.1
BEL | 179 | Bruxelles [Brussel] | BEL | Dutch | t | 59.2
BEL | 179 | Bruxelles [Brussel] | BEL | French | t | 32.6
BEL | 179 | Bruxelles [Brussel] | BEL | German | t | 1
BGD | 150 | Dhaka | BGD | Bengali | t | 97.7
BGR | 539 | Sofija | BGR | Bulgariana | t | 83.2
BHR | 149 | al-Manama | BHR | Arabic | t | 67.7
BIH | 201 | Sarajevo | BIH | Serbo-Croatian | t | 99.2
BLR | 3520 | Minsk | BLR | Belorussian | t | 65.6
BLR | 3520 | Minsk | BLR | Russian | t | 32
BLZ | 185 | Belmopan | BLZ | English | t | 50.8
BMU | 191 | Hamilton | BMU | English | t | 100
BOL | 194 | La Paz | BOL | Aimara | t | 3.2
BOL | 194 | La Paz | BOL | Ketdua | t | 8.1
BOL | 194 | La Paz | BOL | Spanish | t | 87.7
BRA | 211 | Brasilia | BRA | Portuguese | t | 97.5
BRB | 174 | Bridgetown | BRB | English | t | 0
BRN | 538 | Bandar Seri Begawan | BRN | Malay | t | 45.5
BTN | 192 | Thimphu | BTN | Dzongkha | t | 50
CAN | 1822 | Ottawa | CAN | English | t | 60.4
CAN | 1822 | Ottawa | CAN | French | t | 23.4
CCK | 2317 | West Island | CCK | English | t | 0
CHE | 3248 | Bern | CHE | French | t | 19.2
CHE | 3248 | Bern | CHE | German | t | 63.6
CHE | 3248 | Bern | CHE | Italian | t | 7.7
CHE | 3248 | Bern | CHE | Romansh | t | 0.6
CHL | 554 | Santiago de Chile | CHL | Spanish | t | 89.7
CHN | 1891 | Peking | CHN | Chinese | t | 92
COK | 583 | Avarua | COK | Maori | t | 0
COL | 2257 | Santafe de Bogota | COL | Spanish | t | 99
COM | 2295 | Moroni | COM | Comorian | t | 75
CPV | 1859 | Praia | CPV | Portuguese | t | 0
CRI | 584 | San Jose | CRI | Spanish | t | 97.5
CUB | 2413 | La Habana | CUB | Spanish | t | 100
CXR | 1791 | Flying Fish Cove | CXR | English | t | 0
CYM | 553 | George Town | CYM | English | t | 0
CYP | 2430 | Nicosia | CYP | Greek | t | 74.1
CYP | 2430 | Nicosia | CYP | Turkish | t | 22.4
CZE | 3339 | Praha | CZE | Czech | t | 81.2
DEU | 3068 | Berlin | DEU | German | t | 91.3
DJI | 585 | Djibouti | DJI | Arabic | t | 10.6
DNK | 3315 | Kobenhavn | DNK | Danish | t | 93.5
DOM | 587 | Santo Domingo de Guzman | DOM | Spanish | t | 98
DZA | 35 | Alger | DZA | Arabic | t | 86
ECU | 594 | Quito | ECU | Spanish | t | 93
EGY | 608 | Cairo | EGY | Arabic | t | 98.8
ERI | 652 | Asmara | ERI | Tigrinja | t | 49.1
ESH | 2453 | El-Aaiun | ESH | Arabic | t | 100
ESP | 653 | Madrid | ESP | Spanish | t | 74.4
EST | 3791 | Tallinn | EST | Estonian | t | 65.3
FIN | 3236 | Helsinki [Helsingfors] | FIN | Finnish | t | 92.7
FIN | 3236 | Helsinki [Helsingfors] | FIN | Swedish | t | 5.7
FJI | 764 | Suva | FJI | Fijian | t | 50.8
FLK | 763 | Stanley | FLK | English | t | 0
FRA | 2974 | Paris | FRA | French | t | 93.6
FRO | 901 | Torshavn | FRO | Danish | t | 0
FRO | 901 | Torshavn | FRO | Faroese | t | 100
GBR | 456 | London | GBR | English | t | 97.3
GEO | 905 | Tbilisi | GEO | Georgiana | t | 71.7
GIB | 915 | Gibraltar | GIB | English | t | 88.9
GLP | 919 | Basse-Terre | GLP | French | t | 0
GNB | 927 | Bissau | GNB | Portuguese | t | 8.1
GRC | 2401 | Athenai | GRC | Greek | t | 98.5
GRL | 917 | Nuuk | GRL | Danish | t | 12.5
GRL | 917 | Nuuk | GRL | Greenlandic | t | 87.5
GTM | 922 | Ciudad de Guatemala | GTM | Spanish | t | 64.7
GUM | 921 | Agaaa | GUM | Chamorro | t | 29.6
GUM | 921 | Agaaa | GUM | English | t | 37.5
HKG | 937 | Victoria | HKG | English | t | 2.2
HND | 933 | Tegucigalpa | HND | Spanish | t | 97.2
HRV | 2409 | Zagreb | HRV | Serbo-Croatian | t | 95.9
HTI | 929 | Port-au-Prince | HTI | French | t | 0
HUN | 3483 | Budapest | HUN | Hungarian | t | 98.5
IDN | 939 | Jakarta | IDN | Malay | t | 12.1
IND | 1109 | New Delhi | IND | Hindi | t | 39.9
IRL | 1447 | Dublin | IRL | English | t | 98.4
IRL | 1447 | Dublin | IRL | Irish | t | 1.6
IRN | 1380 | Teheran | IRN | Persian | t | 45.7
IRQ | 1365 | Baghdad | IRQ | Arabic | t | 77.2
ISL | 1449 | Reykjavik | ISL | Icelandic | t | 95.7
ISR | 1450 | Jerusalem | ISR | Arabic | t | 18
ISR | 1450 | Jerusalem | ISR | Hebrew | t | 63.1
ITA | 1464 | Roma | ITA | Italian | t | 94.1
JOR | 1786 | Amman | JOR | Arabic | t | 97.9
JPN | 1532 | Tokyo | JPN | Japanese | t | 99.1
KAZ | 1864 | Astana | KAZ | Kazakh | t | 46
KGZ | 2253 | Bishkek | KGZ | Kirgiz | t | 59.7
KGZ | 2253 | Bishkek | KGZ | Russian | t | 16.2
KHM | 1800 | Phnom Penh | KHM | Khmer | t | 88.6
KIR | 2256 | Bairiki | KIR | Kiribati | t | 98.9
KNA | 3064 | Basseterre | KNA | English | t | 0
KOR | 2331 | Seoul | KOR | Korean | t | 99.9
KWT | 2429 | Kuwait | KWT | Arabic | t | 78.1
LAO | 2432 | Vientiane | LAO | Lao | t | 67.2
LBN | 2438 | Beirut | LBN | Arabic | t | 93
LBY | 2441 | Tripoli | LBY | Arabic | t | 96
LCA | 3065 | Castries | LCA | English | t | 20
LIE | 2446 | Vaduz | LIE | German | t | 89
LKA | 3217 | Colombo | LKA | Singali | t | 60.3
LKA | 3217 | Colombo | LKA | Tamil | t | 19.6
LSO | 2437 | Maseru | LSO | English | t | 0
LSO | 2437 | Maseru | LSO | Sotho | t | 85
LTU | 2447 | Vilnius | LTU | Lithuanian | t | 81.6
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | French | t | 4.2
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | German | t | 2.3
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | Luxembourgish | t | 64.4
LVA | 2434 | Riga | LVA | Latvian | t | 55.1
MAC | 2454 | Macao | MAC | Portuguese | t | 2.3
MAR | 2486 | Rabat | MAR | Arabic | t | 65
MCO | 2695 | Monaco-Ville | MCO | French | t | 41.9
MDA | 2690 | Chisinau | MDA | Romanian | t | 61.9
MDG | 2455 | Antananarivo | MDG | French | t | 0
MDG | 2455 | Antananarivo | MDG | Malagasy | t | 98.9
MDV | 2463 | Male | MDV | Dhivehi | t | 100
MEX | 2515 | Ciudad de Mexico | MEX | Spanish | t | 92.1
MHL | 2507 | Dalap-Uliga-Darrit | MHL | English | t | 0
MHL | 2507 | Dalap-Uliga-Darrit | MHL | Marshallese | t | 96.8
MKD | 2460 | Skopje | MKD | Macedonian | t | 66.5
MLT | 2484 | Valletta | MLT | English | t | 2.1
MLT | 2484 | Valletta | MLT | Maltese | t | 95.8
MMR | 2710 | Rangoon (Yangon) | MMR | Burmese | t | 69
MNG | 2696 | Ulan Bator | MNG | Mongolian | t | 78.8
MNP | 2913 | Garapan | MNP | English | t | 4.8
MSR | 2697 | Plymouth | MSR | English | t | 0
MTQ | 2508 | Fort-de-France | MTQ | French | t | 0
MWI | 2462 | Lilongwe | MWI | Chichewa | t | 58.3
MYS | 2464 | Kuala Lumpur | MYS | Malay | t | 58.4
MYT | 2514 | Mamoutzou | MYT | French | t | 20.3
NCL | 3493 | Noumea | NCL | French | t | 34.3
NFK | 2806 | Kingston | NFK | English | t | 0
NIC | 2734 | Managua | NIC | Spanish | t | 97.6
NIU | 2805 | Alofi | NIU | English | t | 0
NLD | 5 | Amsterdam | NLD | Dutch | t | 95.6
NOR | 2807 | Oslo | NOR | Norwegian | t | 96.6
NPL | 2729 | Kathmandu | NPL | Nepali | t | 50.4
NRU | 2728 | Yaren | NRU | English | t | 7.5
NRU | 2728 | Yaren | NRU | Nauru | t | 57.5
NZL | 3499 | Wellington | NZL | English | t | 87
OMN | 2821 | Masqat | OMN | Arabic | t | 76.7
PAK | 2831 | Islamabad | PAK | Urdu | t | 7.6
PAN | 2882 | Ciudad de Panama | PAN | Spanish | t | 76.8
PER | 2890 | Lima | PER | Aimara | t | 2.3
PER | 2890 | Lima | PER | Ketdua | t | 16.4
PER | 2890 | Lima | PER | Spanish | t | 79.8
PHL | 766 | Manila | PHL | Pilipino | t | 29.3
PLW | 2881 | Koror | PLW | English | t | 3.2
PLW | 2881 | Koror | PLW | Palau | t | 82.2
POL | 2928 | Warszawa | POL | Polish | t | 97.6
PRI | 2919 | San Juan | PRI | Spanish | t | 51.3
PRK | 2318 | Pyongyang | PRK | Korean | t | 99.9
PRT | 2914 | Lisboa | PRT | Portuguese | t | 99
PRY | 2885 | Asuncion | PRY | Guarani | t | 40.1
PRY | 2885 | Asuncion | PRY | Spanish | t | 55.1
PYF | 3016 | Papeete | PYF | French | t | 40.8
QAT | 2973 | Doha | QAT | Arabic | t | 40.7
ROM | 3018 | Bucuresti | ROM | Romani | t | 0.7
ROM | 3018 | Bucuresti | ROM | Romanian | t | 90.7
RUS | 3580 | Moscow | RUS | Russian | t | 86.6
RWA | 3047 | Kigali | RWA | French | t | 0
RWA | 3047 | Kigali | RWA | Rwanda | t | 100
SAU | 3173 | Riyadh | SAU | Arabic | t | 95
SDN | 3225 | Khartum | SDN | Arabic | t | 49.4
SEN | 3198 | Dakar | SEN | Wolof | t | 48.1
SGP | 3208 | Singapore | SGP | Chinese | t | 77.1
SGP | 3208 | Singapore | SGP | Malay | t | 14.1
SGP | 3208 | Singapore | SGP | Tamil | t | 7.4
SHN | 3063 | Jamestown | SHN | English | t | 0
SJM | 938 | Longyearbyen | SJM | Norwegian | t | 0
SLV | 645 | San Salvador | SLV | Spanish | t | 100
SMR | 3171 | San Marino | SMR | Italian | t | 100
SOM | 3214 | Mogadishu | SOM | Arabic | t | 0
SOM | 3214 | Mogadishu | SOM | Somali | t | 98.3
SPM | 3067 | Saint-Pierre | SPM | French | t | 0
SVK | 3209 | Bratislava | SVK | Slovak | t | 85.6
SVN | 3212 | Ljubljana | SVN | Slovene | t | 87.9
SWE | 3048 | Stockholm | SWE | Swedish | t | 89.5
SWZ | 3244 | Mbabane | SWZ | Swazi | t | 89.9
SYC | 3206 | Victoria | SYC | English | t | 3.8
SYC | 3206 | Victoria | SYC | French | t | 1.3
SYR | 3250 | Damascus | SYR | Arabic | t | 90
TCA | 3423 | Cockburn Town | TCA | English | t | 0
TCD | 3337 | NDjamena | TCD | Arabic | t | 12.3
TGO | 3332 | Lome | TGO | Ewe | t | 23.2
TGO | 3332 | Lome | TGO | Kabye | t | 13.8
THA | 3320 | Bangkok | THA | Thai | t | 52.6
TJK | 3261 | Dushanbe | TJK | Tadzhik | t | 62.2
TKL | 3333 | Fakaofo | TKL | English | t | 0
TKM | 3419 | Ashgabat | TKM | Turkmenian | t | 76.7
TMP | 1522 | Dili | TMP | Portuguese | t | 0
TON | 3334 | Nukualofa | TON | English | t | 0
TON | 3334 | Nukualofa | TON | Tongan | t | 98.3
TUN | 3349 | Tunis | TUN | Arabic | t | 69.9
TUR | 3358 | Ankara | TUR | Turkish | t | 87.6
TUV | 3424 | Funafuti | TUV | English | t | 0
TUV | 3424 | Funafuti | TUV | Tuvalu | t | 92.5
TWN | 3263 | Taipei | TWN | Mandarin Chinese | t | 20.1
TZA | 3306 | Dodoma | TZA | Swahili | t | 8.8
UKR | 3426 | Kyiv | UKR | Ukrainian | t | 64.7
URY | 3492 | Montevideo | URY | Spanish | t | 95.7
USA | 3813 | Washington | USA | English | t | 86.2
UZB | 3503 | Toskent | UZB | Uzbek | t | 72.6
VAT | 3538 | Citta del Vaticano | VAT | Italian | t | 0
VCT | 3066 | Kingstown | VCT | English | t | 0
VEN | 3539 | Caracas | VEN | Spanish | t | 96.9
VGB | 537 | Road Town | VGB | English | t | 0
VIR | 4067 | Charlotte Amalie | VIR | English | t | 81.7
VNM | 3770 | Hanoi | VNM | Vietnamese | t | 86.8
VUT | 3537 | Port-Vila | VUT | Bislama | t | 56.6
VUT | 3537 | Port-Vila | VUT | English | t | 28.3
VUT | 3537 | Port-Vila | VUT | French | t | 14.2
WSM | 3169 | Apia | WSM | English | t | 0.6
WSM | 3169 | Apia | WSM | Samoan | t | 47.5
YEM | 1780 | Sanaa | YEM | Arabic | t | 99.6
YUG | 1792 | Beograd | YUG | Serbo-Croatian | t | 75.2
ZAF | 716 | Pretoria | ZAF | Afrikaans | t | 14.3
ZAF | 716 | Pretoria | ZAF | English | t | 8.5
ZAF | 716 | Pretoria | ZAF | Xhosa | t | 17.7
ZAF | 716 | Pretoria | ZAF | Zulu | t | 22.7
ZWE | 4068 | Harare | ZWE | English | t | 2.2
(237 rows)
--query2
with lang_total as
( select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
select lang_count,country.code,country.name,country.continent,country.region,country.population
from country left outer join lang_total
on (lang_total.code = country.code)
where country.indepyear > 1970 and lang_total.lang_count >=1 and lang_total.lang_count >=1
order by lang_total.lang_count desc;
lang_count | code | name | continent | region | population
------------+------+---------------------------------+-----------+-----------------+------------
12 | RUS | Russian Federation | Europe | Eastern Europe | 146934000
6 | FSM | Micronesia, Federated States of | Oceania | Micronesia | 119000
4 | AZE | Azerbaijan | Asia | Middle East | 7734000
1 | BIH | Bosnia and Herzegovina | Europe | Southern Europe | 3972000
(4 rows)
-- Using CTE in the WHERE clause
--query3
with lang_total as
( select lang_count,countrycode from
(
(
select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
UNION ALL
(
select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Monarchy')
group by country.code,countrylanguage.countrycode order by country.code)
) FOO1
)
select * from
(
select count(*) as cnt,country.code,country.name
from
country,countrylanguage
where country.code=countrylanguage.countrycode group by country.code,country.name) AS FOO
where foo.cnt = (select max(lang_count) from lang_total) order by foo.code;
cnt | code | name
-----+------+--------------------
12 | CAN | Canada
12 | CHN | China
12 | IND | India
12 | RUS | Russian Federation
12 | USA | United States
(5 rows)
--query4
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select diversecountries.name,city.name,diversecountries.CNT
from diversecountries,city where city.id = diversecountries.capital
order by diversecountries.name;
name | name | cnt
--------------------+------------------+-----
Australia | Canberra | 8
Austria | Wien | 8
Canada | Ottawa | 12
China | Peking | 12
Denmark | Kobenhavn | 7
India | New Delhi | 12
Iran | Teheran | 10
Italy | Roma | 8
Myanmar | Rangoon (Yangon) | 8
Russian Federation | Moscow | 12
South Africa | Pretoria | 11
United States | Washington | 12
(12 rows)
-- query 5
with somecheapasiandiversecountries as
(
select FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,count(*) ASIAN_COUNT from
(
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Southeast Asia'
and country.continent = 'Asia'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Eastern Asia'
and country.continent = 'Asia'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.gnp < 10000
and country.region = 'Middle East'
and country.continent = 'Asia'
) FOO, countrylanguage
where FOO.code = countrylanguage.countrycode
group by FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,countrylanguage.countrycode
HAVING count(*) >=
(select min(CNT) FROM
(select count(*) CNT,country.code from countrylanguage,country
where countrylanguage.countrycode=country.code
and country.continent = 'Asia'
and country.region = 'Southern and Central Asia'
group by country.code
) FOO1
)
)
select FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate,count(*) COMPARED_WITH_CHEAP_ASIAN_CNT
from
(
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.continent = 'North America'
UNION ALL
select country.code,country.name COUNTRY,city.name CAPITAL,country.headofstate
from country,city
where country.capital = city.id
and country.continent = 'South America'
) FOO,countrylanguage
where FOO.code = countrylanguage.countrycode
group by FOO.code,FOO.COUNTRY,FOO.CAPITAL,FOO.headofstate
HAVING count(*) >=
(select min(ASIAN_COUNT) FROM
(select ASIAN_COUNT FROM somecheapasiandiversecountries,country
where somecheapasiandiversecountries.code = country.code
and country.gnp >= country.gnpold
) ASIANCOUNT
)
order by COUNTRY;
code | country | capital | headofstate | compared_with_cheap_asian_cnt
------+----------------------------------+-------------------------+-----------------------------------+-------------------------------
ATG | Antigua and Barbuda | Saint Johns | Elisabeth II | 2
ARG | Argentina | Buenos Aires | Fernando de la Rua | 3
ABW | Aruba | Oranjestad | Beatrix | 4
BHS | Bahamas | Nassau | Elisabeth II | 2
BRB | Barbados | Bridgetown | Elisabeth II | 2
BLZ | Belize | Belmopan | Elisabeth II | 4
BOL | Bolivia | La Paz | Hugo Banzer Suarez | 4
BRA | Brazil | Brasilia | Fernando Henrique Cardoso | 5
CAN | Canada | Ottawa | Elisabeth II | 12
CHL | Chile | Santiago de Chile | Ricardo Lagos Escobar | 4
COL | Colombia | Santafe de Bogota | Andres Pastrana Arango | 5
CRI | Costa Rica | San Jose | Miguel Angel Rodriguez Echeverria | 4
DMA | Dominica | Roseau | Vernon Shaw | 2
DOM | Dominican Republic | Santo Domingo de Guzman | Hipolito Mejia Dominguez | 2
ECU | Ecuador | Quito | Gustavo Noboa Bejarano | 2
SLV | El Salvador | San Salvador | Francisco Guillermo Flores Perez | 2
GUF | French Guiana | Cayenne | Jacques Chirac | 2
GRL | Greenland | Nuuk | Margrethe II | 2
GLP | Guadeloupe | Basse-Terre | Jacques Chirac | 2
GTM | Guatemala | Ciudad de Guatemala | Alfonso Portillo Cabrera | 5
GUY | Guyana | Georgetown | Bharrat Jagdeo | 3
HTI | Haiti | Port-au-Prince | Jean-Bertrand Aristide | 2
HND | Honduras | Tegucigalpa | Carlos Roberto Flores Facusse | 4
JAM | Jamaica | Kingston | Elisabeth II | 2
MTQ | Martinique | Fort-de-France | Jacques Chirac | 2
MEX | Mexico | Ciudad de Mexico | Vicente Fox Quesada | 6
ANT | Netherlands Antilles | Willemstad | Beatrix | 3
NIC | Nicaragua | Managua | Arnoldo Aleman Lacayo | 4
PAN | Panama | Ciudad de Panama | Mireya Elisa Moscoso Rodriguez | 6
PRY | Paraguay | Asuncion | Luis Angel Gonzalez Macchi | 4
PER | Peru | Lima | Valentin Paniagua Corazao | 3
PRI | Puerto Rico | San Juan | George W. Bush | 2
KNA | Saint Kitts and Nevis | Basseterre | Elisabeth II | 2
LCA | Saint Lucia | Castries | Elisabeth II | 2
VCT | Saint Vincent and the Grenadines | Kingstown | Elisabeth II | 2
SUR | Suriname | Paramaribo | Ronald Venetiaan | 2
TTO | Trinidad and Tobago | Port-of-Spain | Arthur N. R. Robinson | 3
USA | United States | Washington | George W. Bush | 12
VEN | Venezuela | Caracas | Hugo Chavez Frias | 3
VIR | Virgin Islands, U.S. | Charlotte Amalie | George W. Bush | 3
(40 rows)
-- query 6
select count(*) from
( select r.* from
( with fact as
(
select country.name as COUNTRY,country.code,city.name as CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,AGG1.region
from
(select
sum(case when (city.population >= 0.5 * country.population) then country.population else city.population end) as S_POPULATION,
sum(case when (gnp >= gnpold) then gnp else gnpold end) as S_GNP,
avg(case when (lifeexpectancy > 60) then 50 else lifeexpectancy end) as AVG_LIFE,country.region
from country,city
where governmentform != 'Constitutional Monarchy'
and country.capital = city.id
and indepyear > 0
group by country.region) AGG1
,country,city
where country.capital = city.id
and country.region = AGG1.region
)
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'South America'
UNION ALL
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'North America'
UNION ALL
select code,COUNTRY,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage
where fact.code = countrylanguage.countrycode and isofficial = 'True'
and fact.region = 'Caribbean'
) as r
left join
(
select 'ARG' as CODE UNION ALL
select 'BOL' as CODE UNION ALL
select 'BRA' as CODE UNION ALL
select 'PER' as CODE UNION ALL
select 'URY' as CODE UNION ALL
select 'IND' as CODE UNION ALL
select 'LCA' as CODE UNION ALL
select 'VCT' as CODE
) as r1
on r.code = r1.code) AS FOO;
count
-------
43
(1 row)
-- query7
with alleuropeanlanguages as
(select country.code,country.name COUNTRY, city.name CAPITAL, language, isofficial, percentage
FROM country,city,countrylanguage
WHERE country.code = countrylanguage.countrycode
and country.capital = city.id
and country.continent = 'Europe')
select * from
(select * from alleuropeanlanguages where isofficial='True') e1,
(select * from alleuropeanlanguages where percentage > 50) e2
where e1.code = e2.code order by e2.COUNTRY,e1.language;
code | country | capital | language | isofficial | percentage | code | country | capital | language | isofficial | percentage
------+------------------------+-----------------------------------+----------------+------------+------------+------+------------------------+-----------------------------------+----------------+------------+------------
ALB | Albania | Tirana | Albaniana | t | 97.9 | ALB | Albania | Tirana | Albaniana | t | 97.9
AUT | Austria | Wien | German | t | 92 | AUT | Austria | Wien | German | t | 92
BLR | Belarus | Minsk | Belorussian | t | 65.6 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BLR | Belarus | Minsk | Russian | t | 32 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | French | t | 32.6 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | German | t | 1 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2 | BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2
BGR | Bulgaria | Sofija | Bulgariana | t | 83.2 | BGR | Bulgaria | Sofija | Bulgariana | t | 83.2
HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9 | HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Praha | Czech | t | 81.2
DNK | Denmark | Kobenhavn | Danish | t | 93.5 | DNK | Denmark | Kobenhavn | Danish | t | 93.5
EST | Estonia | Tallinn | Estonian | t | 65.3 | EST | Estonia | Tallinn | Estonian | t | 65.3
FRO | Faroe Islands | Torshavn | Danish | t | 0 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FRO | Faroe Islands | Torshavn | Faroese | t | 100 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FIN | Finland | Helsinki [Helsingfors] | Swedish | t | 5.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FRA | France | Paris | French | t | 93.6 | FRA | France | Paris | French | t | 93.6
DEU | Germany | Berlin | German | t | 91.3 | DEU | Germany | Berlin | German | t | 91.3
GIB | Gibraltar | Gibraltar | English | t | 88.9 | GIB | Gibraltar | Gibraltar | English | t | 88.9
GRC | Greece | Athenai | Greek | t | 98.5 | GRC | Greece | Athenai | Greek | t | 98.5
HUN | Hungary | Budapest | Hungarian | t | 98.5 | HUN | Hungary | Budapest | Hungarian | t | 98.5
ISL | Iceland | Reykjavik | Icelandic | t | 95.7 | ISL | Iceland | Reykjavik | Icelandic | t | 95.7
IRL | Ireland | Dublin | English | t | 98.4 | IRL | Ireland | Dublin | English | t | 98.4
IRL | Ireland | Dublin | Irish | t | 1.6 | IRL | Ireland | Dublin | English | t | 98.4
ITA | Italy | Roma | Italian | t | 94.1 | ITA | Italy | Roma | Italian | t | 94.1
LVA | Latvia | Riga | Latvian | t | 55.1 | LVA | Latvia | Riga | Latvian | t | 55.1
LIE | Liechtenstein | Vaduz | German | t | 89 | LIE | Liechtenstein | Vaduz | German | t | 89
LTU | Lithuania | Vilnius | Lithuanian | t | 81.6 | LTU | Lithuania | Vilnius | Lithuanian | t | 81.6
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | French | t | 4.2 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | German | t | 2.3 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
MKD | Macedonia | Skopje | Macedonian | t | 66.5 | MKD | Macedonia | Skopje | Macedonian | t | 66.5
MLT | Malta | Valletta | English | t | 2.1 | MLT | Malta | Valletta | Maltese | t | 95.8
MLT | Malta | Valletta | Maltese | t | 95.8 | MLT | Malta | Valletta | Maltese | t | 95.8
MDA | Moldova | Chisinau | Romanian | t | 61.9 | MDA | Moldova | Chisinau | Romanian | t | 61.9
NLD | Netherlands | Amsterdam | Dutch | t | 95.6 | NLD | Netherlands | Amsterdam | Dutch | t | 95.6
NOR | Norway | Oslo | Norwegian | t | 96.6 | NOR | Norway | Oslo | Norwegian | t | 96.6
POL | Poland | Warszawa | Polish | t | 97.6 | POL | Poland | Warszawa | Polish | t | 97.6
PRT | Portugal | Lisboa | Portuguese | t | 99 | PRT | Portugal | Lisboa | Portuguese | t | 99
ROM | Romania | Bucuresti | Romani | t | 0.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
ROM | Romania | Bucuresti | Romanian | t | 90.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
RUS | Russian Federation | Moscow | Russian | t | 86.6 | RUS | Russian Federation | Moscow | Russian | t | 86.6
SMR | San Marino | San Marino | Italian | t | 100 | SMR | San Marino | San Marino | Italian | t | 100
SVK | Slovakia | Bratislava | Slovak | t | 85.6 | SVK | Slovakia | Bratislava | Slovak | t | 85.6
SVN | Slovenia | Ljubljana | Slovene | t | 87.9 | SVN | Slovenia | Ljubljana | Slovene | t | 87.9
ESP | Spain | Madrid | Spanish | t | 74.4 | ESP | Spain | Madrid | Spanish | t | 74.4
SWE | Sweden | Stockholm | Swedish | t | 89.5 | SWE | Sweden | Stockholm | Swedish | t | 89.5
CHE | Switzerland | Bern | French | t | 19.2 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | German | t | 63.6 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Italian | t | 7.7 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Romansh | t | 0.6 | CHE | Switzerland | Bern | German | t | 63.6
UKR | Ukraine | Kyiv | Ukrainian | t | 64.7 | UKR | Ukraine | Kyiv | Ukrainian | t | 64.7
GBR | United Kingdom | London | English | t | 97.3 | GBR | United Kingdom | London | English | t | 97.3
YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2 | YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2
(55 rows)
-- query8
with allcountrystats as
( select country.code,country.name,count(distinct city.id) CITY_CNT,
count(distinct countrylanguage.language) LANG_CNT
from country,city,countrylanguage
where country.code = city.countrycode
and country.code = countrylanguage.countrycode
group by country.code,country.name
)
select sum(FOO.CITY_CNT) REGION_CITY_CNT,sum(FOO.LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > 1
and country.continent = 'Asia'
UNION ALL
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > 1
and country.continent = 'North America'
UNION ALL
select allcountrystats.code,allcountrystats.name COUNTRY,CITY_CNT,LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and CITY_CNT/LANG_CNT > (select max(CITY_CNT/LANG_CNT) from allcountrystats,country where allcountrystats.code = country.code AND country.continent='Europe')
) FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
region_city_cnt | region_lang_cnt | region
-----------------+-----------------+---------------------------
840 | 192 | Caribbean
2824 | 112 | Central America
11336 | 384 | Eastern Asia
2664 | 396 | Middle East
1625 | 125 | North America
3500 | 70 | South America
3179 | 528 | Southeast Asia
12278 | 896 | Southern and Central Asia
(8 rows)
--query 9
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select d1.code,d1.name,d1.capital,city.name CAPITAL_CITY,d1.CNT,d2.CNT
from
diversecountries d1 left join country
ON (d1.code = country.code AND d1.CNT < 8)
left join diversecountries d2
ON (country.code = d2.code AND d2.CNT > 8)
INNER JOIN city
ON(d1.capital = city.id)
ORDER BY d1.name;
code | name | capital | capital_city | cnt | cnt
------+--------------------+---------+------------------+-----+-----
AUS | Australia | 135 | Canberra | 8 |
AUT | Austria | 1523 | Wien | 8 |
CAN | Canada | 1822 | Ottawa | 12 |
CHN | China | 1891 | Peking | 12 |
DNK | Denmark | 3315 | Kobenhavn | 7 |
IND | India | 1109 | New Delhi | 12 |
IRN | Iran | 1380 | Teheran | 10 |
ITA | Italy | 1464 | Roma | 8 |
MMR | Myanmar | 2710 | Rangoon (Yangon) | 8 |
RUS | Russian Federation | 3580 | Moscow | 12 |
ZAF | South Africa | 716 | Pretoria | 11 |
USA | United States | 3813 | Washington | 12 |
(12 rows)
--query 10 , multiple ctes, joins
with longlivingregions as
(
select FOO.*,count(distinct language) as "lang_count"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
avg(lifeexpectancy) as "REGION_LIFETIME",region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP",foo."REGION_LIFETIME"),
denseregions as
(
select FOO.*,count(distinct language) as "lang_count",
sum(surfacearea) as "REGION_SURFACE_AREA"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
and FOO."REGION_POP" != 0
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP"
order by sum(surfacearea)/foo."REGION_POP" desc),
allcountrystats as
( select country.code,country.name,count(distinct city.id) CITY_CNT,
count(distinct countrylanguage.language) LANG_CNT
from country,city,countrylanguage
where country.code = city.countrycode
and country.code = countrylanguage.countrycode
group by country.code,country.name
)
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1800 and 1850
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1850 and 1900
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear > 1900
order by name
LIMIT 50;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
city_cnt | lang_cnt | name | REGION_SURFACE_AREA | REGION_LIFETIME | REGION_POP | lang_count | REGION_GNP | region
----------+----------+---------------------------------------+---------------------+------------------+------------+------------+------------+---------------------------
4 | 5 | Afghanistan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 3 | Albania | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
18 | 2 | Algeria | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
5 | 9 | Angola | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 2 | Antigua and Barbuda | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
57 | 3 | Argentina | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 2 | Armenia | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
14 | 8 | Australia | 62471162.00 | 78.8000030517578 | 22753100 | 11 | 405851.00 | Australia and New Zealand
6 | 8 | Austria | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
4 | 4 | Azerbaijan | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
1 | 2 | Bahamas | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
1 | 2 | Bahrain | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
24 | 7 | Bangladesh | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 2 | Barbados | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
16 | 4 | Belarus | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
9 | 6 | Belgium | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
2 | 4 | Belize | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
4 | 7 | Benin | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Bhutan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
8 | 4 | Bolivia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 1 | Bosnia and Herzegovina | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
2 | 5 | Botswana | 23060186.00 | 44.8199996948242 | 46886000 | 21 | 126931.00 | Southern Africa
250 | 5 | Brazil | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 4 | Brunei | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
10 | 4 | Bulgaria | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
3 | 6 | Burkina Faso | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Burundi | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
3 | 4 | Cambodia | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
7 | 8 | Cameroon | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
49 | 12 | Canada | 236342035.00 | 75.8199996948242 | 309632000 | 18 | 9111890.00 | North America
1 | 2 | Cape Verde | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 6 | Central African Republic | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
2 | 8 | Chad | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
29 | 4 | Chile | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
38 | 5 | Colombia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 5 | Comoros | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
2 | 6 | Congo | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
18 | 10 | Congo, The Democratic Republic of the | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 4 | Costa Rica | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
5 | 5 | Cote deIvoire | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
4 | 2 | Croatia | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
14 | 1 | Cuba | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
2 | 2 | Cyprus | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
10 | 8 | Czech Republic | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
1 | 3 | Djibouti | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
1 | 2 | Dominica | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
6 | 2 | Dominican Republic | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
15 | 2 | Ecuador | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
37 | 2 | Egypt | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
7 | 2 | El Salvador | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
(50 rows)
--query 11
with allcitystats as
( select city.name CITY,city.id,country.name COUNTRY,city.district,city.population as CITY_POP
from
city,country
where city.countrycode = country.code
),
alldistrictstats as
( select allcitystats.district,allcitystats.COUNTRY,sum(CITY_POP) DISTRICT_POP,
count(CITY) as D_CITY_CNT
from allcitystats
group by allcitystats.district,allcitystats.COUNTRY
order by district,COUNTRY
),
allcountrystats as
( select alldistrictstats.COUNTRY,country.code,sum(D_CITY_CNT) C_CITY_CNT,
count(distinct countrylanguage.language) C_LANG_CNT
from alldistrictstats,country,countrylanguage
where alldistrictstats.COUNTRY = country.name
and country.code = countrylanguage.countrycode
group by COUNTRY,code
),
asian_region_stats as
(
select sum(FOO.C_CITY_CNT) REGION_CITY_CNT,sum(FOO.C_LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountrystats.code,allcountrystats.COUNTRY,C_CITY_CNT,C_LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and C_CITY_CNT/C_LANG_CNT > 1
and country.continent = 'Asia') FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region
)
select * from
(
select REGION_CITY_CNT as CITY_CNT,REGION_LANG_CNT as LANG_CNT, region as IDENTIFIER from asian_region_stats
UNION ALL
(
select sum(FOO.C_CITY_CNT) CITY_CNT,sum(FOO.C_LANG_CNT) LANG_CNT,FOO.region as IDENTIFIER
FROM
(
select allcountrystats.code,allcountrystats.COUNTRY,C_CITY_CNT,C_LANG_CNT,country.region,allcitystats.CITY CAPITAL
from allcountrystats,country,allcitystats
where allcountrystats.code = country.code
and country.capital = allcitystats.id
and C_CITY_CNT/C_LANG_CNT > 1
and country.continent = 'Europe') FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region
)
) FOO1
order by FOO1.lang_cnt,FOO1.identifier;
city_cnt | lang_cnt | identifier
----------+----------+---------------------------
494 | 10 | British Islands
159 | 48 | Baltic Countries
1295 | 161 | Nordic Countries
50176 | 264 | Eastern Asia
9414 | 369 | Western Europe
11880 | 450 | Southern Europe
32900 | 610 | Eastern Europe
29161 | 616 | Southeast Asia
8568 | 792 | Middle East
80388 | 1092 | Southern and Central Asia
(10 rows)
-- queries with CTEs on AO tables
DROP TABLE IF EXISTS CITY_AO;
NOTICE: table "city_ao" does not exist, skipping
CREATE TABLE CITY_AO (
id integer NOT NULL,
name text NOT NULL,
countrycode character(3) NOT NULL,
district text NOT NULL,
population integer NOT NULL
) with (appendonly=true) distributed by(id);
DROP TABLE IF EXISTS country_ao;
NOTICE: table "country_ao" does not exist, skipping
CREATE TABLE country_ao (
code character(3) NOT NULL,
name text NOT NULL,
continent text NOT NULL,
region text NOT NULL,
surfacearea numeric(10,2) NOT NULL,
indepyear smallint,
population integer NOT NULL,
lifeexpectancy real,
gnp numeric(10,2),
gnpold numeric(10,2),
localname text NOT NULL,
governmentform text NOT NULL,
headofstate text,
capital integer,
code2 character(2) NOT NULL
) with (appendonly=true) distributed by (code);
DROP TABLE IF EXISTS countrylanguage_ao;
NOTICE: table "countrylanguage_ao" does not exist, skipping
CREATE TABLE countrylanguage_ao (
countrycode character(3) NOT NULL,
"language" text NOT NULL,
isofficial boolean NOT NULL,
percentage real NOT NULL
) with (appendonly=true) distributed by (countrycode,language);
create index bitmap_city_ao_countrycode on city_ao using bitmap(countrycode);
create index bitmap_country_ao_gf on country_ao using bitmap(governmentform);
create index bitmap_country_ao_region on country_ao using bitmap(region);
create index bitmap_country_ao_continent on country_ao using bitmap(continent);
create index bitmap_countrylanguage_ao_countrycode on countrylanguage_ao using bitmap(countrycode);
INSERT INTO CITY_AO SELECT * FROM CITY;
INSERT INTO COUNTRY_AO SELECT * FROM COUNTRY;
INSERT INTO COUNTRYLANGUAGE_AO SELECT * FROM COUNTRYLANGUAGE;
ANALYZE CITY_AO;
ANALYZE COUNTRY_AO;
ANALYZE COUNTRYLANGUAGE_AO;
set enable_seqscan=off;
set enable_indexscan=on;
--query1
with capitals as
(select country_ao.code,id,city_ao.name from city_ao,country_ao
where city_ao.countrycode = country_ao.code AND city_ao.id = country_ao.capital)
select * from
capitals,countrylanguage_ao
where capitals.code = countrylanguage_ao.countrycode and isofficial='true'
order by capitals.code,countrylanguage_ao.language;
code | id | name | countrycode | language | isofficial | percentage
------+------+-----------------------------------+-------------+------------------+------------+------------
ABW | 129 | Oranjestad | ABW | Dutch | t | 5.3
AFG | 1 | Kabul | AFG | Dari | t | 32.1
AFG | 1 | Kabul | AFG | Pashto | t | 52.4
AIA | 62 | The Valley | AIA | English | t | 0
ALB | 34 | Tirana | ALB | Albaniana | t | 97.9
AND | 55 | Andorra la Vella | AND | Catalan | t | 32.3
ANT | 33 | Willemstad | ANT | Dutch | t | 0
ANT | 33 | Willemstad | ANT | Papiamento | t | 86.2
ARE | 65 | Abu Dhabi | ARE | Arabic | t | 42
ARG | 69 | Buenos Aires | ARG | Spanish | t | 96.8
ARM | 126 | Yerevan | ARM | Armenian | t | 93.4
ASM | 54 | Fagatogo | ASM | English | t | 3.1
ASM | 54 | Fagatogo | ASM | Samoan | t | 90.6
ATG | 63 | Saint Johns | ATG | English | t | 0
AUS | 135 | Canberra | AUS | English | t | 81.2
AUT | 1523 | Wien | AUT | German | t | 92
AZE | 144 | Baku | AZE | Azerbaijani | t | 89
BDI | 552 | Bujumbura | BDI | French | t | 0
BDI | 552 | Bujumbura | BDI | Kirundi | t | 98.1
BEL | 179 | Bruxelles [Brussel] | BEL | Dutch | t | 59.2
BEL | 179 | Bruxelles [Brussel] | BEL | French | t | 32.6
BEL | 179 | Bruxelles [Brussel] | BEL | German | t | 1
BGD | 150 | Dhaka | BGD | Bengali | t | 97.7
BGR | 539 | Sofija | BGR | Bulgariana | t | 83.2
BHR | 149 | al-Manama | BHR | Arabic | t | 67.7
BIH | 201 | Sarajevo | BIH | Serbo-Croatian | t | 99.2
BLR | 3520 | Minsk | BLR | Belorussian | t | 65.6
BLR | 3520 | Minsk | BLR | Russian | t | 32
BLZ | 185 | Belmopan | BLZ | English | t | 50.8
BMU | 191 | Hamilton | BMU | English | t | 100
BOL | 194 | La Paz | BOL | Aimara | t | 3.2
BOL | 194 | La Paz | BOL | Ketdua | t | 8.1
BOL | 194 | La Paz | BOL | Spanish | t | 87.7
BRA | 211 | Brasilia | BRA | Portuguese | t | 97.5
BRB | 174 | Bridgetown | BRB | English | t | 0
BRN | 538 | Bandar Seri Begawan | BRN | Malay | t | 45.5
BTN | 192 | Thimphu | BTN | Dzongkha | t | 50
CAN | 1822 | Ottawa | CAN | English | t | 60.4
CAN | 1822 | Ottawa | CAN | French | t | 23.4
CCK | 2317 | West Island | CCK | English | t | 0
CHE | 3248 | Bern | CHE | French | t | 19.2
CHE | 3248 | Bern | CHE | German | t | 63.6
CHE | 3248 | Bern | CHE | Italian | t | 7.7
CHE | 3248 | Bern | CHE | Romansh | t | 0.6
CHL | 554 | Santiago de Chile | CHL | Spanish | t | 89.7
CHN | 1891 | Peking | CHN | Chinese | t | 92
COK | 583 | Avarua | COK | Maori | t | 0
COL | 2257 | Santafe de Bogota | COL | Spanish | t | 99
COM | 2295 | Moroni | COM | Comorian | t | 75
CPV | 1859 | Praia | CPV | Portuguese | t | 0
CRI | 584 | San Jose | CRI | Spanish | t | 97.5
CUB | 2413 | La Habana | CUB | Spanish | t | 100
CXR | 1791 | Flying Fish Cove | CXR | English | t | 0
CYM | 553 | George Town | CYM | English | t | 0
CYP | 2430 | Nicosia | CYP | Greek | t | 74.1
CYP | 2430 | Nicosia | CYP | Turkish | t | 22.4
CZE | 3339 | Praha | CZE | Czech | t | 81.2
DEU | 3068 | Berlin | DEU | German | t | 91.3
DJI | 585 | Djibouti | DJI | Arabic | t | 10.6
DNK | 3315 | Kobenhavn | DNK | Danish | t | 93.5
DOM | 587 | Santo Domingo de Guzman | DOM | Spanish | t | 98
DZA | 35 | Alger | DZA | Arabic | t | 86
ECU | 594 | Quito | ECU | Spanish | t | 93
EGY | 608 | Cairo | EGY | Arabic | t | 98.8
ERI | 652 | Asmara | ERI | Tigrinja | t | 49.1
ESH | 2453 | El-Aaiun | ESH | Arabic | t | 100
ESP | 653 | Madrid | ESP | Spanish | t | 74.4
EST | 3791 | Tallinn | EST | Estonian | t | 65.3
FIN | 3236 | Helsinki [Helsingfors] | FIN | Finnish | t | 92.7
FIN | 3236 | Helsinki [Helsingfors] | FIN | Swedish | t | 5.7
FJI | 764 | Suva | FJI | Fijian | t | 50.8
FLK | 763 | Stanley | FLK | English | t | 0
FRA | 2974 | Paris | FRA | French | t | 93.6
FRO | 901 | Torshavn | FRO | Danish | t | 0
FRO | 901 | Torshavn | FRO | Faroese | t | 100
GBR | 456 | London | GBR | English | t | 97.3
GEO | 905 | Tbilisi | GEO | Georgiana | t | 71.7
GIB | 915 | Gibraltar | GIB | English | t | 88.9
GLP | 919 | Basse-Terre | GLP | French | t | 0
GNB | 927 | Bissau | GNB | Portuguese | t | 8.1
GRC | 2401 | Athenai | GRC | Greek | t | 98.5
GRL | 917 | Nuuk | GRL | Danish | t | 12.5
GRL | 917 | Nuuk | GRL | Greenlandic | t | 87.5
GTM | 922 | Ciudad de Guatemala | GTM | Spanish | t | 64.7
GUM | 921 | Agaaa | GUM | Chamorro | t | 29.6
GUM | 921 | Agaaa | GUM | English | t | 37.5
HKG | 937 | Victoria | HKG | English | t | 2.2
HND | 933 | Tegucigalpa | HND | Spanish | t | 97.2
HRV | 2409 | Zagreb | HRV | Serbo-Croatian | t | 95.9
HTI | 929 | Port-au-Prince | HTI | French | t | 0
HUN | 3483 | Budapest | HUN | Hungarian | t | 98.5
IDN | 939 | Jakarta | IDN | Malay | t | 12.1
IND | 1109 | New Delhi | IND | Hindi | t | 39.9
IRL | 1447 | Dublin | IRL | English | t | 98.4
IRL | 1447 | Dublin | IRL | Irish | t | 1.6
IRN | 1380 | Teheran | IRN | Persian | t | 45.7
IRQ | 1365 | Baghdad | IRQ | Arabic | t | 77.2
ISL | 1449 | Reykjavik | ISL | Icelandic | t | 95.7
ISR | 1450 | Jerusalem | ISR | Arabic | t | 18
ISR | 1450 | Jerusalem | ISR | Hebrew | t | 63.1
ITA | 1464 | Roma | ITA | Italian | t | 94.1
JOR | 1786 | Amman | JOR | Arabic | t | 97.9
JPN | 1532 | Tokyo | JPN | Japanese | t | 99.1
KAZ | 1864 | Astana | KAZ | Kazakh | t | 46
KGZ | 2253 | Bishkek | KGZ | Kirgiz | t | 59.7
KGZ | 2253 | Bishkek | KGZ | Russian | t | 16.2
KHM | 1800 | Phnom Penh | KHM | Khmer | t | 88.6
KIR | 2256 | Bairiki | KIR | Kiribati | t | 98.9
KNA | 3064 | Basseterre | KNA | English | t | 0
KOR | 2331 | Seoul | KOR | Korean | t | 99.9
KWT | 2429 | Kuwait | KWT | Arabic | t | 78.1
LAO | 2432 | Vientiane | LAO | Lao | t | 67.2
LBN | 2438 | Beirut | LBN | Arabic | t | 93
LBY | 2441 | Tripoli | LBY | Arabic | t | 96
LCA | 3065 | Castries | LCA | English | t | 20
LIE | 2446 | Vaduz | LIE | German | t | 89
LKA | 3217 | Colombo | LKA | Singali | t | 60.3
LKA | 3217 | Colombo | LKA | Tamil | t | 19.6
LSO | 2437 | Maseru | LSO | English | t | 0
LSO | 2437 | Maseru | LSO | Sotho | t | 85
LTU | 2447 | Vilnius | LTU | Lithuanian | t | 81.6
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | French | t | 4.2
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | German | t | 2.3
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | Luxembourgish | t | 64.4
LVA | 2434 | Riga | LVA | Latvian | t | 55.1
MAC | 2454 | Macao | MAC | Portuguese | t | 2.3
MAR | 2486 | Rabat | MAR | Arabic | t | 65
MCO | 2695 | Monaco-Ville | MCO | French | t | 41.9
MDA | 2690 | Chisinau | MDA | Romanian | t | 61.9
MDG | 2455 | Antananarivo | MDG | French | t | 0
MDG | 2455 | Antananarivo | MDG | Malagasy | t | 98.9
MDV | 2463 | Male | MDV | Dhivehi | t | 100
MEX | 2515 | Ciudad de Mexico | MEX | Spanish | t | 92.1
MHL | 2507 | Dalap-Uliga-Darrit | MHL | English | t | 0
MHL | 2507 | Dalap-Uliga-Darrit | MHL | Marshallese | t | 96.8
MKD | 2460 | Skopje | MKD | Macedonian | t | 66.5
MLT | 2484 | Valletta | MLT | English | t | 2.1
MLT | 2484 | Valletta | MLT | Maltese | t | 95.8
MMR | 2710 | Rangoon (Yangon) | MMR | Burmese | t | 69
MNG | 2696 | Ulan Bator | MNG | Mongolian | t | 78.8
MNP | 2913 | Garapan | MNP | English | t | 4.8
MSR | 2697 | Plymouth | MSR | English | t | 0
MTQ | 2508 | Fort-de-France | MTQ | French | t | 0
MWI | 2462 | Lilongwe | MWI | Chichewa | t | 58.3
MYS | 2464 | Kuala Lumpur | MYS | Malay | t | 58.4
MYT | 2514 | Mamoutzou | MYT | French | t | 20.3
NCL | 3493 | Noumea | NCL | French | t | 34.3
NFK | 2806 | Kingston | NFK | English | t | 0
NIC | 2734 | Managua | NIC | Spanish | t | 97.6
NIU | 2805 | Alofi | NIU | English | t | 0
NLD | 5 | Amsterdam | NLD | Dutch | t | 95.6
NOR | 2807 | Oslo | NOR | Norwegian | t | 96.6
NPL | 2729 | Kathmandu | NPL | Nepali | t | 50.4
NRU | 2728 | Yaren | NRU | English | t | 7.5
NRU | 2728 | Yaren | NRU | Nauru | t | 57.5
NZL | 3499 | Wellington | NZL | English | t | 87
OMN | 2821 | Masqat | OMN | Arabic | t | 76.7
PAK | 2831 | Islamabad | PAK | Urdu | t | 7.6
PAN | 2882 | Ciudad de Panama | PAN | Spanish | t | 76.8
PER | 2890 | Lima | PER | Aimara | t | 2.3
PER | 2890 | Lima | PER | Ketdua | t | 16.4
PER | 2890 | Lima | PER | Spanish | t | 79.8
PHL | 766 | Manila | PHL | Pilipino | t | 29.3
PLW | 2881 | Koror | PLW | English | t | 3.2
PLW | 2881 | Koror | PLW | Palau | t | 82.2
POL | 2928 | Warszawa | POL | Polish | t | 97.6
PRI | 2919 | San Juan | PRI | Spanish | t | 51.3
PRK | 2318 | Pyongyang | PRK | Korean | t | 99.9
PRT | 2914 | Lisboa | PRT | Portuguese | t | 99
PRY | 2885 | Asuncion | PRY | Guarani | t | 40.1
PRY | 2885 | Asuncion | PRY | Spanish | t | 55.1
PYF | 3016 | Papeete | PYF | French | t | 40.8
QAT | 2973 | Doha | QAT | Arabic | t | 40.7
ROM | 3018 | Bucuresti | ROM | Romani | t | 0.7
ROM | 3018 | Bucuresti | ROM | Romanian | t | 90.7
RUS | 3580 | Moscow | RUS | Russian | t | 86.6
RWA | 3047 | Kigali | RWA | French | t | 0
RWA | 3047 | Kigali | RWA | Rwanda | t | 100
SAU | 3173 | Riyadh | SAU | Arabic | t | 95
SDN | 3225 | Khartum | SDN | Arabic | t | 49.4
SEN | 3198 | Dakar | SEN | Wolof | t | 48.1
SGP | 3208 | Singapore | SGP | Chinese | t | 77.1
SGP | 3208 | Singapore | SGP | Malay | t | 14.1
SGP | 3208 | Singapore | SGP | Tamil | t | 7.4
SHN | 3063 | Jamestown | SHN | English | t | 0
SJM | 938 | Longyearbyen | SJM | Norwegian | t | 0
SLV | 645 | San Salvador | SLV | Spanish | t | 100
SMR | 3171 | San Marino | SMR | Italian | t | 100
SOM | 3214 | Mogadishu | SOM | Arabic | t | 0
SOM | 3214 | Mogadishu | SOM | Somali | t | 98.3
SPM | 3067 | Saint-Pierre | SPM | French | t | 0
SVK | 3209 | Bratislava | SVK | Slovak | t | 85.6
SVN | 3212 | Ljubljana | SVN | Slovene | t | 87.9
SWE | 3048 | Stockholm | SWE | Swedish | t | 89.5
SWZ | 3244 | Mbabane | SWZ | Swazi | t | 89.9
SYC | 3206 | Victoria | SYC | English | t | 3.8
SYC | 3206 | Victoria | SYC | French | t | 1.3
SYR | 3250 | Damascus | SYR | Arabic | t | 90
TCA | 3423 | Cockburn Town | TCA | English | t | 0
TCD | 3337 | NDjamena | TCD | Arabic | t | 12.3
TGO | 3332 | Lome | TGO | Ewe | t | 23.2
TGO | 3332 | Lome | TGO | Kabye | t | 13.8
THA | 3320 | Bangkok | THA | Thai | t | 52.6
TJK | 3261 | Dushanbe | TJK | Tadzhik | t | 62.2
TKL | 3333 | Fakaofo | TKL | English | t | 0
TKM | 3419 | Ashgabat | TKM | Turkmenian | t | 76.7
TMP | 1522 | Dili | TMP | Portuguese | t | 0
TON | 3334 | Nukualofa | TON | English | t | 0
TON | 3334 | Nukualofa | TON | Tongan | t | 98.3
TUN | 3349 | Tunis | TUN | Arabic | t | 69.9
TUR | 3358 | Ankara | TUR | Turkish | t | 87.6
TUV | 3424 | Funafuti | TUV | English | t | 0
TUV | 3424 | Funafuti | TUV | Tuvalu | t | 92.5
TWN | 3263 | Taipei | TWN | Mandarin Chinese | t | 20.1
TZA | 3306 | Dodoma | TZA | Swahili | t | 8.8
UKR | 3426 | Kyiv | UKR | Ukrainian | t | 64.7
URY | 3492 | Montevideo | URY | Spanish | t | 95.7
USA | 3813 | Washington | USA | English | t | 86.2
UZB | 3503 | Toskent | UZB | Uzbek | t | 72.6
VAT | 3538 | Citta del Vaticano | VAT | Italian | t | 0
VCT | 3066 | Kingstown | VCT | English | t | 0
VEN | 3539 | Caracas | VEN | Spanish | t | 96.9
VGB | 537 | Road Town | VGB | English | t | 0
VIR | 4067 | Charlotte Amalie | VIR | English | t | 81.7
VNM | 3770 | Hanoi | VNM | Vietnamese | t | 86.8
VUT | 3537 | Port-Vila | VUT | Bislama | t | 56.6
VUT | 3537 | Port-Vila | VUT | English | t | 28.3
VUT | 3537 | Port-Vila | VUT | French | t | 14.2
WSM | 3169 | Apia | WSM | English | t | 0.6
WSM | 3169 | Apia | WSM | Samoan | t | 47.5
YEM | 1780 | Sanaa | YEM | Arabic | t | 99.6
YUG | 1792 | Beograd | YUG | Serbo-Croatian | t | 75.2
ZAF | 716 | Pretoria | ZAF | Afrikaans | t | 14.3
ZAF | 716 | Pretoria | ZAF | English | t | 8.5
ZAF | 716 | Pretoria | ZAF | Xhosa | t | 17.7
ZAF | 716 | Pretoria | ZAF | Zulu | t | 22.7
ZWE | 4068 | Harare | ZWE | English | t | 2.2
(237 rows)
--query2
with lang_total as
( select count(*) as lang_count,country_ao.code,countrylanguage_ao.countrycode
from country_ao join countrylanguage_ao on (country_ao.code=countrylanguage_ao.countrycode and governmentform='Federal Republic')
group by country_ao.code,countrylanguage_ao.countrycode order by country_ao.code)
select lang_count,country_ao.code,country_ao.name,country_ao.continent,country_ao.region,country_ao.population
from country_ao left outer join lang_total
on (lang_total.code = country_ao.code)
where country_ao.indepyear > 1970 and lang_total.lang_count >=1 and lang_total.lang_count >=1
order by lang_total.lang_count desc;
lang_count | code | name | continent | region | population
------------+------+---------------------------------+-----------+-----------------+------------
12 | RUS | Russian Federation | Europe | Eastern Europe | 146934000
6 | FSM | Micronesia, Federated States of | Oceania | Micronesia | 119000
4 | AZE | Azerbaijan | Asia | Middle East | 7734000
1 | BIH | Bosnia and Herzegovina | Europe | Southern Europe | 3972000
(4 rows)
-- Using CTE in the WHERE clause
--query3
with lang_total as
( select lang_count,countrycode from
(
(
select count(*) as lang_count,country_ao.code,countrylanguage_ao.countrycode
from country_ao join countrylanguage_ao on (country_ao.code=countrylanguage_ao.countrycode and governmentform='Federal Republic')
group by country_ao.code,countrylanguage_ao.countrycode order by country_ao.code)
UNION ALL
(
select count(*) as lang_count,country_ao.code,countrylanguage_ao.countrycode
from country_ao join countrylanguage_ao on (country_ao.code=countrylanguage_ao.countrycode and governmentform='Monarchy')
group by country_ao.code,countrylanguage_ao.countrycode order by country_ao.code)
) FOO1
)
select * from
(
select count(*) as cnt,country_ao.code,country_ao.name
from
country_ao,countrylanguage_ao
where country_ao.code=countrylanguage_ao.countrycode group by country_ao.code,country_ao.name) AS FOO
where foo.cnt = (select max(lang_count) from lang_total) order by foo.code;
cnt | code | name
-----+------+--------------------
12 | CAN | Canada
12 | CHN | China
12 | IND | India
12 | RUS | Russian Federation
12 | USA | United States
(5 rows)
--query4
with diversecountries as
(select country_ao.code,country_ao.name,country_ao.capital,d.CNT
from country_ao,
(select countrylanguage_ao.countrycode,count(*) as CNT from countrylanguage_ao group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country_ao.code and country_ao.gnp > 100000)
select diversecountries.name,city_ao.name,diversecountries.CNT
from diversecountries,city_ao where city_ao.id = diversecountries.capital
order by diversecountries.name;
name | name | cnt
--------------------+------------------+-----
Australia | Canberra | 8
Austria | Wien | 8
Canada | Ottawa | 12
China | Peking | 12
Denmark | Kobenhavn | 7
India | New Delhi | 12
Iran | Teheran | 10
Italy | Roma | 8
Myanmar | Rangoon (Yangon) | 8
Russian Federation | Moscow | 12
South Africa | Pretoria | 11
United States | Washington | 12
(12 rows)
-- some queries with merge joins and index scans
set enable_nestloop=off;
set enable_hashjoin=off;
set enable_mergejoin=on;
-- query 5
with somecheapasiandiversecountries as
(
select FOO.code,FOO.COUNTRY_AO,FOO.CAPITAL,FOO.headofstate,count(*) ASIAN_COUNT from
(
select country_ao.code,country_ao.name COUNTRY_AO,city_ao.name CAPITAL,country_ao.headofstate
from country_ao,city_ao
where country_ao.capital = city_ao.id
and country_ao.gnp < 10000
and country_ao.region = 'Southeast Asia'
and country_ao.continent = 'Asia'
UNION ALL
select country_ao.code,country_ao.name COUNTRY_AO,city_ao.name CAPITAL,country_ao.headofstate
from country_ao,city_ao
where country_ao.capital = city_ao.id
and country_ao.gnp < 10000
and country_ao.region = 'Eastern Asia'
and country_ao.continent = 'Asia'
UNION ALL
select country_ao.code,country_ao.name COUNTRY_AO,city_ao.name CAPITAL,country_ao.headofstate
from country_ao,city_ao
where country_ao.capital = city_ao.id
and country_ao.gnp < 10000
and country_ao.region = 'Middle East'
and country_ao.continent = 'Asia'
) FOO, countrylanguage_ao
where FOO.code = countrylanguage_ao.countrycode
group by FOO.code,FOO.COUNTRY_AO,FOO.CAPITAL,FOO.headofstate,countrylanguage_ao.countrycode
HAVING count(*) >=
(select min(CNT) FROM
(select count(*) CNT,country_ao.code from countrylanguage_ao,country_ao
where countrylanguage_ao.countrycode=country_ao.code
and country_ao.continent = 'Asia'
and country_ao.region = 'Southern and Central Asia'
group by country_ao.code
) FOO1
)
)
select FOO.code,FOO.COUNTRY_AO,FOO.CAPITAL,FOO.headofstate,count(*) COMPARED_WITH_CHEAP_ASIAN_CNT
from
(
select country_ao.code,country_ao.name COUNTRY_AO,city_ao.name CAPITAL,country_ao.headofstate
from country_ao,city_ao
where country_ao.capital = city_ao.id
and country_ao.continent = 'North America'
UNION ALL
select country_ao.code,country_ao.name COUNTRY_AO,city_ao.name CAPITAL,country_ao.headofstate
from country_ao,city_ao
where country_ao.capital = city_ao.id
and country_ao.continent = 'South America'
) FOO,countrylanguage_ao
where FOO.code = countrylanguage_ao.countrycode
group by FOO.code,FOO.COUNTRY_AO,FOO.CAPITAL,FOO.headofstate
HAVING count(*) >=
(select min(ASIAN_COUNT) FROM
(select ASIAN_COUNT FROM somecheapasiandiversecountries,country_ao
where somecheapasiandiversecountries.code = country_ao.code
and country_ao.gnp >= country_ao.gnpold
) ASIANCOUNT
)
order by COUNTRY_AO;
code | country_ao | capital | headofstate | compared_with_cheap_asian_cnt
------+----------------------------------+-------------------------+-----------------------------------+-------------------------------
ATG | Antigua and Barbuda | Saint Johns | Elisabeth II | 2
ARG | Argentina | Buenos Aires | Fernando de la Rua | 3
ABW | Aruba | Oranjestad | Beatrix | 4
BHS | Bahamas | Nassau | Elisabeth II | 2
BRB | Barbados | Bridgetown | Elisabeth II | 2
BLZ | Belize | Belmopan | Elisabeth II | 4
BOL | Bolivia | La Paz | Hugo Banzer Suarez | 4
BRA | Brazil | Brasilia | Fernando Henrique Cardoso | 5
CAN | Canada | Ottawa | Elisabeth II | 12
CHL | Chile | Santiago de Chile | Ricardo Lagos Escobar | 4
COL | Colombia | Santafe de Bogota | Andres Pastrana Arango | 5
CRI | Costa Rica | San Jose | Miguel Angel Rodriguez Echeverria | 4
DMA | Dominica | Roseau | Vernon Shaw | 2
DOM | Dominican Republic | Santo Domingo de Guzman | Hipolito Mejia Dominguez | 2
ECU | Ecuador | Quito | Gustavo Noboa Bejarano | 2
SLV | El Salvador | San Salvador | Francisco Guillermo Flores Perez | 2
GUF | French Guiana | Cayenne | Jacques Chirac | 2
GRL | Greenland | Nuuk | Margrethe II | 2
GLP | Guadeloupe | Basse-Terre | Jacques Chirac | 2
GTM | Guatemala | Ciudad de Guatemala | Alfonso Portillo Cabrera | 5
GUY | Guyana | Georgetown | Bharrat Jagdeo | 3
HTI | Haiti | Port-au-Prince | Jean-Bertrand Aristide | 2
HND | Honduras | Tegucigalpa | Carlos Roberto Flores Facusse | 4
JAM | Jamaica | Kingston | Elisabeth II | 2
MTQ | Martinique | Fort-de-France | Jacques Chirac | 2
MEX | Mexico | Ciudad de Mexico | Vicente Fox Quesada | 6
ANT | Netherlands Antilles | Willemstad | Beatrix | 3
NIC | Nicaragua | Managua | Arnoldo Aleman Lacayo | 4
PAN | Panama | Ciudad de Panama | Mireya Elisa Moscoso Rodriguez | 6
PRY | Paraguay | Asuncion | Luis Angel Gonzalez Macchi | 4
PER | Peru | Lima | Valentin Paniagua Corazao | 3
PRI | Puerto Rico | San Juan | George W. Bush | 2
KNA | Saint Kitts and Nevis | Basseterre | Elisabeth II | 2
LCA | Saint Lucia | Castries | Elisabeth II | 2
VCT | Saint Vincent and the Grenadines | Kingstown | Elisabeth II | 2
SUR | Suriname | Paramaribo | Ronald Venetiaan | 2
TTO | Trinidad and Tobago | Port-of-Spain | Arthur N. R. Robinson | 3
USA | United States | Washington | George W. Bush | 12
VEN | Venezuela | Caracas | Hugo Chavez Frias | 3
VIR | Virgin Islands, U.S. | Charlotte Amalie | George W. Bush | 3
(40 rows)
-- query 6
select count(*) from
( select r.* from
( with fact as
(
select country_ao.name as COUNTRY_AO,country_ao.code,city_ao.name as CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,AGG1.region
from
(select
sum(case when (city_ao.population >= 0.5 * country_ao.population) then country_ao.population else city_ao.population end) as S_POPULATION,
sum(case when (gnp >= gnpold) then gnp else gnpold end) as S_GNP,
avg(case when (lifeexpectancy > 60) then 50 else lifeexpectancy end) as AVG_LIFE,country_ao.region
from country_ao,city_ao
where governmentform != 'Constitutional Monarchy'
and country_ao.capital = city_ao.id
and indepyear > 0
group by country_ao.region) AGG1
,country_ao,city_ao
where country_ao.capital = city_ao.id
and country_ao.region = AGG1.region
)
select code,COUNTRY_AO,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage_ao
where fact.code = countrylanguage_ao.countrycode and isofficial = 'True'
and fact.region = 'South America'
UNION ALL
select code,COUNTRY_AO,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage_ao
where fact.code = countrylanguage_ao.countrycode and isofficial = 'True'
and fact.region = 'North America'
UNION ALL
select code,COUNTRY_AO,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage_ao
where fact.code = countrylanguage_ao.countrycode and isofficial = 'True'
and fact.region = 'Caribbean'
) as r
left join
(
select 'ARG' as CODE UNION ALL
select 'BOL' as CODE UNION ALL
select 'BRA' as CODE UNION ALL
select 'PER' as CODE UNION ALL
select 'URY' as CODE UNION ALL
select 'IND' as CODE UNION ALL
select 'LCA' as CODE UNION ALL
select 'VCT' as CODE
) as r1
on r.code = r1.code) AS FOO;
count
-------
43
(1 row)
-- query7
with alleuropeanlanguages as
(select country_ao.code,country_ao.name COUNTRY_AO, city_ao.name CAPITAL, language, isofficial, percentage
FROM country_ao,city_ao,countrylanguage_ao
WHERE country_ao.code = countrylanguage_ao.countrycode
and country_ao.capital = city_ao.id
and country_ao.continent = 'Europe')
select * from
(select * from alleuropeanlanguages where isofficial='True') e1,
(select * from alleuropeanlanguages where percentage > 50) e2
where e1.code = e2.code order by e2.COUNTRY_AO,e1.language;
code | country_ao | capital | language | isofficial | percentage | code | country_ao | capital | language | isofficial | percentage
------+------------------------+-----------------------------------+----------------+------------+------------+------+------------------------+-----------------------------------+----------------+------------+------------
ALB | Albania | Tirana | Albaniana | t | 97.9 | ALB | Albania | Tirana | Albaniana | t | 97.9
AUT | Austria | Wien | German | t | 92 | AUT | Austria | Wien | German | t | 92
BLR | Belarus | Minsk | Belorussian | t | 65.6 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BLR | Belarus | Minsk | Russian | t | 32 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | French | t | 32.6 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | German | t | 1 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2 | BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2
BGR | Bulgaria | Sofija | Bulgariana | t | 83.2 | BGR | Bulgaria | Sofija | Bulgariana | t | 83.2
HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9 | HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Praha | Czech | t | 81.2
DNK | Denmark | Kobenhavn | Danish | t | 93.5 | DNK | Denmark | Kobenhavn | Danish | t | 93.5
EST | Estonia | Tallinn | Estonian | t | 65.3 | EST | Estonia | Tallinn | Estonian | t | 65.3
FRO | Faroe Islands | Torshavn | Danish | t | 0 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FRO | Faroe Islands | Torshavn | Faroese | t | 100 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FIN | Finland | Helsinki [Helsingfors] | Swedish | t | 5.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FRA | France | Paris | French | t | 93.6 | FRA | France | Paris | French | t | 93.6
DEU | Germany | Berlin | German | t | 91.3 | DEU | Germany | Berlin | German | t | 91.3
GIB | Gibraltar | Gibraltar | English | t | 88.9 | GIB | Gibraltar | Gibraltar | English | t | 88.9
GRC | Greece | Athenai | Greek | t | 98.5 | GRC | Greece | Athenai | Greek | t | 98.5
HUN | Hungary | Budapest | Hungarian | t | 98.5 | HUN | Hungary | Budapest | Hungarian | t | 98.5
ISL | Iceland | Reykjavik | Icelandic | t | 95.7 | ISL | Iceland | Reykjavik | Icelandic | t | 95.7
IRL | Ireland | Dublin | English | t | 98.4 | IRL | Ireland | Dublin | English | t | 98.4
IRL | Ireland | Dublin | Irish | t | 1.6 | IRL | Ireland | Dublin | English | t | 98.4
ITA | Italy | Roma | Italian | t | 94.1 | ITA | Italy | Roma | Italian | t | 94.1
LVA | Latvia | Riga | Latvian | t | 55.1 | LVA | Latvia | Riga | Latvian | t | 55.1
LIE | Liechtenstein | Vaduz | German | t | 89 | LIE | Liechtenstein | Vaduz | German | t | 89
LTU | Lithuania | Vilnius | Lithuanian | t | 81.6 | LTU | Lithuania | Vilnius | Lithuanian | t | 81.6
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | French | t | 4.2 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | German | t | 2.3 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
MKD | Macedonia | Skopje | Macedonian | t | 66.5 | MKD | Macedonia | Skopje | Macedonian | t | 66.5
MLT | Malta | Valletta | English | t | 2.1 | MLT | Malta | Valletta | Maltese | t | 95.8
MLT | Malta | Valletta | Maltese | t | 95.8 | MLT | Malta | Valletta | Maltese | t | 95.8
MDA | Moldova | Chisinau | Romanian | t | 61.9 | MDA | Moldova | Chisinau | Romanian | t | 61.9
NLD | Netherlands | Amsterdam | Dutch | t | 95.6 | NLD | Netherlands | Amsterdam | Dutch | t | 95.6
NOR | Norway | Oslo | Norwegian | t | 96.6 | NOR | Norway | Oslo | Norwegian | t | 96.6
POL | Poland | Warszawa | Polish | t | 97.6 | POL | Poland | Warszawa | Polish | t | 97.6
PRT | Portugal | Lisboa | Portuguese | t | 99 | PRT | Portugal | Lisboa | Portuguese | t | 99
ROM | Romania | Bucuresti | Romani | t | 0.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
ROM | Romania | Bucuresti | Romanian | t | 90.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
RUS | Russian Federation | Moscow | Russian | t | 86.6 | RUS | Russian Federation | Moscow | Russian | t | 86.6
SMR | San Marino | San Marino | Italian | t | 100 | SMR | San Marino | San Marino | Italian | t | 100
SVK | Slovakia | Bratislava | Slovak | t | 85.6 | SVK | Slovakia | Bratislava | Slovak | t | 85.6
SVN | Slovenia | Ljubljana | Slovene | t | 87.9 | SVN | Slovenia | Ljubljana | Slovene | t | 87.9
ESP | Spain | Madrid | Spanish | t | 74.4 | ESP | Spain | Madrid | Spanish | t | 74.4
SWE | Sweden | Stockholm | Swedish | t | 89.5 | SWE | Sweden | Stockholm | Swedish | t | 89.5
CHE | Switzerland | Bern | French | t | 19.2 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | German | t | 63.6 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Italian | t | 7.7 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Romansh | t | 0.6 | CHE | Switzerland | Bern | German | t | 63.6
UKR | Ukraine | Kyiv | Ukrainian | t | 64.7 | UKR | Ukraine | Kyiv | Ukrainian | t | 64.7
GBR | United Kingdom | London | English | t | 97.3 | GBR | United Kingdom | London | English | t | 97.3
YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2 | YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2
(55 rows)
-- query8
with allcountry_aostats as
( select country_ao.code,country_ao.name,count(distinct city_ao.id) CITY_AO_CNT,
count(distinct countrylanguage_ao.language) LANG_CNT
from country_ao,city_ao,countrylanguage_ao
where country_ao.code = city_ao.countrycode
and country_ao.code = countrylanguage_ao.countrycode
group by country_ao.code,country_ao.name
)
select sum(FOO.CITY_AO_CNT) REGION_CITY_AO_CNT,sum(FOO.LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountry_aostats.code,allcountry_aostats.name COUNTRY_AO,CITY_AO_CNT,LANG_CNT,country_ao.region,city_ao.name CAPITAL
from allcountry_aostats,country_ao,city_ao
where allcountry_aostats.code = country_ao.code
and country_ao.capital = city_ao.id
and CITY_AO_CNT/LANG_CNT > 1
and country_ao.continent = 'Asia'
UNION ALL
select allcountry_aostats.code,allcountry_aostats.name COUNTRY_AO,CITY_AO_CNT,LANG_CNT,country_ao.region,city_ao.name CAPITAL
from allcountry_aostats,country_ao,city_ao
where allcountry_aostats.code = country_ao.code
and country_ao.capital = city_ao.id
and CITY_AO_CNT/LANG_CNT > 1
and country_ao.continent = 'North America'
UNION ALL
select allcountry_aostats.code,allcountry_aostats.name COUNTRY_AO,CITY_AO_CNT,LANG_CNT,country_ao.region,city_ao.name CAPITAL
from allcountry_aostats,country_ao,city_ao
where allcountry_aostats.code = country_ao.code
and country_ao.capital = city_ao.id
and CITY_AO_CNT/LANG_CNT > (select max(CITY_AO_CNT/LANG_CNT) from allcountry_aostats,country_ao where allcountry_aostats.code = country_ao.code AND country_ao.continent='Europe')
) FOO
,allcountry_aostats,country_ao
WHERE allcountry_aostats.code = country_ao.code
and FOO.region = country_ao.region
group by FOO.region order by FOO.region;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
region_city_ao_cnt | region_lang_cnt | region
--------------------+-----------------+---------------------------
840 | 192 | Caribbean
2824 | 112 | Central America
11336 | 384 | Eastern Asia
2664 | 396 | Middle East
1625 | 125 | North America
3500 | 70 | South America
3179 | 528 | Southeast Asia
12278 | 896 | Southern and Central Asia
(8 rows)
-- some queries with hash joins and index scans
set enable_nestloop=off;
set enable_hashjoin=on;
set enable_mergejoin=off;
--query 9
with diversecountries as
(select country_ao.code,country_ao.name,country_ao.capital,d.CNT
from country_ao,
(select countrylanguage_ao.countrycode,count(*) as CNT from countrylanguage_ao group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country_ao.code and country_ao.gnp > 100000)
select d1.code,d1.name,d1.capital,city_ao.name CAPITAL_CITY_AO,d1.CNT,d2.CNT
from
diversecountries d1 left join country_ao
ON (d1.code = country_ao.code AND d1.CNT < 8)
left join diversecountries d2
ON (country_ao.code = d2.code AND d2.CNT > 8)
INNER JOIN city_ao
ON(d1.capital = city_ao.id)
ORDER BY d1.name;
code | name | capital | capital_city_ao | cnt | cnt
------+--------------------+---------+------------------+-----+-----
AUS | Australia | 135 | Canberra | 8 |
AUT | Austria | 1523 | Wien | 8 |
CAN | Canada | 1822 | Ottawa | 12 |
CHN | China | 1891 | Peking | 12 |
DNK | Denmark | 3315 | Kobenhavn | 7 |
IND | India | 1109 | New Delhi | 12 |
IRN | Iran | 1380 | Teheran | 10 |
ITA | Italy | 1464 | Roma | 8 |
MMR | Myanmar | 2710 | Rangoon (Yangon) | 8 |
RUS | Russian Federation | 3580 | Moscow | 12 |
ZAF | South Africa | 716 | Pretoria | 11 |
USA | United States | 3813 | Washington | 12 |
(12 rows)
--query 10 , multiple ctes, joins
with longlivingregions as
(
select FOO.*,count(distinct language) as "lang_count"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
avg(lifeexpectancy) as "REGION_LIFETIME",region
from
country_ao
group by region
) FOO,countrylanguage_ao,country_ao
where
country_ao.code = countrylanguage_ao.countrycode
and FOO.region = country_ao.region
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP",foo."REGION_LIFETIME"),
denseregions as
(
select FOO.*,count(distinct language) as "lang_count",
sum(surfacearea) as "REGION_SURFACE_AREA"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
region
from
country_ao
group by region
) FOO,countrylanguage_ao,country_ao
where
country_ao.code = countrylanguage_ao.countrycode
and FOO.region = country_ao.region
and FOO."REGION_POP" != 0
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP"
order by sum(surfacearea)/foo."REGION_POP" desc),
allcountry_aostats as
( select country_ao.code,country_ao.name,count(distinct city_ao.id) CITY_AO_CNT,
count(distinct countrylanguage_ao.language) LANG_CNT
from country_ao,city_ao,countrylanguage_ao
where country_ao.code = city_ao.countrycode
and country_ao.code = countrylanguage_ao.countrycode
group by country_ao.code,country_ao.name
)
select allcountry_aostats.CITY_AO_CNT,allcountry_aostats.LANG_CNT,allcountry_aostats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountry_aostats,country_ao
where longlivingregions.region = denseregions.region and allcountry_aostats.code = country_ao.code and country_ao.region = longlivingregions.region
and country_ao.indepyear between 1800 and 1850
UNION ALL
select allcountry_aostats.CITY_AO_CNT,allcountry_aostats.LANG_CNT,allcountry_aostats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountry_aostats,country_ao
where longlivingregions.region = denseregions.region and allcountry_aostats.code = country_ao.code and country_ao.region = longlivingregions.region
and country_ao.indepyear between 1850 and 1900
UNION ALL
select allcountry_aostats.CITY_AO_CNT,allcountry_aostats.LANG_CNT,allcountry_aostats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountry_aostats,country_ao
where longlivingregions.region = denseregions.region and allcountry_aostats.code = country_ao.code and country_ao.region = longlivingregions.region
and country_ao.indepyear > 1900
order by name
LIMIT 50;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
city_ao_cnt | lang_cnt | name | REGION_SURFACE_AREA | REGION_LIFETIME | REGION_POP | lang_count | REGION_GNP | region
-------------+----------+---------------------------------------+---------------------+------------------+------------+------------+------------+---------------------------
4 | 5 | Afghanistan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 3 | Albania | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
18 | 2 | Algeria | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
5 | 9 | Angola | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 2 | Antigua and Barbuda | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
57 | 3 | Argentina | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 2 | Armenia | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
14 | 8 | Australia | 62471162.00 | 78.8000030517578 | 22753100 | 11 | 405851.00 | Australia and New Zealand
6 | 8 | Austria | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
4 | 4 | Azerbaijan | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
1 | 2 | Bahamas | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
1 | 2 | Bahrain | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
24 | 7 | Bangladesh | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 2 | Barbados | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
16 | 4 | Belarus | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
9 | 6 | Belgium | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
2 | 4 | Belize | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
4 | 7 | Benin | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Bhutan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
8 | 4 | Bolivia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 1 | Bosnia and Herzegovina | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
2 | 5 | Botswana | 23060186.00 | 44.8199996948242 | 46886000 | 21 | 126931.00 | Southern Africa
250 | 5 | Brazil | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 4 | Brunei | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
10 | 4 | Bulgaria | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
3 | 6 | Burkina Faso | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Burundi | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
3 | 4 | Cambodia | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
7 | 8 | Cameroon | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
49 | 12 | Canada | 236342035.00 | 75.8199996948242 | 309632000 | 18 | 9111890.00 | North America
1 | 2 | Cape Verde | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 6 | Central African Republic | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
2 | 8 | Chad | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
29 | 4 | Chile | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
38 | 5 | Colombia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 5 | Comoros | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
2 | 6 | Congo | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
18 | 10 | Congo, The Democratic Republic of the | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 4 | Costa Rica | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
5 | 5 | Cote deIvoire | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
4 | 2 | Croatia | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
14 | 1 | Cuba | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
2 | 2 | Cyprus | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
10 | 8 | Czech Republic | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
1 | 3 | Djibouti | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
1 | 2 | Dominica | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
6 | 2 | Dominican Republic | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
15 | 2 | Ecuador | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
37 | 2 | Egypt | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
7 | 2 | El Salvador | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
(50 rows)
--query 11
with allcity_aostats as
( select city_ao.name CITY_AO,city_ao.id,country_ao.name COUNTRY_AO,city_ao.district,city_ao.population as CITY_AO_POP
from
city_ao,country_ao
where city_ao.countrycode = country_ao.code
),
alldistrictstats as
( select allcity_aostats.district,allcity_aostats.COUNTRY_AO,sum(CITY_AO_POP) DISTRICT_POP,
count(CITY_AO) as D_CITY_AO_CNT
from allcity_aostats
group by allcity_aostats.district,allcity_aostats.COUNTRY_AO
order by district,COUNTRY_AO
),
allcountry_aostats as
( select alldistrictstats.COUNTRY_AO,country_ao.code,sum(D_CITY_AO_CNT) C_CITY_AO_CNT,
count(distinct countrylanguage_ao.language) C_LANG_CNT
from alldistrictstats,country_ao,countrylanguage_ao
where alldistrictstats.COUNTRY_AO = country_ao.name
and country_ao.code = countrylanguage_ao.countrycode
group by COUNTRY_AO,code
),
asian_region_stats as
(
select sum(FOO.C_CITY_AO_CNT) REGION_CITY_AO_CNT,sum(FOO.C_LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountry_aostats.code,allcountry_aostats.COUNTRY_AO,C_CITY_AO_CNT,C_LANG_CNT,country_ao.region,city_ao.name CAPITAL
from allcountry_aostats,country_ao,city_ao
where allcountry_aostats.code = country_ao.code
and country_ao.capital = city_ao.id
and C_CITY_AO_CNT/C_LANG_CNT > 1
and country_ao.continent = 'Asia') FOO
,allcountry_aostats,country_ao
WHERE allcountry_aostats.code = country_ao.code
and FOO.region = country_ao.region
group by FOO.region order by FOO.region
)
select * from
(
select REGION_CITY_AO_CNT as CITY_AO_CNT,REGION_LANG_CNT as LANG_CNT, region as IDENTIFIER from asian_region_stats
UNION ALL
(
select sum(FOO.C_CITY_AO_CNT) CITY_AO_CNT,sum(FOO.C_LANG_CNT) LANG_CNT,FOO.region as IDENTIFIER
FROM
(
select allcountry_aostats.code,allcountry_aostats.COUNTRY_AO,C_CITY_AO_CNT,C_LANG_CNT,country_ao.region,allcity_aostats.CITY_AO CAPITAL
from allcountry_aostats,country_ao,allcity_aostats
where allcountry_aostats.code = country_ao.code
and country_ao.capital = allcity_aostats.id
and C_CITY_AO_CNT/C_LANG_CNT > 1
and country_ao.continent = 'Europe') FOO
,allcountry_aostats,country_ao
WHERE allcountry_aostats.code = country_ao.code
and FOO.region = country_ao.region
group by FOO.region order by FOO.region
)
) FOO1
order by FOO1.lang_cnt,FOO1.identifier;
city_ao_cnt | lang_cnt | identifier
-------------+----------+---------------------------
494 | 10 | British Islands
159 | 48 | Baltic Countries
1295 | 161 | Nordic Countries
50176 | 264 | Eastern Asia
9414 | 369 | Western Europe
11880 | 450 | Southern Europe
32900 | 610 | Eastern Europe
29161 | 616 | Southeast Asia
8568 | 792 | Middle East
80388 | 1092 | Southern and Central Asia
(10 rows)
-- queries with CTEs on CO tables
DROP TABLE IF EXISTS CITY_CO;
NOTICE: table "city_co" does not exist, skipping
CREATE TABLE CITY_CO (
id integer NOT NULL,
name text NOT NULL,
countrycode character(3) NOT NULL,
district text NOT NULL,
population integer NOT NULL
) with (appendonly=true,orientation=column) distributed by(id);
DROP TABLE IF EXISTS country_co;
NOTICE: table "country_co" does not exist, skipping
CREATE TABLE country_co (
code character(3) NOT NULL,
name text NOT NULL,
continent text NOT NULL,
region text NOT NULL,
surfacearea numeric(10,2) NOT NULL,
indepyear smallint,
population integer NOT NULL,
lifeexpectancy real,
gnp numeric(10,2),
gnpold numeric(10,2),
localname text NOT NULL,
governmentform text NOT NULL,
headofstate text,
capital integer,
code2 character(2) NOT NULL
) with (appendonly=true,orientation=column) distributed by (code);
DROP TABLE IF EXISTS countrylanguage_co;
NOTICE: table "countrylanguage_co" does not exist, skipping
CREATE TABLE countrylanguage_co (
countrycode character(3) NOT NULL,
"language" text NOT NULL,
isofficial boolean NOT NULL,
percentage real NOT NULL
) with (appendonly=true,orientation=column) distributed by (countrycode,language);
create index bitmap_city_co_countrycode on city_co using bitmap(countrycode);
create index bitmap_country_co_gf on country_co using bitmap(governmentform);
create index bitmap_country_co_region on country_co using bitmap(region);
create index bitmap_country_co_continent on country_co using bitmap(continent);
create index bitmap_countrylanguage_co_countrycode on countrylanguage_co using bitmap(countrycode);
INSERT INTO CITY_CO SELECT * FROM CITY;
INSERT INTO COUNTRY_CO SELECT * FROM COUNTRY;
INSERT INTO COUNTRYLANGUAGE_CO SELECT * FROM COUNTRYLANGUAGE;
ANALYZE CITY_CO;
ANALYZE COUNTRY_CO;
ANALYZE COUNTRYLANGUAGE_CO;
set enable_seqscan=off;
set enable_indexscan=on;
--query1
with capitals as
(select country_co.code,id,city_co.name from city_co,country_co
where city_co.countrycode = country_co.code AND city_co.id = country_co.capital)
select * from
capitals,countrylanguage_co
where capitals.code = countrylanguage_co.countrycode and isofficial='true'
order by capitals.code,countrylanguage_co.language;
code | id | name | countrycode | language | isofficial | percentage
------+------+-----------------------------------+-------------+------------------+------------+------------
ABW | 129 | Oranjestad | ABW | Dutch | t | 5.3
AFG | 1 | Kabul | AFG | Dari | t | 32.1
AFG | 1 | Kabul | AFG | Pashto | t | 52.4
AIA | 62 | The Valley | AIA | English | t | 0
ALB | 34 | Tirana | ALB | Albaniana | t | 97.9
AND | 55 | Andorra la Vella | AND | Catalan | t | 32.3
ANT | 33 | Willemstad | ANT | Dutch | t | 0
ANT | 33 | Willemstad | ANT | Papiamento | t | 86.2
ARE | 65 | Abu Dhabi | ARE | Arabic | t | 42
ARG | 69 | Buenos Aires | ARG | Spanish | t | 96.8
ARM | 126 | Yerevan | ARM | Armenian | t | 93.4
ASM | 54 | Fagatogo | ASM | English | t | 3.1
ASM | 54 | Fagatogo | ASM | Samoan | t | 90.6
ATG | 63 | Saint Johns | ATG | English | t | 0
AUS | 135 | Canberra | AUS | English | t | 81.2
AUT | 1523 | Wien | AUT | German | t | 92
AZE | 144 | Baku | AZE | Azerbaijani | t | 89
BDI | 552 | Bujumbura | BDI | French | t | 0
BDI | 552 | Bujumbura | BDI | Kirundi | t | 98.1
BEL | 179 | Bruxelles [Brussel] | BEL | Dutch | t | 59.2
BEL | 179 | Bruxelles [Brussel] | BEL | French | t | 32.6
BEL | 179 | Bruxelles [Brussel] | BEL | German | t | 1
BGD | 150 | Dhaka | BGD | Bengali | t | 97.7
BGR | 539 | Sofija | BGR | Bulgariana | t | 83.2
BHR | 149 | al-Manama | BHR | Arabic | t | 67.7
BIH | 201 | Sarajevo | BIH | Serbo-Croatian | t | 99.2
BLR | 3520 | Minsk | BLR | Belorussian | t | 65.6
BLR | 3520 | Minsk | BLR | Russian | t | 32
BLZ | 185 | Belmopan | BLZ | English | t | 50.8
BMU | 191 | Hamilton | BMU | English | t | 100
BOL | 194 | La Paz | BOL | Aimara | t | 3.2
BOL | 194 | La Paz | BOL | Ketdua | t | 8.1
BOL | 194 | La Paz | BOL | Spanish | t | 87.7
BRA | 211 | Brasilia | BRA | Portuguese | t | 97.5
BRB | 174 | Bridgetown | BRB | English | t | 0
BRN | 538 | Bandar Seri Begawan | BRN | Malay | t | 45.5
BTN | 192 | Thimphu | BTN | Dzongkha | t | 50
CAN | 1822 | Ottawa | CAN | English | t | 60.4
CAN | 1822 | Ottawa | CAN | French | t | 23.4
CCK | 2317 | West Island | CCK | English | t | 0
CHE | 3248 | Bern | CHE | French | t | 19.2
CHE | 3248 | Bern | CHE | German | t | 63.6
CHE | 3248 | Bern | CHE | Italian | t | 7.7
CHE | 3248 | Bern | CHE | Romansh | t | 0.6
CHL | 554 | Santiago de Chile | CHL | Spanish | t | 89.7
CHN | 1891 | Peking | CHN | Chinese | t | 92
COK | 583 | Avarua | COK | Maori | t | 0
COL | 2257 | Santafe de Bogota | COL | Spanish | t | 99
COM | 2295 | Moroni | COM | Comorian | t | 75
CPV | 1859 | Praia | CPV | Portuguese | t | 0
CRI | 584 | San Jose | CRI | Spanish | t | 97.5
CUB | 2413 | La Habana | CUB | Spanish | t | 100
CXR | 1791 | Flying Fish Cove | CXR | English | t | 0
CYM | 553 | George Town | CYM | English | t | 0
CYP | 2430 | Nicosia | CYP | Greek | t | 74.1
CYP | 2430 | Nicosia | CYP | Turkish | t | 22.4
CZE | 3339 | Praha | CZE | Czech | t | 81.2
DEU | 3068 | Berlin | DEU | German | t | 91.3
DJI | 585 | Djibouti | DJI | Arabic | t | 10.6
DNK | 3315 | Kobenhavn | DNK | Danish | t | 93.5
DOM | 587 | Santo Domingo de Guzman | DOM | Spanish | t | 98
DZA | 35 | Alger | DZA | Arabic | t | 86
ECU | 594 | Quito | ECU | Spanish | t | 93
EGY | 608 | Cairo | EGY | Arabic | t | 98.8
ERI | 652 | Asmara | ERI | Tigrinja | t | 49.1
ESH | 2453 | El-Aaiun | ESH | Arabic | t | 100
ESP | 653 | Madrid | ESP | Spanish | t | 74.4
EST | 3791 | Tallinn | EST | Estonian | t | 65.3
FIN | 3236 | Helsinki [Helsingfors] | FIN | Finnish | t | 92.7
FIN | 3236 | Helsinki [Helsingfors] | FIN | Swedish | t | 5.7
FJI | 764 | Suva | FJI | Fijian | t | 50.8
FLK | 763 | Stanley | FLK | English | t | 0
FRA | 2974 | Paris | FRA | French | t | 93.6
FRO | 901 | Torshavn | FRO | Danish | t | 0
FRO | 901 | Torshavn | FRO | Faroese | t | 100
GBR | 456 | London | GBR | English | t | 97.3
GEO | 905 | Tbilisi | GEO | Georgiana | t | 71.7
GIB | 915 | Gibraltar | GIB | English | t | 88.9
GLP | 919 | Basse-Terre | GLP | French | t | 0
GNB | 927 | Bissau | GNB | Portuguese | t | 8.1
GRC | 2401 | Athenai | GRC | Greek | t | 98.5
GRL | 917 | Nuuk | GRL | Danish | t | 12.5
GRL | 917 | Nuuk | GRL | Greenlandic | t | 87.5
GTM | 922 | Ciudad de Guatemala | GTM | Spanish | t | 64.7
GUM | 921 | Agaaa | GUM | Chamorro | t | 29.6
GUM | 921 | Agaaa | GUM | English | t | 37.5
HKG | 937 | Victoria | HKG | English | t | 2.2
HND | 933 | Tegucigalpa | HND | Spanish | t | 97.2
HRV | 2409 | Zagreb | HRV | Serbo-Croatian | t | 95.9
HTI | 929 | Port-au-Prince | HTI | French | t | 0
HUN | 3483 | Budapest | HUN | Hungarian | t | 98.5
IDN | 939 | Jakarta | IDN | Malay | t | 12.1
IND | 1109 | New Delhi | IND | Hindi | t | 39.9
IRL | 1447 | Dublin | IRL | English | t | 98.4
IRL | 1447 | Dublin | IRL | Irish | t | 1.6
IRN | 1380 | Teheran | IRN | Persian | t | 45.7
IRQ | 1365 | Baghdad | IRQ | Arabic | t | 77.2
ISL | 1449 | Reykjavik | ISL | Icelandic | t | 95.7
ISR | 1450 | Jerusalem | ISR | Arabic | t | 18
ISR | 1450 | Jerusalem | ISR | Hebrew | t | 63.1
ITA | 1464 | Roma | ITA | Italian | t | 94.1
JOR | 1786 | Amman | JOR | Arabic | t | 97.9
JPN | 1532 | Tokyo | JPN | Japanese | t | 99.1
KAZ | 1864 | Astana | KAZ | Kazakh | t | 46
KGZ | 2253 | Bishkek | KGZ | Kirgiz | t | 59.7
KGZ | 2253 | Bishkek | KGZ | Russian | t | 16.2
KHM | 1800 | Phnom Penh | KHM | Khmer | t | 88.6
KIR | 2256 | Bairiki | KIR | Kiribati | t | 98.9
KNA | 3064 | Basseterre | KNA | English | t | 0
KOR | 2331 | Seoul | KOR | Korean | t | 99.9
KWT | 2429 | Kuwait | KWT | Arabic | t | 78.1
LAO | 2432 | Vientiane | LAO | Lao | t | 67.2
LBN | 2438 | Beirut | LBN | Arabic | t | 93
LBY | 2441 | Tripoli | LBY | Arabic | t | 96
LCA | 3065 | Castries | LCA | English | t | 20
LIE | 2446 | Vaduz | LIE | German | t | 89
LKA | 3217 | Colombo | LKA | Singali | t | 60.3
LKA | 3217 | Colombo | LKA | Tamil | t | 19.6
LSO | 2437 | Maseru | LSO | English | t | 0
LSO | 2437 | Maseru | LSO | Sotho | t | 85
LTU | 2447 | Vilnius | LTU | Lithuanian | t | 81.6
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | French | t | 4.2
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | German | t | 2.3
LUX | 2452 | Luxembourg [Luxemburg/Letzebuerg] | LUX | Luxembourgish | t | 64.4
LVA | 2434 | Riga | LVA | Latvian | t | 55.1
MAC | 2454 | Macao | MAC | Portuguese | t | 2.3
MAR | 2486 | Rabat | MAR | Arabic | t | 65
MCO | 2695 | Monaco-Ville | MCO | French | t | 41.9
MDA | 2690 | Chisinau | MDA | Romanian | t | 61.9
MDG | 2455 | Antananarivo | MDG | French | t | 0
MDG | 2455 | Antananarivo | MDG | Malagasy | t | 98.9
MDV | 2463 | Male | MDV | Dhivehi | t | 100
MEX | 2515 | Ciudad de Mexico | MEX | Spanish | t | 92.1
MHL | 2507 | Dalap-Uliga-Darrit | MHL | English | t | 0
MHL | 2507 | Dalap-Uliga-Darrit | MHL | Marshallese | t | 96.8
MKD | 2460 | Skopje | MKD | Macedonian | t | 66.5
MLT | 2484 | Valletta | MLT | English | t | 2.1
MLT | 2484 | Valletta | MLT | Maltese | t | 95.8
MMR | 2710 | Rangoon (Yangon) | MMR | Burmese | t | 69
MNG | 2696 | Ulan Bator | MNG | Mongolian | t | 78.8
MNP | 2913 | Garapan | MNP | English | t | 4.8
MSR | 2697 | Plymouth | MSR | English | t | 0
MTQ | 2508 | Fort-de-France | MTQ | French | t | 0
MWI | 2462 | Lilongwe | MWI | Chichewa | t | 58.3
MYS | 2464 | Kuala Lumpur | MYS | Malay | t | 58.4
MYT | 2514 | Mamoutzou | MYT | French | t | 20.3
NCL | 3493 | Noumea | NCL | French | t | 34.3
NFK | 2806 | Kingston | NFK | English | t | 0
NIC | 2734 | Managua | NIC | Spanish | t | 97.6
NIU | 2805 | Alofi | NIU | English | t | 0
NLD | 5 | Amsterdam | NLD | Dutch | t | 95.6
NOR | 2807 | Oslo | NOR | Norwegian | t | 96.6
NPL | 2729 | Kathmandu | NPL | Nepali | t | 50.4
NRU | 2728 | Yaren | NRU | English | t | 7.5
NRU | 2728 | Yaren | NRU | Nauru | t | 57.5
NZL | 3499 | Wellington | NZL | English | t | 87
OMN | 2821 | Masqat | OMN | Arabic | t | 76.7
PAK | 2831 | Islamabad | PAK | Urdu | t | 7.6
PAN | 2882 | Ciudad de Panama | PAN | Spanish | t | 76.8
PER | 2890 | Lima | PER | Aimara | t | 2.3
PER | 2890 | Lima | PER | Ketdua | t | 16.4
PER | 2890 | Lima | PER | Spanish | t | 79.8
PHL | 766 | Manila | PHL | Pilipino | t | 29.3
PLW | 2881 | Koror | PLW | English | t | 3.2
PLW | 2881 | Koror | PLW | Palau | t | 82.2
POL | 2928 | Warszawa | POL | Polish | t | 97.6
PRI | 2919 | San Juan | PRI | Spanish | t | 51.3
PRK | 2318 | Pyongyang | PRK | Korean | t | 99.9
PRT | 2914 | Lisboa | PRT | Portuguese | t | 99
PRY | 2885 | Asuncion | PRY | Guarani | t | 40.1
PRY | 2885 | Asuncion | PRY | Spanish | t | 55.1
PYF | 3016 | Papeete | PYF | French | t | 40.8
QAT | 2973 | Doha | QAT | Arabic | t | 40.7
ROM | 3018 | Bucuresti | ROM | Romani | t | 0.7
ROM | 3018 | Bucuresti | ROM | Romanian | t | 90.7
RUS | 3580 | Moscow | RUS | Russian | t | 86.6
RWA | 3047 | Kigali | RWA | French | t | 0
RWA | 3047 | Kigali | RWA | Rwanda | t | 100
SAU | 3173 | Riyadh | SAU | Arabic | t | 95
SDN | 3225 | Khartum | SDN | Arabic | t | 49.4
SEN | 3198 | Dakar | SEN | Wolof | t | 48.1
SGP | 3208 | Singapore | SGP | Chinese | t | 77.1
SGP | 3208 | Singapore | SGP | Malay | t | 14.1
SGP | 3208 | Singapore | SGP | Tamil | t | 7.4
SHN | 3063 | Jamestown | SHN | English | t | 0
SJM | 938 | Longyearbyen | SJM | Norwegian | t | 0
SLV | 645 | San Salvador | SLV | Spanish | t | 100
SMR | 3171 | San Marino | SMR | Italian | t | 100
SOM | 3214 | Mogadishu | SOM | Arabic | t | 0
SOM | 3214 | Mogadishu | SOM | Somali | t | 98.3
SPM | 3067 | Saint-Pierre | SPM | French | t | 0
SVK | 3209 | Bratislava | SVK | Slovak | t | 85.6
SVN | 3212 | Ljubljana | SVN | Slovene | t | 87.9
SWE | 3048 | Stockholm | SWE | Swedish | t | 89.5
SWZ | 3244 | Mbabane | SWZ | Swazi | t | 89.9
SYC | 3206 | Victoria | SYC | English | t | 3.8
SYC | 3206 | Victoria | SYC | French | t | 1.3
SYR | 3250 | Damascus | SYR | Arabic | t | 90
TCA | 3423 | Cockburn Town | TCA | English | t | 0
TCD | 3337 | NDjamena | TCD | Arabic | t | 12.3
TGO | 3332 | Lome | TGO | Ewe | t | 23.2
TGO | 3332 | Lome | TGO | Kabye | t | 13.8
THA | 3320 | Bangkok | THA | Thai | t | 52.6
TJK | 3261 | Dushanbe | TJK | Tadzhik | t | 62.2
TKL | 3333 | Fakaofo | TKL | English | t | 0
TKM | 3419 | Ashgabat | TKM | Turkmenian | t | 76.7
TMP | 1522 | Dili | TMP | Portuguese | t | 0
TON | 3334 | Nukualofa | TON | English | t | 0
TON | 3334 | Nukualofa | TON | Tongan | t | 98.3
TUN | 3349 | Tunis | TUN | Arabic | t | 69.9
TUR | 3358 | Ankara | TUR | Turkish | t | 87.6
TUV | 3424 | Funafuti | TUV | English | t | 0
TUV | 3424 | Funafuti | TUV | Tuvalu | t | 92.5
TWN | 3263 | Taipei | TWN | Mandarin Chinese | t | 20.1
TZA | 3306 | Dodoma | TZA | Swahili | t | 8.8
UKR | 3426 | Kyiv | UKR | Ukrainian | t | 64.7
URY | 3492 | Montevideo | URY | Spanish | t | 95.7
USA | 3813 | Washington | USA | English | t | 86.2
UZB | 3503 | Toskent | UZB | Uzbek | t | 72.6
VAT | 3538 | Citta del Vaticano | VAT | Italian | t | 0
VCT | 3066 | Kingstown | VCT | English | t | 0
VEN | 3539 | Caracas | VEN | Spanish | t | 96.9
VGB | 537 | Road Town | VGB | English | t | 0
VIR | 4067 | Charlotte Amalie | VIR | English | t | 81.7
VNM | 3770 | Hanoi | VNM | Vietnamese | t | 86.8
VUT | 3537 | Port-Vila | VUT | Bislama | t | 56.6
VUT | 3537 | Port-Vila | VUT | English | t | 28.3
VUT | 3537 | Port-Vila | VUT | French | t | 14.2
WSM | 3169 | Apia | WSM | English | t | 0.6
WSM | 3169 | Apia | WSM | Samoan | t | 47.5
YEM | 1780 | Sanaa | YEM | Arabic | t | 99.6
YUG | 1792 | Beograd | YUG | Serbo-Croatian | t | 75.2
ZAF | 716 | Pretoria | ZAF | Afrikaans | t | 14.3
ZAF | 716 | Pretoria | ZAF | English | t | 8.5
ZAF | 716 | Pretoria | ZAF | Xhosa | t | 17.7
ZAF | 716 | Pretoria | ZAF | Zulu | t | 22.7
ZWE | 4068 | Harare | ZWE | English | t | 2.2
(237 rows)
--query2
with lang_total as
( select count(*) as lang_count,country_co.code,countrylanguage_co.countrycode
from country_co join countrylanguage_co on (country_co.code=countrylanguage_co.countrycode and governmentform='Federal Republic')
group by country_co.code,countrylanguage_co.countrycode order by country_co.code)
select lang_count,country_co.code,country_co.name,country_co.continent,country_co.region,country_co.population
from country_co left outer join lang_total
on (lang_total.code = country_co.code)
where country_co.indepyear > 1970 and lang_total.lang_count >=1 and lang_total.lang_count >=1
order by lang_total.lang_count desc;
lang_count | code | name | continent | region | population
------------+------+---------------------------------+-----------+-----------------+------------
12 | RUS | Russian Federation | Europe | Eastern Europe | 146934000
6 | FSM | Micronesia, Federated States of | Oceania | Micronesia | 119000
4 | AZE | Azerbaijan | Asia | Middle East | 7734000
1 | BIH | Bosnia and Herzegovina | Europe | Southern Europe | 3972000
(4 rows)
-- Using CTE in the WHERE clause
--query3
with lang_total as
( select lang_count,countrycode from
(
(
select count(*) as lang_count,country_co.code,countrylanguage_co.countrycode
from country_co join countrylanguage_co on (country_co.code=countrylanguage_co.countrycode and governmentform='Federal Republic')
group by country_co.code,countrylanguage_co.countrycode order by country_co.code)
UNION ALL
(
select count(*) as lang_count,country_co.code,countrylanguage_co.countrycode
from country_co join countrylanguage_co on (country_co.code=countrylanguage_co.countrycode and governmentform='Monarchy')
group by country_co.code,countrylanguage_co.countrycode order by country_co.code)
) FOO1
)
select * from
(
select count(*) as cnt,country_co.code,country_co.name
from
country_co,countrylanguage_co
where country_co.code=countrylanguage_co.countrycode group by country_co.code,country_co.name) AS FOO
where foo.cnt = (select max(lang_count) from lang_total) order by foo.code;
cnt | code | name
-----+------+--------------------
12 | CAN | Canada
12 | CHN | China
12 | IND | India
12 | RUS | Russian Federation
12 | USA | United States
(5 rows)
--query4
with diversecountries as
(select country_co.code,country_co.name,country_co.capital,d.CNT
from country_co,
(select countrylanguage_co.countrycode,count(*) as CNT from countrylanguage_co group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country_co.code and country_co.gnp > 100000)
select diversecountries.name,city_co.name,diversecountries.CNT
from diversecountries,city_co where city_co.id = diversecountries.capital
order by diversecountries.name;
name | name | cnt
--------------------+------------------+-----
Australia | Canberra | 8
Austria | Wien | 8
Canada | Ottawa | 12
China | Peking | 12
Denmark | Kobenhavn | 7
India | New Delhi | 12
Iran | Teheran | 10
Italy | Roma | 8
Myanmar | Rangoon (Yangon) | 8
Russian Federation | Moscow | 12
South Africa | Pretoria | 11
United States | Washington | 12
(12 rows)
-- some queries with merge joins and index scans
set enable_nestloop=off;
set enable_hashjoin=off;
set enable_mergejoin=on;
-- query 5
with somecheapasiandiversecountries as
(
select FOO.code,FOO.COUNTRY_CO,FOO.CAPITAL,FOO.headofstate,count(*) ASIAN_COUNT from
(
select country_co.code,country_co.name COUNTRY_CO,city_co.name CAPITAL,country_co.headofstate
from country_co,city_co
where country_co.capital = city_co.id
and country_co.gnp < 10000
and country_co.region = 'Southeast Asia'
and country_co.continent = 'Asia'
UNION ALL
select country_co.code,country_co.name COUNTRY_CO,city_co.name CAPITAL,country_co.headofstate
from country_co,city_co
where country_co.capital = city_co.id
and country_co.gnp < 10000
and country_co.region = 'Eastern Asia'
and country_co.continent = 'Asia'
UNION ALL
select country_co.code,country_co.name COUNTRY_CO,city_co.name CAPITAL,country_co.headofstate
from country_co,city_co
where country_co.capital = city_co.id
and country_co.gnp < 10000
and country_co.region = 'Middle East'
and country_co.continent = 'Asia'
) FOO, countrylanguage_co
where FOO.code = countrylanguage_co.countrycode
group by FOO.code,FOO.COUNTRY_CO,FOO.CAPITAL,FOO.headofstate,countrylanguage_co.countrycode
HAVING count(*) >=
(select min(CNT) FROM
(select count(*) CNT,country_co.code from countrylanguage_co,country_co
where countrylanguage_co.countrycode=country_co.code
and country_co.continent = 'Asia'
and country_co.region = 'Southern and Central Asia'
group by country_co.code
) FOO1
)
)
select FOO.code,FOO.COUNTRY_CO,FOO.CAPITAL,FOO.headofstate,count(*) COMPARED_WITH_CHEAP_ASIAN_CNT
from
(
select country_co.code,country_co.name COUNTRY_CO,city_co.name CAPITAL,country_co.headofstate
from country_co,city_co
where country_co.capital = city_co.id
and country_co.continent = 'North America'
UNION ALL
select country_co.code,country_co.name COUNTRY_CO,city_co.name CAPITAL,country_co.headofstate
from country_co,city_co
where country_co.capital = city_co.id
and country_co.continent = 'South America'
) FOO,countrylanguage_co
where FOO.code = countrylanguage_co.countrycode
group by FOO.code,FOO.COUNTRY_CO,FOO.CAPITAL,FOO.headofstate
HAVING count(*) >=
(select min(ASIAN_COUNT) FROM
(select ASIAN_COUNT FROM somecheapasiandiversecountries,country_co
where somecheapasiandiversecountries.code = country_co.code
and country_co.gnp >= country_co.gnpold
) ASIANCOUNT
)
order by COUNTRY_CO;
code | country_co | capital | headofstate | compared_with_cheap_asian_cnt
------+----------------------------------+-------------------------+-----------------------------------+-------------------------------
ATG | Antigua and Barbuda | Saint Johns | Elisabeth II | 2
ARG | Argentina | Buenos Aires | Fernando de la Rua | 3
ABW | Aruba | Oranjestad | Beatrix | 4
BHS | Bahamas | Nassau | Elisabeth II | 2
BRB | Barbados | Bridgetown | Elisabeth II | 2
BLZ | Belize | Belmopan | Elisabeth II | 4
BOL | Bolivia | La Paz | Hugo Banzer Suarez | 4
BRA | Brazil | Brasilia | Fernando Henrique Cardoso | 5
CAN | Canada | Ottawa | Elisabeth II | 12
CHL | Chile | Santiago de Chile | Ricardo Lagos Escobar | 4
COL | Colombia | Santafe de Bogota | Andres Pastrana Arango | 5
CRI | Costa Rica | San Jose | Miguel Angel Rodriguez Echeverria | 4
DMA | Dominica | Roseau | Vernon Shaw | 2
DOM | Dominican Republic | Santo Domingo de Guzman | Hipolito Mejia Dominguez | 2
ECU | Ecuador | Quito | Gustavo Noboa Bejarano | 2
SLV | El Salvador | San Salvador | Francisco Guillermo Flores Perez | 2
GUF | French Guiana | Cayenne | Jacques Chirac | 2
GRL | Greenland | Nuuk | Margrethe II | 2
GLP | Guadeloupe | Basse-Terre | Jacques Chirac | 2
GTM | Guatemala | Ciudad de Guatemala | Alfonso Portillo Cabrera | 5
GUY | Guyana | Georgetown | Bharrat Jagdeo | 3
HTI | Haiti | Port-au-Prince | Jean-Bertrand Aristide | 2
HND | Honduras | Tegucigalpa | Carlos Roberto Flores Facusse | 4
JAM | Jamaica | Kingston | Elisabeth II | 2
MTQ | Martinique | Fort-de-France | Jacques Chirac | 2
MEX | Mexico | Ciudad de Mexico | Vicente Fox Quesada | 6
ANT | Netherlands Antilles | Willemstad | Beatrix | 3
NIC | Nicaragua | Managua | Arnoldo Aleman Lacayo | 4
PAN | Panama | Ciudad de Panama | Mireya Elisa Moscoso Rodriguez | 6
PRY | Paraguay | Asuncion | Luis Angel Gonzalez Macchi | 4
PER | Peru | Lima | Valentin Paniagua Corazao | 3
PRI | Puerto Rico | San Juan | George W. Bush | 2
KNA | Saint Kitts and Nevis | Basseterre | Elisabeth II | 2
LCA | Saint Lucia | Castries | Elisabeth II | 2
VCT | Saint Vincent and the Grenadines | Kingstown | Elisabeth II | 2
SUR | Suriname | Paramaribo | Ronald Venetiaan | 2
TTO | Trinidad and Tobago | Port-of-Spain | Arthur N. R. Robinson | 3
USA | United States | Washington | George W. Bush | 12
VEN | Venezuela | Caracas | Hugo Chavez Frias | 3
VIR | Virgin Islands, U.S. | Charlotte Amalie | George W. Bush | 3
(40 rows)
-- query 6
select count(*) from
( select r.* from
( with fact as
(
select country_co.name as COUNTRY_CO,country_co.code,city_co.name as CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,AGG1.region
from
(select
sum(case when (city_co.population >= 0.5 * country_co.population) then country_co.population else city_co.population end) as S_POPULATION,
sum(case when (gnp >= gnpold) then gnp else gnpold end) as S_GNP,
avg(case when (lifeexpectancy > 60) then 50 else lifeexpectancy end) as AVG_LIFE,country_co.region
from country_co,city_co
where governmentform != 'Constitutional Monarchy'
and country_co.capital = city_co.id
and indepyear > 0
group by country_co.region) AGG1
,country_co,city_co
where country_co.capital = city_co.id
and country_co.region = AGG1.region
)
select code,COUNTRY_CO,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage_co
where fact.code = countrylanguage_co.countrycode and isofficial = 'True'
and fact.region = 'South America'
UNION ALL
select code,COUNTRY_CO,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage_co
where fact.code = countrylanguage_co.countrycode and isofficial = 'True'
and fact.region = 'North America'
UNION ALL
select code,COUNTRY_CO,CAPITAL,S_POPULATION,S_GNP,AVG_LIFE,language as OFFICIALLANGUAGE,region
from fact,countrylanguage_co
where fact.code = countrylanguage_co.countrycode and isofficial = 'True'
and fact.region = 'Caribbean'
) as r
left join
(
select 'ARG' as CODE UNION ALL
select 'BOL' as CODE UNION ALL
select 'BRA' as CODE UNION ALL
select 'PER' as CODE UNION ALL
select 'URY' as CODE UNION ALL
select 'IND' as CODE UNION ALL
select 'LCA' as CODE UNION ALL
select 'VCT' as CODE
) as r1
on r.code = r1.code) AS FOO;
count
-------
43
(1 row)
-- query7
with alleuropeanlanguages as
(select country_co.code,country_co.name COUNTRY_CO, city_co.name CAPITAL, language, isofficial, percentage
FROM country_co,city_co,countrylanguage_co
WHERE country_co.code = countrylanguage_co.countrycode
and country_co.capital = city_co.id
and country_co.continent = 'Europe')
select * from
(select * from alleuropeanlanguages where isofficial='True') e1,
(select * from alleuropeanlanguages where percentage > 50) e2
where e1.code = e2.code order by e2.COUNTRY_CO,e1.language;
code | country_co | capital | language | isofficial | percentage | code | country_co | capital | language | isofficial | percentage
------+------------------------+-----------------------------------+----------------+------------+------------+------+------------------------+-----------------------------------+----------------+------------+------------
ALB | Albania | Tirana | Albaniana | t | 97.9 | ALB | Albania | Tirana | Albaniana | t | 97.9
AUT | Austria | Wien | German | t | 92 | AUT | Austria | Wien | German | t | 92
BLR | Belarus | Minsk | Belorussian | t | 65.6 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BLR | Belarus | Minsk | Russian | t | 32 | BLR | Belarus | Minsk | Belorussian | t | 65.6
BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | French | t | 32.6 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BEL | Belgium | Bruxelles [Brussel] | German | t | 1 | BEL | Belgium | Bruxelles [Brussel] | Dutch | t | 59.2
BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2 | BIH | Bosnia and Herzegovina | Sarajevo | Serbo-Croatian | t | 99.2
BGR | Bulgaria | Sofija | Bulgariana | t | 83.2 | BGR | Bulgaria | Sofija | Bulgariana | t | 83.2
HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9 | HRV | Croatia | Zagreb | Serbo-Croatian | t | 95.9
CZE | Czech Republic | Praha | Czech | t | 81.2 | CZE | Czech Republic | Praha | Czech | t | 81.2
DNK | Denmark | Kobenhavn | Danish | t | 93.5 | DNK | Denmark | Kobenhavn | Danish | t | 93.5
EST | Estonia | Tallinn | Estonian | t | 65.3 | EST | Estonia | Tallinn | Estonian | t | 65.3
FRO | Faroe Islands | Torshavn | Danish | t | 0 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FRO | Faroe Islands | Torshavn | Faroese | t | 100 | FRO | Faroe Islands | Torshavn | Faroese | t | 100
FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FIN | Finland | Helsinki [Helsingfors] | Swedish | t | 5.7 | FIN | Finland | Helsinki [Helsingfors] | Finnish | t | 92.7
FRA | France | Paris | French | t | 93.6 | FRA | France | Paris | French | t | 93.6
DEU | Germany | Berlin | German | t | 91.3 | DEU | Germany | Berlin | German | t | 91.3
GIB | Gibraltar | Gibraltar | English | t | 88.9 | GIB | Gibraltar | Gibraltar | English | t | 88.9
GRC | Greece | Athenai | Greek | t | 98.5 | GRC | Greece | Athenai | Greek | t | 98.5
HUN | Hungary | Budapest | Hungarian | t | 98.5 | HUN | Hungary | Budapest | Hungarian | t | 98.5
ISL | Iceland | Reykjavik | Icelandic | t | 95.7 | ISL | Iceland | Reykjavik | Icelandic | t | 95.7
IRL | Ireland | Dublin | English | t | 98.4 | IRL | Ireland | Dublin | English | t | 98.4
IRL | Ireland | Dublin | Irish | t | 1.6 | IRL | Ireland | Dublin | English | t | 98.4
ITA | Italy | Roma | Italian | t | 94.1 | ITA | Italy | Roma | Italian | t | 94.1
LVA | Latvia | Riga | Latvian | t | 55.1 | LVA | Latvia | Riga | Latvian | t | 55.1
LIE | Liechtenstein | Vaduz | German | t | 89 | LIE | Liechtenstein | Vaduz | German | t | 89
LTU | Lithuania | Vilnius | Lithuanian | t | 81.6 | LTU | Lithuania | Vilnius | Lithuanian | t | 81.6
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | French | t | 4.2 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | German | t | 2.3 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4 | LUX | Luxembourg | Luxembourg [Luxemburg/Letzebuerg] | Luxembourgish | t | 64.4
MKD | Macedonia | Skopje | Macedonian | t | 66.5 | MKD | Macedonia | Skopje | Macedonian | t | 66.5
MLT | Malta | Valletta | English | t | 2.1 | MLT | Malta | Valletta | Maltese | t | 95.8
MLT | Malta | Valletta | Maltese | t | 95.8 | MLT | Malta | Valletta | Maltese | t | 95.8
MDA | Moldova | Chisinau | Romanian | t | 61.9 | MDA | Moldova | Chisinau | Romanian | t | 61.9
NLD | Netherlands | Amsterdam | Dutch | t | 95.6 | NLD | Netherlands | Amsterdam | Dutch | t | 95.6
NOR | Norway | Oslo | Norwegian | t | 96.6 | NOR | Norway | Oslo | Norwegian | t | 96.6
POL | Poland | Warszawa | Polish | t | 97.6 | POL | Poland | Warszawa | Polish | t | 97.6
PRT | Portugal | Lisboa | Portuguese | t | 99 | PRT | Portugal | Lisboa | Portuguese | t | 99
ROM | Romania | Bucuresti | Romani | t | 0.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
ROM | Romania | Bucuresti | Romanian | t | 90.7 | ROM | Romania | Bucuresti | Romanian | t | 90.7
RUS | Russian Federation | Moscow | Russian | t | 86.6 | RUS | Russian Federation | Moscow | Russian | t | 86.6
SMR | San Marino | San Marino | Italian | t | 100 | SMR | San Marino | San Marino | Italian | t | 100
SVK | Slovakia | Bratislava | Slovak | t | 85.6 | SVK | Slovakia | Bratislava | Slovak | t | 85.6
SVN | Slovenia | Ljubljana | Slovene | t | 87.9 | SVN | Slovenia | Ljubljana | Slovene | t | 87.9
ESP | Spain | Madrid | Spanish | t | 74.4 | ESP | Spain | Madrid | Spanish | t | 74.4
SWE | Sweden | Stockholm | Swedish | t | 89.5 | SWE | Sweden | Stockholm | Swedish | t | 89.5
CHE | Switzerland | Bern | French | t | 19.2 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | German | t | 63.6 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Italian | t | 7.7 | CHE | Switzerland | Bern | German | t | 63.6
CHE | Switzerland | Bern | Romansh | t | 0.6 | CHE | Switzerland | Bern | German | t | 63.6
UKR | Ukraine | Kyiv | Ukrainian | t | 64.7 | UKR | Ukraine | Kyiv | Ukrainian | t | 64.7
GBR | United Kingdom | London | English | t | 97.3 | GBR | United Kingdom | London | English | t | 97.3
YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2 | YUG | Yugoslavia | Beograd | Serbo-Croatian | t | 75.2
(55 rows)
-- query8
with allcountry_costats as
( select country_co.code,country_co.name,count(distinct city_co.id) CITY_CO_CNT,
count(distinct countrylanguage_co.language) LANG_CNT
from country_co,city_co,countrylanguage_co
where country_co.code = city_co.countrycode
and country_co.code = countrylanguage_co.countrycode
group by country_co.code,country_co.name
)
select sum(FOO.CITY_CO_CNT) REGION_CITY_CO_CNT,sum(FOO.LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountry_costats.code,allcountry_costats.name COUNTRY_CO,CITY_CO_CNT,LANG_CNT,country_co.region,city_co.name CAPITAL
from allcountry_costats,country_co,city_co
where allcountry_costats.code = country_co.code
and country_co.capital = city_co.id
and CITY_CO_CNT/LANG_CNT > 1
and country_co.continent = 'Asia'
UNION ALL
select allcountry_costats.code,allcountry_costats.name COUNTRY_CO,CITY_CO_CNT,LANG_CNT,country_co.region,city_co.name CAPITAL
from allcountry_costats,country_co,city_co
where allcountry_costats.code = country_co.code
and country_co.capital = city_co.id
and CITY_CO_CNT/LANG_CNT > 1
and country_co.continent = 'North America'
UNION ALL
select allcountry_costats.code,allcountry_costats.name COUNTRY_CO,CITY_CO_CNT,LANG_CNT,country_co.region,city_co.name CAPITAL
from allcountry_costats,country_co,city_co
where allcountry_costats.code = country_co.code
and country_co.capital = city_co.id
and CITY_CO_CNT/LANG_CNT > (select max(CITY_CO_CNT/LANG_CNT) from allcountry_costats,country_co where allcountry_costats.code = country_co.code AND country_co.continent='Europe')
) FOO
,allcountry_costats,country_co
WHERE allcountry_costats.code = country_co.code
and FOO.region = country_co.region
group by FOO.region order by FOO.region;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
region_city_co_cnt | region_lang_cnt | region
--------------------+-----------------+---------------------------
840 | 192 | Caribbean
2824 | 112 | Central America
11336 | 384 | Eastern Asia
2664 | 396 | Middle East
1625 | 125 | North America
3500 | 70 | South America
3179 | 528 | Southeast Asia
12278 | 896 | Southern and Central Asia
(8 rows)
-- some queries with hash joins and index scans
set enable_nestloop=off;
set enable_hashjoin=on;
set enable_mergejoin=off;
--query 9
with diversecountries as
(select country_co.code,country_co.name,country_co.capital,d.CNT
from country_co,
(select countrylanguage_co.countrycode,count(*) as CNT from countrylanguage_co group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country_co.code and country_co.gnp > 100000)
select d1.code,d1.name,d1.capital,city_co.name CAPITAL_CITY_CO,d1.CNT,d2.CNT
from
diversecountries d1 left join country_co
ON (d1.code = country_co.code AND d1.CNT < 8)
left join diversecountries d2
ON (country_co.code = d2.code AND d2.CNT > 8)
INNER JOIN city_co
ON(d1.capital = city_co.id)
ORDER BY d1.name;
code | name | capital | capital_city_co | cnt | cnt
------+--------------------+---------+------------------+-----+-----
AUS | Australia | 135 | Canberra | 8 |
AUT | Austria | 1523 | Wien | 8 |
CAN | Canada | 1822 | Ottawa | 12 |
CHN | China | 1891 | Peking | 12 |
DNK | Denmark | 3315 | Kobenhavn | 7 |
IND | India | 1109 | New Delhi | 12 |
IRN | Iran | 1380 | Teheran | 10 |
ITA | Italy | 1464 | Roma | 8 |
MMR | Myanmar | 2710 | Rangoon (Yangon) | 8 |
RUS | Russian Federation | 3580 | Moscow | 12 |
ZAF | South Africa | 716 | Pretoria | 11 |
USA | United States | 3813 | Washington | 12 |
(12 rows)
--query 10 , multiple ctes, joins
with longlivingregions as
(
select FOO.*,count(distinct language) as "lang_count"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
avg(lifeexpectancy) as "REGION_LIFETIME",region
from
country_co
group by region
) FOO,countrylanguage_co,country_co
where
country_co.code = countrylanguage_co.countrycode
and FOO.region = country_co.region
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP",foo."REGION_LIFETIME"),
denseregions as
(
select FOO.*,count(distinct language) as "lang_count",
sum(surfacearea) as "REGION_SURFACE_AREA"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
region
from
country_co
group by region
) FOO,countrylanguage_co,country_co
where
country_co.code = countrylanguage_co.countrycode
and FOO.region = country_co.region
and FOO."REGION_POP" != 0
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP"
order by sum(surfacearea)/foo."REGION_POP" desc),
allcountry_costats as
( select country_co.code,country_co.name,count(distinct city_co.id) CITY_CO_CNT,
count(distinct countrylanguage_co.language) LANG_CNT
from country_co,city_co,countrylanguage_co
where country_co.code = city_co.countrycode
and country_co.code = countrylanguage_co.countrycode
group by country_co.code,country_co.name
)
select allcountry_costats.CITY_CO_CNT,allcountry_costats.LANG_CNT,allcountry_costats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountry_costats,country_co
where longlivingregions.region = denseregions.region and allcountry_costats.code = country_co.code and country_co.region = longlivingregions.region
and country_co.indepyear between 1800 and 1850
UNION ALL
select allcountry_costats.CITY_CO_CNT,allcountry_costats.LANG_CNT,allcountry_costats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountry_costats,country_co
where longlivingregions.region = denseregions.region and allcountry_costats.code = country_co.code and country_co.region = longlivingregions.region
and country_co.indepyear between 1850 and 1900
UNION ALL
select allcountry_costats.CITY_CO_CNT,allcountry_costats.LANG_CNT,allcountry_costats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountry_costats,country_co
where longlivingregions.region = denseregions.region and allcountry_costats.code = country_co.code and country_co.region = longlivingregions.region
and country_co.indepyear > 1900
order by name
LIMIT 50;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
city_co_cnt | lang_cnt | name | REGION_SURFACE_AREA | REGION_LIFETIME | REGION_POP | lang_count | REGION_GNP | region
-------------+----------+---------------------------------------+---------------------+------------------+------------+------------+------------+---------------------------
4 | 5 | Afghanistan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 3 | Albania | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
18 | 2 | Algeria | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
5 | 9 | Angola | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 2 | Antigua and Barbuda | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
57 | 3 | Argentina | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 2 | Armenia | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
14 | 8 | Australia | 62471162.00 | 78.8000030517578 | 22753100 | 11 | 405851.00 | Australia and New Zealand
6 | 8 | Austria | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
4 | 4 | Azerbaijan | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
1 | 2 | Bahamas | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
1 | 2 | Bahrain | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
24 | 7 | Bangladesh | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 2 | Barbados | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
16 | 4 | Belarus | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
9 | 6 | Belgium | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
2 | 4 | Belize | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
4 | 7 | Benin | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Bhutan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
8 | 4 | Bolivia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 1 | Bosnia and Herzegovina | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
2 | 5 | Botswana | 23060186.00 | 44.8199996948242 | 46886000 | 21 | 126931.00 | Southern Africa
250 | 5 | Brazil | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 4 | Brunei | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
10 | 4 | Bulgaria | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
3 | 6 | Burkina Faso | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Burundi | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
3 | 4 | Cambodia | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
7 | 8 | Cameroon | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
49 | 12 | Canada | 236342035.00 | 75.8199996948242 | 309632000 | 18 | 9111890.00 | North America
1 | 2 | Cape Verde | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 6 | Central African Republic | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
2 | 8 | Chad | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
29 | 4 | Chile | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
38 | 5 | Colombia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 5 | Comoros | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
2 | 6 | Congo | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
18 | 10 | Congo, The Democratic Republic of the | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 4 | Costa Rica | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
5 | 5 | Cote deIvoire | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
4 | 2 | Croatia | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
14 | 1 | Cuba | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
2 | 2 | Cyprus | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
10 | 8 | Czech Republic | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
1 | 3 | Djibouti | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
1 | 2 | Dominica | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
6 | 2 | Dominican Republic | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
15 | 2 | Ecuador | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
37 | 2 | Egypt | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
7 | 2 | El Salvador | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
(50 rows)
--query 11
with allcity_costats as
( select city_co.name CITY_CO,city_co.id,country_co.name COUNTRY_CO,city_co.district,city_co.population as CITY_CO_POP
from
city_co,country_co
where city_co.countrycode = country_co.code
),
alldistrictstats as
( select allcity_costats.district,allcity_costats.COUNTRY_CO,sum(CITY_CO_POP) DISTRICT_POP,
count(CITY_CO) as D_CITY_CO_CNT
from allcity_costats
group by allcity_costats.district,allcity_costats.COUNTRY_CO
order by district,COUNTRY_CO
),
allcountry_costats as
( select alldistrictstats.COUNTRY_CO,country_co.code,sum(D_CITY_CO_CNT) C_CITY_CO_CNT,
count(distinct countrylanguage_co.language) C_LANG_CNT
from alldistrictstats,country_co,countrylanguage_co
where alldistrictstats.COUNTRY_CO = country_co.name
and country_co.code = countrylanguage_co.countrycode
group by COUNTRY_CO,code
),
asian_region_stats as
(
select sum(FOO.C_CITY_CO_CNT) REGION_CITY_CO_CNT,sum(FOO.C_LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountry_costats.code,allcountry_costats.COUNTRY_CO,C_CITY_CO_CNT,C_LANG_CNT,country_co.region,city_co.name CAPITAL
from allcountry_costats,country_co,city_co
where allcountry_costats.code = country_co.code
and country_co.capital = city_co.id
and C_CITY_CO_CNT/C_LANG_CNT > 1
and country_co.continent = 'Asia') FOO
,allcountry_costats,country_co
WHERE allcountry_costats.code = country_co.code
and FOO.region = country_co.region
group by FOO.region order by FOO.region
)
select * from
(
select REGION_CITY_CO_CNT as CITY_CO_CNT,REGION_LANG_CNT as LANG_CNT, region as IDENTIFIER from asian_region_stats
UNION ALL
(
select sum(FOO.C_CITY_CO_CNT) CITY_CO_CNT,sum(FOO.C_LANG_CNT) LANG_CNT,FOO.region as IDENTIFIER
FROM
(
select allcountry_costats.code,allcountry_costats.COUNTRY_CO,C_CITY_CO_CNT,C_LANG_CNT,country_co.region,allcity_costats.CITY_CO CAPITAL
from allcountry_costats,country_co,allcity_costats
where allcountry_costats.code = country_co.code
and country_co.capital = allcity_costats.id
and C_CITY_CO_CNT/C_LANG_CNT > 1
and country_co.continent = 'Europe') FOO
,allcountry_costats,country_co
WHERE allcountry_costats.code = country_co.code
and FOO.region = country_co.region
group by FOO.region order by FOO.region
)
) FOO1
order by FOO1.lang_cnt,FOO1.identifier;
city_co_cnt | lang_cnt | identifier
-------------+----------+---------------------------
494 | 10 | British Islands
159 | 48 | Baltic Countries
1295 | 161 | Nordic Countries
50176 | 264 | Eastern Asia
9414 | 369 | Western Europe
11880 | 450 | Southern Europe
32900 | 610 | Eastern Europe
29161 | 616 | Southeast Asia
8568 | 792 | Middle East
80388 | 1092 | Southern and Central Asia
(10 rows)
-- Queries using multiple CTEs
-- query1 - all CTEs being used once in the main query
with capitals as
(select country.code,id,city.name from city,country
where city.countrycode = country.code AND city.id = country.capital),
lang_total as
( select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code),
alleuropeanlanguages as
(select country.code,country.name COUNTRY, city.name CAPITAL, language, isofficial, percentage
FROM country,city,countrylanguage
WHERE country.code = countrylanguage.countrycode
and country.capital = city.id
and country.continent = 'Europe'),
diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000)
select capitals.name CAPITAL,lang_total.lang_count COUNTRY_LANG_COUNT,capitals.code,country.name
from capitals,lang_total,country
where capitals.code = lang_total.countrycode
and capitals.code = country.code
and country.code NOT IN (select diversecountries.code from diversecountries,alleuropeanlanguages
where diversecountries.code = alleuropeanlanguages.code)
order by capitals.code;
capital | country_lang_count | code | name
------------------+--------------------+------+---------------------------------
Buenos Aires | 3 | ARG | Argentina
Baku | 4 | AZE | Azerbaijan
Sarajevo | 1 | BIH | Bosnia and Herzegovina
Brasilia | 5 | BRA | Brazil
Berlin | 6 | DEU | Germany
Palikir | 6 | FSM | Micronesia, Federated States of
New Delhi | 12 | IND | India
Antananarivo | 2 | MDG | Madagascar
Ciudad de Mexico | 6 | MEX | Mexico
Abuja | 10 | NGA | Nigeria
Washington | 12 | USA | United States
Caracas | 3 | VEN | Venezuela
Beograd | 6 | YUG | Yugoslavia
(13 rows)
-- query 2 multiple CTEs being used multiple times through joins
with longlivingregions as
(
select FOO.*,count(distinct language) as "lang_count"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
avg(lifeexpectancy) as "REGION_LIFETIME",region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP",foo."REGION_LIFETIME"),
denseregions as
(
select FOO.*,count(distinct language) as "lang_count",
sum(surfacearea) as "REGION_SURFACE_AREA"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
and FOO."REGION_POP" != 0
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP"
order by sum(surfacearea)/foo."REGION_POP" desc),
allcountrystats as
( select country.code,country.name,count(distinct city.id) CITY_CNT,
count(distinct countrylanguage.language) LANG_CNT
from country,city,countrylanguage
where country.code = city.countrycode
and country.code = countrylanguage.countrycode
group by country.code,country.name
)
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1800 and 1850
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1850 and 1900
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear > 1900
order by name
LIMIT 50;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
city_cnt | lang_cnt | name | REGION_SURFACE_AREA | REGION_LIFETIME | REGION_POP | lang_count | REGION_GNP | region
----------+----------+---------------------------------------+---------------------+------------------+------------+------------+------------+---------------------------
4 | 5 | Afghanistan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 3 | Albania | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
18 | 2 | Algeria | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
5 | 9 | Angola | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 2 | Antigua and Barbuda | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
57 | 3 | Argentina | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 2 | Armenia | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
14 | 8 | Australia | 62471162.00 | 78.8000030517578 | 22753100 | 11 | 405851.00 | Australia and New Zealand
6 | 8 | Austria | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
4 | 4 | Azerbaijan | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
1 | 2 | Bahamas | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
1 | 2 | Bahrain | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
24 | 7 | Bangladesh | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
1 | 2 | Barbados | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
16 | 4 | Belarus | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
9 | 6 | Belgium | 6649768.00 | 78.2555567423503 | 183247600 | 21 | 4673272.00 | Western Europe
2 | 4 | Belize | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
4 | 7 | Benin | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Bhutan | 90749795.00 | 61.3500003814697 | 1490776000 | 54 | 810604.00 | Southern and Central Asia
8 | 4 | Bolivia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
3 | 1 | Bosnia and Herzegovina | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
2 | 5 | Botswana | 23060186.00 | 44.8199996948242 | 46886000 | 21 | 126931.00 | Southern Africa
250 | 5 | Brazil | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 4 | Brunei | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
10 | 4 | Bulgaria | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
3 | 6 | Burkina Faso | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 3 | Burundi | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
3 | 4 | Cambodia | 35322186.00 | 64.4000001387163 | 518541000 | 47 | 642643.00 | Southeast Asia
7 | 8 | Cameroon | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
49 | 12 | Canada | 236342035.00 | 75.8199996948242 | 309632000 | 18 | 9111890.00 | North America
1 | 2 | Cape Verde | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
1 | 6 | Central African Republic | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
2 | 8 | Chad | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
29 | 4 | Chile | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
38 | 5 | Colombia | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
1 | 5 | Comoros | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
2 | 6 | Congo | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
18 | 10 | Congo, The Democratic Republic of the | 55663022.00 | 50.3111110263401 | 95652000 | 47 | 32938.00 | Central Africa
1 | 4 | Costa Rica | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
5 | 5 | Cote deIvoire | 39744907.00 | 52.7411768296186 | 221672000 | 65 | 106711.00 | Western Africa
4 | 2 | Croatia | 5845195.40 | 76.5285720825195 | 144674200 | 22 | 2012289.00 | Southern Europe
14 | 1 | Cuba | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
2 | 2 | Cyprus | 10153662.00 | 70.5666671329074 | 188380700 | 21 | 677260.00 | Middle East
10 | 8 | Czech Republic | 214731845.00 | 69.9299995422363 | 307026000 | 28 | 659980.00 | Eastern Europe
1 | 3 | Djibouti | 43631319.00 | 50.8105261953254 | 246999000 | 80 | 69925.00 | Eastern Africa
1 | 2 | Dominica | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
6 | 2 | Dominican Republic | 363261.00 | 73.0583332379659 | 38140000 | 10 | 103586.20 | Caribbean
15 | 2 | Ecuador | 74318167.00 | 70.9461532005897 | 345780000 | 21 | 1511874.00 | South America
37 | 2 | Egypt | 36993520.00 | 65.3857127598354 | 173266000 | 14 | 243870.00 | Northern Africa
7 | 2 | El Salvador | 14052371.00 | 71.0249996185303 | 135221000 | 23 | 473151.00 | Central America
(50 rows)
-- Queries with multiple CTEs referencing one another
-- query1
with city_official_language as
(select city.id,city.name CITY,country.name COUNTRY,countrylanguage.language,city.population,country.capital
from city,countrylanguage,country
where city.countrycode = country.code
and country.code = countrylanguage.countrycode
and countrylanguage.isofficial = 'True'
),
capital_official_language as
(select c2.CITY, c1.COUNTRY,c1.language OFFICIAL_LANGUAGE,c1.CITY CAPITAL
from city_official_language c1 , city_official_language c2
where c1.id = c2.capital
and c1.id != c2.id
),
alleuropeanlanguages as
(select c.COUNTRY,c.CITY,c.CAPITAL,c.OFFICIAL_LANGUAGE
from capital_official_language c, country
where c.COUNTRY = country.name
and country.continent = 'Europe'
)
select code,COUNTRY,CITY,alleuropeanlanguages.CAPITAL,OFFICIAL_LANGUAGE from alleuropeanlanguages,country
where alleuropeanlanguages.COUNTRY = country.name
and alleuropeanlanguages.city in (select CITY from city_official_language)
and alleuropeanlanguages.OFFICIAL_LANGUAGE IN (select OFFICIAL_LANGUAGE from capital_official_language)
order by code,country,city,official_language
limit 100;
code | country | city | capital | official_language
------+------------------------+--------------+---------------------+-------------------
AUT | Austria | Graz | Wien | German
AUT | Austria | Innsbruck | Wien | German
AUT | Austria | Klagenfurt | Wien | German
AUT | Austria | Linz | Wien | German
AUT | Austria | Salzburg | Wien | German
BEL | Belgium | Antwerpen | Bruxelles [Brussel] | Dutch
BEL | Belgium | Antwerpen | Bruxelles [Brussel] | Dutch
BEL | Belgium | Antwerpen | Bruxelles [Brussel] | Dutch
BEL | Belgium | Antwerpen | Bruxelles [Brussel] | French
BEL | Belgium | Antwerpen | Bruxelles [Brussel] | French
BEL | Belgium | Antwerpen | Bruxelles [Brussel] | French
BEL | Belgium | Antwerpen | Bruxelles [Brussel] | German
BEL | Belgium | Antwerpen | Bruxelles [Brussel] | German
BEL | Belgium | Antwerpen | Bruxelles [Brussel] | German
BEL | Belgium | Brugge | Bruxelles [Brussel] | Dutch
BEL | Belgium | Brugge | Bruxelles [Brussel] | Dutch
BEL | Belgium | Brugge | Bruxelles [Brussel] | Dutch
BEL | Belgium | Brugge | Bruxelles [Brussel] | French
BEL | Belgium | Brugge | Bruxelles [Brussel] | French
BEL | Belgium | Brugge | Bruxelles [Brussel] | French
BEL | Belgium | Brugge | Bruxelles [Brussel] | German
BEL | Belgium | Brugge | Bruxelles [Brussel] | German
BEL | Belgium | Brugge | Bruxelles [Brussel] | German
BEL | Belgium | Charleroi | Bruxelles [Brussel] | Dutch
BEL | Belgium | Charleroi | Bruxelles [Brussel] | Dutch
BEL | Belgium | Charleroi | Bruxelles [Brussel] | Dutch
BEL | Belgium | Charleroi | Bruxelles [Brussel] | French
BEL | Belgium | Charleroi | Bruxelles [Brussel] | French
BEL | Belgium | Charleroi | Bruxelles [Brussel] | French
BEL | Belgium | Charleroi | Bruxelles [Brussel] | German
BEL | Belgium | Charleroi | Bruxelles [Brussel] | German
BEL | Belgium | Charleroi | Bruxelles [Brussel] | German
BEL | Belgium | Gent | Bruxelles [Brussel] | Dutch
BEL | Belgium | Gent | Bruxelles [Brussel] | Dutch
BEL | Belgium | Gent | Bruxelles [Brussel] | Dutch
BEL | Belgium | Gent | Bruxelles [Brussel] | French
BEL | Belgium | Gent | Bruxelles [Brussel] | French
BEL | Belgium | Gent | Bruxelles [Brussel] | French
BEL | Belgium | Gent | Bruxelles [Brussel] | German
BEL | Belgium | Gent | Bruxelles [Brussel] | German
BEL | Belgium | Gent | Bruxelles [Brussel] | German
BEL | Belgium | Liege | Bruxelles [Brussel] | Dutch
BEL | Belgium | Liege | Bruxelles [Brussel] | Dutch
BEL | Belgium | Liege | Bruxelles [Brussel] | Dutch
BEL | Belgium | Liege | Bruxelles [Brussel] | French
BEL | Belgium | Liege | Bruxelles [Brussel] | French
BEL | Belgium | Liege | Bruxelles [Brussel] | French
BEL | Belgium | Liege | Bruxelles [Brussel] | German
BEL | Belgium | Liege | Bruxelles [Brussel] | German
BEL | Belgium | Liege | Bruxelles [Brussel] | German
BEL | Belgium | Mons | Bruxelles [Brussel] | Dutch
BEL | Belgium | Mons | Bruxelles [Brussel] | Dutch
BEL | Belgium | Mons | Bruxelles [Brussel] | Dutch
BEL | Belgium | Mons | Bruxelles [Brussel] | French
BEL | Belgium | Mons | Bruxelles [Brussel] | French
BEL | Belgium | Mons | Bruxelles [Brussel] | French
BEL | Belgium | Mons | Bruxelles [Brussel] | German
BEL | Belgium | Mons | Bruxelles [Brussel] | German
BEL | Belgium | Mons | Bruxelles [Brussel] | German
BEL | Belgium | Namur | Bruxelles [Brussel] | Dutch
BEL | Belgium | Namur | Bruxelles [Brussel] | Dutch
BEL | Belgium | Namur | Bruxelles [Brussel] | Dutch
BEL | Belgium | Namur | Bruxelles [Brussel] | French
BEL | Belgium | Namur | Bruxelles [Brussel] | French
BEL | Belgium | Namur | Bruxelles [Brussel] | French
BEL | Belgium | Namur | Bruxelles [Brussel] | German
BEL | Belgium | Namur | Bruxelles [Brussel] | German
BEL | Belgium | Namur | Bruxelles [Brussel] | German
BEL | Belgium | Schaerbeek | Bruxelles [Brussel] | Dutch
BEL | Belgium | Schaerbeek | Bruxelles [Brussel] | Dutch
BEL | Belgium | Schaerbeek | Bruxelles [Brussel] | Dutch
BEL | Belgium | Schaerbeek | Bruxelles [Brussel] | French
BEL | Belgium | Schaerbeek | Bruxelles [Brussel] | French
BEL | Belgium | Schaerbeek | Bruxelles [Brussel] | French
BEL | Belgium | Schaerbeek | Bruxelles [Brussel] | German
BEL | Belgium | Schaerbeek | Bruxelles [Brussel] | German
BEL | Belgium | Schaerbeek | Bruxelles [Brussel] | German
BGR | Bulgaria | Burgas | Sofija | Bulgariana
BGR | Bulgaria | Dobric | Sofija | Bulgariana
BGR | Bulgaria | Gumen | Sofija | Bulgariana
BGR | Bulgaria | Pleven | Sofija | Bulgariana
BGR | Bulgaria | Plovdiv | Sofija | Bulgariana
BGR | Bulgaria | Ruse | Sofija | Bulgariana
BGR | Bulgaria | Sliven | Sofija | Bulgariana
BGR | Bulgaria | Stara Zagora | Sofija | Bulgariana
BGR | Bulgaria | Varna | Sofija | Bulgariana
BIH | Bosnia and Herzegovina | Banja Luka | Sarajevo | Serbo-Croatian
BIH | Bosnia and Herzegovina | Zenica | Sarajevo | Serbo-Croatian
BLR | Belarus | Baranovitdi | Minsk | Belorussian
BLR | Belarus | Baranovitdi | Minsk | Belorussian
BLR | Belarus | Baranovitdi | Minsk | Russian
BLR | Belarus | Baranovitdi | Minsk | Russian
BLR | Belarus | Bobruisk | Minsk | Belorussian
BLR | Belarus | Bobruisk | Minsk | Belorussian
BLR | Belarus | Bobruisk | Minsk | Russian
BLR | Belarus | Bobruisk | Minsk | Russian
BLR | Belarus | Borisov | Minsk | Belorussian
BLR | Belarus | Borisov | Minsk | Belorussian
BLR | Belarus | Borisov | Minsk | Russian
BLR | Belarus | Borisov | Minsk | Russian
(100 rows)
--query2
with allcitystats as
( select city.name CITY,city.id,country.name COUNTRY,city.district,city.population as CITY_POP
from
city,country
where city.countrycode = country.code
),
alldistrictstats as
( select allcitystats.district,allcitystats.COUNTRY,sum(CITY_POP) DISTRICT_POP,
count(CITY) as D_CITY_CNT
from allcitystats
group by allcitystats.district,allcitystats.COUNTRY
order by district,COUNTRY
),
allcountrystats as
( select alldistrictstats.COUNTRY,country.code,sum(D_CITY_CNT) C_CITY_CNT,
count(distinct countrylanguage.language) C_LANG_CNT
from alldistrictstats,country,countrylanguage
where alldistrictstats.COUNTRY = country.name
and country.code = countrylanguage.countrycode
group by COUNTRY,code
),
asian_region_stats as
(
select sum(FOO.C_CITY_CNT) REGION_CITY_CNT,sum(FOO.C_LANG_CNT) REGION_LANG_CNT,FOO.region
FROM
(
select allcountrystats.code,allcountrystats.COUNTRY,C_CITY_CNT,C_LANG_CNT,country.region,city.name CAPITAL
from allcountrystats,country,city
where allcountrystats.code = country.code
and country.capital = city.id
and C_CITY_CNT/C_LANG_CNT > 1
and country.continent = 'Asia') FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region
)
select * from
(
select REGION_CITY_CNT as CITY_CNT,REGION_LANG_CNT as LANG_CNT, region as IDENTIFIER from asian_region_stats
UNION ALL
(
select sum(FOO.C_CITY_CNT) CITY_CNT,sum(FOO.C_LANG_CNT) LANG_CNT,FOO.region as IDENTIFIER
FROM
(
select allcountrystats.code,allcountrystats.COUNTRY,C_CITY_CNT,C_LANG_CNT,country.region,allcitystats.CITY CAPITAL
from allcountrystats,country,allcitystats
where allcountrystats.code = country.code
and country.capital = allcitystats.id
and C_CITY_CNT/C_LANG_CNT > 1
and country.continent = 'Europe') FOO
,allcountrystats,country
WHERE allcountrystats.code = country.code
and FOO.region = country.region
group by FOO.region order by FOO.region
)
) FOO1
order by FOO1.lang_cnt,FOO1.identifier;
city_cnt | lang_cnt | identifier
----------+----------+---------------------------
494 | 10 | British Islands
159 | 48 | Baltic Countries
1295 | 161 | Nordic Countries
50176 | 264 | Eastern Asia
9414 | 369 | Western Europe
11880 | 450 | Southern Europe
32900 | 610 | Eastern Europe
29161 | 616 | Southeast Asia
8568 | 792 | Middle East
80388 | 1092 | Southern and Central Asia
(10 rows)
-- queries using column lists for CTEs
-- query 1 use column list despite having no duplicate names
with capitals("C","ID","CAP") as
(select country.code,id,city.name from city,country
where city.countrycode = country.code AND city.id = country.capital),
lang_total(LC,CC,CLC) as
( select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
select country.name,"C","ID","CAP",lang_total.lc
from capitals,lang_total,country
where capitals."C" = country.code
and country.code = lang_total.cc;
name | C | ID | CAP | lc
---------------------------------+-----+------+------------------+----
Austria | AUT | 1523 | Wien | 8
Venezuela | VEN | 3539 | Caracas | 3
Russian Federation | RUS | 3580 | Moscow | 12
Bosnia and Herzegovina | BIH | 201 | Sarajevo | 1
Brazil | BRA | 211 | Brasilia | 5
Madagascar | MDG | 2455 | Antananarivo | 2
Mexico | MEX | 2515 | Ciudad de Mexico | 6
Argentina | ARG | 69 | Buenos Aires | 3
Azerbaijan | AZE | 144 | Baku | 4
United States | USA | 3813 | Washington | 12
Yugoslavia | YUG | 1792 | Beograd | 6
Micronesia, Federated States of | FSM | 2689 | Palikir | 6
Nigeria | NGA | 2754 | Abuja | 10
Germany | DEU | 3068 | Berlin | 6
India | IND | 1109 | New Delhi | 12
(15 rows)
-- query 2 Check case sensitivity for quoted names in column list. This should error out
with "lang_total"("LC",CC,CLC) as
( select count(*) as lang_count,country.code,countrylanguage.countrycode
from country join countrylanguage on (country.code=countrylanguage.countrycode and governmentform='Federal Republic')
group by country.code,countrylanguage.countrycode order by country.code)
select LC from lang_total;
ERROR: column "lc" does not exist
LINE 5: select LC from lang_total;
^
-- query 3 use column list when there are duplicate names within the CTE
with capitals("CO_C","C_ID","CAPITAL",country) as
(select country.code,id,city.name,country.name from city,country
where city.countrycode = country.code AND city.id = country.capital)
select "CO_C","C_ID","CAPITAL",COUNTRY from capitals where "CO_C"='SMR';
CO_C | C_ID | CAPITAL | country
------+------+------------+------------
SMR | 3171 | San Marino | San Marino
(1 row)
-- query4 use column list within another CTE
with capitals("CO_C","C_ID","CAPITAL",country) as
(select country.code,id,city.name,country.name from city,country
where city.countrycode = country.code AND city.id = country.capital),
south_asian_capitals(A_CO_C,"C_ID",A_C_NAME,A_CO_NAME) as
(select "CO_C","C_ID","CAPITAL",country from capitals,country where capitals."CO_C"=country.code and country.region = 'Southern and Central Asia')
select "a_co_c","C_ID",A_c_NaMe,"a_co_name" from south_asian_capitals order by A_CO_C;-- queries using CTEs in initplans and main plan
a_co_c | C_ID | a_c_name | a_co_name
--------+------+-----------+--------------
AFG | 1 | Kabul | Afghanistan
BGD | 150 | Dhaka | Bangladesh
BTN | 192 | Thimphu | Bhutan
IND | 1109 | New Delhi | India
IRN | 1380 | Teheran | Iran
KAZ | 1864 | Astana | Kazakstan
KGZ | 2253 | Bishkek | Kyrgyzstan
LKA | 3217 | Colombo | Sri Lanka
MDV | 2463 | Male | Maldives
NPL | 2729 | Kathmandu | Nepal
PAK | 2831 | Islamabad | Pakistan
TJK | 3261 | Dushanbe | Tajikistan
TKM | 3419 | Ashgabat | Turkmenistan
UZB | 3503 | Toskent | Uzbekistan
(14 rows)
--query1 using CTE in the select list(initplan) . One CTE using another CTE in it's initplan
with gnpstats as
(
select REG_GNP/REG_OLD_GNP as GNP_INDEX,region from
(select sum(gnpold) as REG_OLD_GNP,
sum(gnp) as REG_GNP,region
from country
group by region) FOO
where (case when (REG_GNP/REG_OLD_GNP) > 0 then 1.2 * (REG_GNP/REG_OLD_GNP) else null end) between 0.5 and 1.5
order by region
),
gnp_index_compare as
(
select (select max(GNP_INDEX) from gnpstats) M_GNP_IDX,(gnp/gnpold) as GNP_IDX,country.name from
country where country.continent = 'Asia'
)
select (select min(GNP_IDX) from gnp_index_compare) MIN_COUNTRY_GNP_IDX, (select max(GNP_INDEX) from gnpstats) MAX_REG_GNP_IDX,city.name CAPITAL,country.name COUNTRY
from city,country where city.id = country.capital and country.continent='Europe';
min_country_gnp_idx | max_reg_gnp_idx | capital | country
------------------------+--------------------+-----------------------------------+-------------------------------
0.39526143942847043283 | 1.2055929013175585 | Gibraltar | Gibraltar
0.39526143942847043283 | 1.2055929013175585 | Longyearbyen | Svalbard and Jan Mayen
0.39526143942847043283 | 1.2055929013175585 | Tallinn | Estonia
0.39526143942847043283 | 1.2055929013175585 | Kyiv | Ukraine
0.39526143942847043283 | 1.2055929013175585 | Riga | Latvia
0.39526143942847043283 | 1.2055929013175585 | Vaduz | Liechtenstein
0.39526143942847043283 | 1.2055929013175585 | Valletta | Malta
0.39526143942847043283 | 1.2055929013175585 | Monaco-Ville | Monaco
0.39526143942847043283 | 1.2055929013175585 | Ljubljana | Slovenia
0.39526143942847043283 | 1.2055929013175585 | Tirana | Albania
0.39526143942847043283 | 1.2055929013175585 | Luxembourg [Luxemburg/Letzebuerg] | Luxembourg
0.39526143942847043283 | 1.2055929013175585 | Praha | Czech Republic
0.39526143942847043283 | 1.2055929013175585 | Citta del Vaticano | Holy See (Vatican City State)
0.39526143942847043283 | 1.2055929013175585 | London | United Kingdom
0.39526143942847043283 | 1.2055929013175585 | Roma | Italy
0.39526143942847043283 | 1.2055929013175585 | Beograd | Yugoslavia
0.39526143942847043283 | 1.2055929013175585 | Athenai | Greece
0.39526143942847043283 | 1.2055929013175585 | Zagreb | Croatia
0.39526143942847043283 | 1.2055929013175585 | Warszawa | Poland
0.39526143942847043283 | 1.2055929013175585 | Bucuresti | Romania
0.39526143942847043283 | 1.2055929013175585 | Bratislava | Slovakia
0.39526143942847043283 | 1.2055929013175585 | Wien | Austria
0.39526143942847043283 | 1.2055929013175585 | Skopje | Macedonia
0.39526143942847043283 | 1.2055929013175585 | Paris | France
0.39526143942847043283 | 1.2055929013175585 | San Marino | San Marino
0.39526143942847043283 | 1.2055929013175585 | Bern | Switzerland
0.39526143942847043283 | 1.2055929013175585 | Moscow | Russian Federation
0.39526143942847043283 | 1.2055929013175585 | Amsterdam | Netherlands
0.39526143942847043283 | 1.2055929013175585 | Sofija | Bulgaria
0.39526143942847043283 | 1.2055929013175585 | Dublin | Ireland
0.39526143942847043283 | 1.2055929013175585 | Oslo | Norway
0.39526143942847043283 | 1.2055929013175585 | Lisboa | Portugal
0.39526143942847043283 | 1.2055929013175585 | Andorra la Vella | Andorra
0.39526143942847043283 | 1.2055929013175585 | Bruxelles [Brussel] | Belgium
0.39526143942847043283 | 1.2055929013175585 | Madrid | Spain
0.39526143942847043283 | 1.2055929013175585 | Vilnius | Lithuania
0.39526143942847043283 | 1.2055929013175585 | Stockholm | Sweden
0.39526143942847043283 | 1.2055929013175585 | Berlin | Germany
0.39526143942847043283 | 1.2055929013175585 | Helsinki [Helsingfors] | Finland
0.39526143942847043283 | 1.2055929013175585 | Kobenhavn | Denmark
0.39526143942847043283 | 1.2055929013175585 | Minsk | Belarus
0.39526143942847043283 | 1.2055929013175585 | Sarajevo | Bosnia and Herzegovina
0.39526143942847043283 | 1.2055929013175585 | Torshavn | Faroe Islands
0.39526143942847043283 | 1.2055929013175585 | Reykjavik | Iceland
0.39526143942847043283 | 1.2055929013175585 | Chisinau | Moldova
0.39526143942847043283 | 1.2055929013175585 | Budapest | Hungary
(46 rows)
--query2 using the CTE in the where clause(initplan) of the main query. One CTE using another CTE in it's where clause as it's initplan
with diversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) > 6) d
where d.countrycode = country.code and country.gnp > 100000),
notdiversecountries as
(select country.code,country.name,country.capital,d.CNT
from country,
(select countrylanguage.countrycode,count(*) as CNT from countrylanguage group by countrycode
HAVING count(*) < 3) d
where d.countrycode = country.code and country.gnp > 100000
and d.CNT < (select max(CNT) from diversecountries))
select LANG_CNT,name from
(
select count(*) LANG_CNT,country.name,country.code from country,countrylanguage
where country.code = countrylanguage.countrycode and country.continent = 'North America'
group by country.name,country.code
) FOO
where FOO.LANG_CNT between (select min(CNT) from notdiversecountries) AND (select max(CNT) from diversecountries);
lang_cnt | name
----------+----------------------------------
4 | Aruba
2 | El Salvador
1 | Saint Pierre and Miquelon
2 | Dominica
2 | Jamaica
6 | Panama
3 | Virgin Islands, U.S.
1 | Anguilla
4 | Belize
3 | Trinidad and Tobago
1 | Cayman Islands
2 | Bahamas
2 | Dominican Republic
2 | Greenland
2 | Martinique
2 | Saint Vincent and the Grenadines
2 | Saint Kitts and Nevis
4 | Nicaragua
2 | Saint Lucia
12 | United States
12 | Canada
3 | Netherlands Antilles
6 | Mexico
4 | Costa Rica
1 | Turks and Caicos Islands
2 | Puerto Rico
1 | Virgin Islands, British
5 | Guatemala
1 | Cuba
1 | Bermuda
1 | Montserrat
2 | Barbados
2 | Haiti
1 | Grenada
2 | Guadeloupe
4 | Honduras
2 | Antigua and Barbuda
(37 rows)
--query3 using CTE more than once in the same initplan and also more than once in the main query
with alleuropeanlanguages as
(select country.code,country.name COUNTRY, city.name CAPITAL, language, isofficial, percentage
FROM country,city,countrylanguage
WHERE country.code = countrylanguage.countrycode
and country.capital = city.id
and country.continent = 'Europe'),
all_official_languages as
(
select country.code,country.name,alleuropeanlanguages.language,alleuropeanlanguages.percentage
from
country,alleuropeanlanguages
where country.code = alleuropeanlanguages.code and isofficial = 'True'
and country.governmentform NOT IN (select 'Commonwealth of the US' UNION ALL select 'Monarchy (Sultanate)' UNION ALL select 'Monarchy')
and country.gnp > (select min(gnpold) from country where country.region = 'Western Europe')
)
SELECT avg(percentage),language
FROM
(
select country.name,city.name,percentage,language
from country,city,all_official_languages
where country.code = all_official_languages.code and country.capital = city.id and country.continent = 'Europe'
UNION ALL
select country.name,city.name,percentage,language
from country,city,all_official_languages
where country.code = all_official_languages.code and country.capital = city.id and country.continent = 'Asia'
) FOO
group by FOO.language
HAVING avg(percentage) >=
(select avg(percentage) from
( select percentage from all_official_languages,country where country.code = all_official_languages.code and country.region = 'British Islands'
UNION
select percentage from all_official_languages,country where country.code = all_official_languages.code and country.region = 'Western Europe'
) FOO )
order by FOO.language;
avg | language
------------------+----------------
97.9000015258789 | Albaniana
65.5999984741211 | Belorussian
83.1999969482422 | Bulgariana
81.1999969482422 | Czech
93.5 | Danish
77.3999996185303 | Dutch
65.9333348274231 | English
65.3000030517578 | Estonian
92.6999969482422 | Finnish
56.5333335796992 | German
98.5 | Greek
98.5 | Hungarian
95.6999969482422 | Icelandic
50.8999991416931 | Italian
55.0999984741211 | Latvian
81.5999984741211 | Lithuanian
64.4000015258789 | Luxembourgish
66.5 | Macedonian
95.8000030517578 | Maltese
96.5999984741211 | Norwegian
97.5999984741211 | Polish
99 | Portuguese
76.2999992370605 | Romanian
59.2999992370605 | Russian
90.0999984741211 | Serbo-Croatian
85.5999984741211 | Slovak
87.9000015258789 | Slovene
74.4000015258789 | Spanish
64.6999969482422 | Ukrainian
(29 rows)
-- queries using deeply nested CTEs
-- query1 using a CTE at every subquery level
select avg(population),CITY
from
(
with size0_cities(CITY,POPULATION) as (select city.name,city.population from city where city.population >= 100)
select CITY,POPULATION from
(
with size1_cities(CITY,POPULATION) as ( select city,population from size0_cities where population >= 1000 )
select CITY,POPULATION from
(
with size2_cities(CITY,POPULATION) as (select city,population from size1_cities where population >= 10000)
select CITY,POPULATION from
(
with size3_cities as (select city,population from size2_cities where population >= 20000)
select CITY,POPULATION from
(
with size4_cities as (select city,population from size3_cities where population >= 50000)
select CITY,POPULATION from
(
with size5_cities as (select city,population from size4_cities where population >= 80000)
select CITY,POPULATION from
(
with size6_cities as (select city,population from size5_cities where population >= 150000)
select CITY,POPULATION from
(
with size7_cities as (select city,population from size6_cities where population >= 200000)
select CITY,POPULATION from
(
with size8_cities as (select city,population from size7_cities where population >= 250000)
select CITY,POPULATION from
(
with size9_cities as (select city,population from size8_cities where population >= 300000)
select city,population from
(
with size10_cities as (select city,population from size9_cities where population >= 6500000)
select CITY,POPULATION from
(
with size11_cities as (select city,population from size10_cities where population >= 7000000)
select s1.city,s1.population from size11_cities s1,size10_cities s2
) FOO11
) FOO10
) FOO9
) FOO8
) FOO7
) FOO6
) FOO5
) FOO4
)FOO3
) FOO2
) FOO1 order by city
) FOO0 group by city order by city;
avg | city
-----------------------+------------------
8591309.000000000000 | Ciudad de Mexico
7206704.000000000000 | Delhi
8787958.000000000000 | Istanbul
9604900.000000000000 | Jakarta
9269265.000000000000 | Karachi
7285000.000000000000 | London
8389200.000000000000 | Moscow
10500000.000000000000 | Mumbai (Bombay)
8008278.000000000000 | New York
7472000.000000000000 | Peking
9968485.000000000000 | Sao Paulo
9981619.000000000000 | Seoul
9696300.000000000000 | Shanghai
7980230.000000000000 | Tokyo
(14 rows)
--query 2 deeply nested CTEs with shared scans in the plan
select avg(population),CITY
from
(
with size0_cities(CITY,POPULATION) as (select city.name,city.population from city where city.population >= 100)
select CITY,POPULATION from
(
with size1_cities(CITY,POPULATION) as ( select city,population from size0_cities where population >= 1000 )
select CITY,POPULATION from
(
with size2_cities(CITY,POPULATION) as (select city,population from size1_cities where population >= 10000)
select CITY,POPULATION from
(
with size3_cities as (select city,population from size2_cities where population >= 20000)
select CITY,POPULATION from
(
with size4_cities as (select city,population from size3_cities where population >= 50000)
select CITY,POPULATION from
(
with size5_cities as (select city,population from size4_cities where population >= 80000)
select CITY,POPULATION from
(
with size6_cities as (select city,population from size5_cities where population >= 150000)
select CITY,POPULATION from
(
with size7_cities as (select city,population from size6_cities where population >= 200000)
select CITY,POPULATION from
(
with size8_cities as (select city,population from size7_cities where population >= 250000)
select CITY,POPULATION from
(
with size9_cities as (select city,population from size8_cities where population >= 300000)
select city,population from
(
with size10_cities as (select city,population from size9_cities where population >= 6500000)
select CITY,POPULATION from
(
with size11_cities as (select city,population from size10_cities where population >= 7000000)
select s1.city,s1.population from size11_cities s1,size10_cities s2
) FOO11
) FOO10
) FOO9
) FOO8
) FOO7
) FOO6
) FOO5
) FOO4
)FOO3
) FOO2
) FOO1 order by city
) FOO0 group by city order by city;
avg | city
-----------------------+------------------
8591309.000000000000 | Ciudad de Mexico
7206704.000000000000 | Delhi
8787958.000000000000 | Istanbul
9604900.000000000000 | Jakarta
9269265.000000000000 | Karachi
7285000.000000000000 | London
8389200.000000000000 | Moscow
10500000.000000000000 | Mumbai (Bombay)
8008278.000000000000 | New York
7472000.000000000000 | Peking
9968485.000000000000 | Sao Paulo
9981619.000000000000 | Seoul
9696300.000000000000 | Shanghai
7980230.000000000000 | Tokyo
(14 rows)
-- query 3 deeply nested CTEs using every CTE defined
select avg(population) avg_p,CITY
from
(
with size0_cities(CITY,POPULATION) as (select city.name,city.population from city where city.population >= 350000)
select CITY,POPULATION from
(
with size1_cities(CITY,POPULATION) as ( select city,population from size0_cities where population >= 360000 )
select CITY,POPULATION from
(
with size2_cities(CITY,POPULATION) as (select city,population from size1_cities where population >= 370000)
select CITY,POPULATION from
(
with size3_cities as (select city,population from size2_cities where population >= 380000)
select CITY,POPULATION from
(
with size4_cities as (select city,population from size3_cities where population >= 390000)
select CITY,POPULATION from
(
with size5_cities as (select city,population from size4_cities where population >= 400000)
select CITY,POPULATION from
(
with size6_cities as (select city,population from size5_cities where population >= 410000)
select CITY,POPULATION from
(
with size7_cities as (select city,population from size6_cities where population >= 420000)
select CITY,POPULATION from
(
with size8_cities as (select city,population from size7_cities where population >= 430000)
select CITY,POPULATION from
(
with size9_cities as (select city,population from size8_cities where population >= 440000)
select city,population from
(
with size10_cities as (select city,population from size9_cities where population >= 6500000)
select CITY,POPULATION from
(
with size11_cities as (select city,population from size10_cities where population >= 7000000)
select s1.city,s1.population from size11_cities s1,size1_cities s2
UNION
select s1.city,s1.population from size10_cities s1,size2_cities s2
UNION
select s1.city,s1.population from size9_cities s1,size3_cities s2
UNION
select s1.city,s1.population from size8_cities s1,size4_cities s2
UNION
select s1.city,s1.population from size7_cities s1,size5_cities s2
UNION
select s1.city,s1.population from size6_cities s1,size6_cities s2
) FOO11
) FOO10
) FOO9
) FOO8
) FOO7
) FOO6
) FOO5
) FOO4
)FOO3
) FOO2
) FOO1 order by city
) FOO0 group by city order by avg_p,city
LIMIT 20;
avg_p | city
---------------------+----------------
410000.000000000000 | Port Harcourt
410102.000000000000 | Alberton
410407.000000000000 | Xiangfan
410775.000000000000 | Pingdingshan
411542.000000000000 | Bhubaneswar
411822.000000000000 | General Santos
412639.000000000000 | Kaunas
415346.000000000000 | San Salvador
415466.000000000000 | Panzhihua
416100.000000000000 | al-Taif
416289.000000000000 | Bikaner
416428.000000000000 | Matamoros
416474.000000000000 | Atlanta
416988.000000000000 | Szczecin
417517.000000000000 | Kisangani
417597.000000000000 | Sialkot
417610.000000000000 | Suez
417748.000000000000 | Rasht
417810.000000000000 | Kolwezi
418624.000000000000 | Kirkuk
(20 rows)
-- sanity tests with queries using CTEs in insert,update,delete and create
-- query 1 CTAS using CTE
create table bad_headofstates as
(
with bad_headofstates as
(
select country.code,country.name,country.headofstate,countrylanguage.language
from
country,countrylanguage
where country.code = countrylanguage.countrycode and countrylanguage.isofficial=true
and (country.gnp < country.gnpold or country.gnp < 3000)
)
select OUTERMOST_FOO.*,bad_headofstates.headofstate from (
select avg(population),region from
(
select FOO.*,bad_headofstates.headofstate,city.name
from
(select bad_headofstates.code,country.capital,country.region,country.population from
bad_headofstates,country where bad_headofstates.code = country.code) FOO, bad_headofstates,city
where FOO.code = bad_headofstates.code and FOO.capital = city.id) OUTER_FOO
group by region ) OUTERMOST_FOO,bad_headofstates,country
where country.code = bad_headofstates.code and country.region = OUTERMOST_FOO.region
order by OUTERMOST_FOO.region,bad_headofstates.headofstate LIMIT 40
);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause. Creating a NULL policy entry.
select * from bad_headofstates order by region,headofstate;
avg | region | headofstate
-----------------------+---------------------------+----------------------------------
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
744388.888888888889 | Caribbean | Beatrix
744388.888888888889 | Caribbean | Beatrix
744388.888888888889 | Caribbean | Beatrix
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Fidel Castro Ruz
744388.888888888889 | Caribbean | George W. Bush
744388.888888888889 | Caribbean | Jacques Chirac
7651000.000000000000 | Central Africa | Idriss Deby
2657500.000000000000 | Central America | Arnoldo Aleman Lacayo
2657500.000000000000 | Central America | Elisabeth II
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Bakili Muluzi
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | Isayas Afewerki [Isaias Afwerki]
5982809.523809523810 | Eastern Africa | Ismail Omar Guelleh
5982809.523809523810 | Eastern Africa | Jacques Chirac
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Robert G. Mugabe
34288500.000000000000 | Eastern Asia | Akihito
34288500.000000000000 | Eastern Asia | Chen Shui-bian
34288500.000000000000 | Eastern Asia | Jiang Zemin
(40 rows)
--query 2 insert using CTE
insert into bad_headofstates
(
with bad_headofstates as
(
select country.code,country.name,country.headofstate,countrylanguage.language
from
country,countrylanguage
where country.code = countrylanguage.countrycode and countrylanguage.isofficial=true
and (country.gnp < country.gnpold or country.gnp < 3000)
)
select OUTERMOST_FOO.*,bad_headofstates.headofstate from (
select avg(population),region from
(
select FOO.*,bad_headofstates.headofstate,city.name
from
(select bad_headofstates.code,country.capital,country.region,country.population from
bad_headofstates,country where bad_headofstates.code = country.code) FOO, bad_headofstates,city
where FOO.code = bad_headofstates.code and FOO.capital = city.id) OUTER_FOO
group by region ) OUTERMOST_FOO,bad_headofstates,country
where country.code = bad_headofstates.code and country.region = OUTERMOST_FOO.region
order by OUTERMOST_FOO.region,bad_headofstates.headofstate LIMIT 40
);
select * from bad_headofstates order by region,headofstate;
avg | region | headofstate
-----------------------+---------------------------+----------------------------------
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
744388.888888888889 | Caribbean | Beatrix
744388.888888888889 | Caribbean | Beatrix
744388.888888888889 | Caribbean | Beatrix
744388.888888888889 | Caribbean | Beatrix
744388.888888888889 | Caribbean | Beatrix
744388.888888888889 | Caribbean | Beatrix
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Elisabeth II
744388.888888888889 | Caribbean | Fidel Castro Ruz
744388.888888888889 | Caribbean | Fidel Castro Ruz
744388.888888888889 | Caribbean | George W. Bush
744388.888888888889 | Caribbean | George W. Bush
744388.888888888889 | Caribbean | Jacques Chirac
744388.888888888889 | Caribbean | Jacques Chirac
7651000.000000000000 | Central Africa | Idriss Deby
7651000.000000000000 | Central Africa | Idriss Deby
2657500.000000000000 | Central America | Arnoldo Aleman Lacayo
2657500.000000000000 | Central America | Arnoldo Aleman Lacayo
2657500.000000000000 | Central America | Elisabeth II
2657500.000000000000 | Central America | Elisabeth II
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Bakili Muluzi
5982809.523809523810 | Eastern Africa | Bakili Muluzi
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | Isayas Afewerki [Isaias Afwerki]
5982809.523809523810 | Eastern Africa | Isayas Afewerki [Isaias Afwerki]
5982809.523809523810 | Eastern Africa | Ismail Omar Guelleh
5982809.523809523810 | Eastern Africa | Ismail Omar Guelleh
5982809.523809523810 | Eastern Africa | Jacques Chirac
5982809.523809523810 | Eastern Africa | Jacques Chirac
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Robert G. Mugabe
5982809.523809523810 | Eastern Africa | Robert G. Mugabe
34288500.000000000000 | Eastern Asia | Akihito
34288500.000000000000 | Eastern Asia | Akihito
34288500.000000000000 | Eastern Asia | Chen Shui-bian
34288500.000000000000 | Eastern Asia | Chen Shui-bian
34288500.000000000000 | Eastern Asia | Jiang Zemin
34288500.000000000000 | Eastern Asia | Jiang Zemin
(80 rows)
--query3 update using CTE
update bad_headofstates set region = cm.region FROM
(
with bad_headofstates as
(
select country.code,country.name,country.headofstate,countrylanguage.language
from
country,countrylanguage
where country.code = countrylanguage.countrycode and countrylanguage.isofficial=true
and (country.gnp < country.gnpold or country.gnp < 3000)
)
select avg(OUTERMOST_FOO.AVG),OUTERMOST_FOO.region from (
select avg(population) AVG,region from
(
select FOO.*,bad_headofstates.headofstate,city.name
from
(select bad_headofstates.code,country.capital,country.region,country.population from
bad_headofstates,country where bad_headofstates.code = country.code) FOO, bad_headofstates,city
where FOO.code = bad_headofstates.code and FOO.capital = city.id) OUTER_FOO
group by region ) OUTERMOST_FOO,bad_headofstates,country
where country.code = bad_headofstates.code and country.region = OUTERMOST_FOO.region
and OUTERMOST_FOO.region = 'Central America'
group by OUTERMOST_FOO.region
order by OUTERMOST_FOO.region
) cm
where bad_headofstates.region = 'Caribbean';
select * from bad_headofstates order by avg,region,headofstate;
avg | region | headofstate
-----------------------+---------------------------+----------------------------------
744388.888888888889 | Central America | Beatrix
744388.888888888889 | Central America | Beatrix
744388.888888888889 | Central America | Beatrix
744388.888888888889 | Central America | Beatrix
744388.888888888889 | Central America | Beatrix
744388.888888888889 | Central America | Beatrix
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Elisabeth II
744388.888888888889 | Central America | Fidel Castro Ruz
744388.888888888889 | Central America | Fidel Castro Ruz
744388.888888888889 | Central America | George W. Bush
744388.888888888889 | Central America | George W. Bush
744388.888888888889 | Central America | Jacques Chirac
744388.888888888889 | Central America | Jacques Chirac
2657500.000000000000 | Central America | Arnoldo Aleman Lacayo
2657500.000000000000 | Central America | Arnoldo Aleman Lacayo
2657500.000000000000 | Central America | Elisabeth II
2657500.000000000000 | Central America | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Bakili Muluzi
5982809.523809523810 | Eastern Africa | Bakili Muluzi
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | Isayas Afewerki [Isaias Afwerki]
5982809.523809523810 | Eastern Africa | Isayas Afewerki [Isaias Afwerki]
5982809.523809523810 | Eastern Africa | Ismail Omar Guelleh
5982809.523809523810 | Eastern Africa | Ismail Omar Guelleh
5982809.523809523810 | Eastern Africa | Jacques Chirac
5982809.523809523810 | Eastern Africa | Jacques Chirac
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Robert G. Mugabe
5982809.523809523810 | Eastern Africa | Robert G. Mugabe
7651000.000000000000 | Central Africa | Idriss Deby
7651000.000000000000 | Central Africa | Idriss Deby
34288500.000000000000 | Eastern Asia | Akihito
34288500.000000000000 | Eastern Asia | Akihito
34288500.000000000000 | Eastern Asia | Chen Shui-bian
34288500.000000000000 | Eastern Asia | Chen Shui-bian
34288500.000000000000 | Eastern Asia | Jiang Zemin
34288500.000000000000 | Eastern Asia | Jiang Zemin
(80 rows)
--query4 delete using CTE
delete from bad_headofstates USING
(
with bad_headofstates as
(
select country.code,country.name,country.headofstate,countrylanguage.language
from
country,countrylanguage
where country.code = countrylanguage.countrycode and countrylanguage.isofficial=true
and (country.gnp < country.gnpold or country.gnp < 3000)
)
select avg(OUTERMOST_FOO.AVG),OUTERMOST_FOO.region from (
select avg(population) AVG,region from
(
select FOO.*,bad_headofstates.headofstate,city.name
from
(select bad_headofstates.code,country.capital,country.region,country.population from
bad_headofstates,country where bad_headofstates.code = country.code) FOO, bad_headofstates,city
where FOO.code = bad_headofstates.code and FOO.capital = city.id) OUTER_FOO
group by region ) OUTERMOST_FOO,bad_headofstates,country
where country.code = bad_headofstates.code and country.region = OUTERMOST_FOO.region
and OUTERMOST_FOO.region = 'Central America'
group by OUTERMOST_FOO.region
order by OUTERMOST_FOO.region
) as cm
where bad_headofstates.region = cm.region;
select * from bad_headofstates order by region,headofstate;
avg | region | headofstate
-----------------------+---------------------------+----------------------------------
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
7651000.000000000000 | Central Africa | Idriss Deby
7651000.000000000000 | Central Africa | Idriss Deby
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Abdiqassim Salad Hassan
5982809.523809523810 | Eastern Africa | Bakili Muluzi
5982809.523809523810 | Eastern Africa | Bakili Muluzi
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | France-Albert Rene
5982809.523809523810 | Eastern Africa | Isayas Afewerki [Isaias Afwerki]
5982809.523809523810 | Eastern Africa | Isayas Afewerki [Isaias Afwerki]
5982809.523809523810 | Eastern Africa | Ismail Omar Guelleh
5982809.523809523810 | Eastern Africa | Ismail Omar Guelleh
5982809.523809523810 | Eastern Africa | Jacques Chirac
5982809.523809523810 | Eastern Africa | Jacques Chirac
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Paul Kagame
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Pierre Buyoya
5982809.523809523810 | Eastern Africa | Robert G. Mugabe
5982809.523809523810 | Eastern Africa | Robert G. Mugabe
34288500.000000000000 | Eastern Asia | Akihito
34288500.000000000000 | Eastern Asia | Akihito
34288500.000000000000 | Eastern Asia | Chen Shui-bian
34288500.000000000000 | Eastern Asia | Chen Shui-bian
34288500.000000000000 | Eastern Asia | Jiang Zemin
34288500.000000000000 | Eastern Asia | Jiang Zemin
(44 rows)
-- delete using CTE in its subplan
delete from bad_headofstates where bad_headofstates.avg NOT IN
(
with bad_headofstates as
(
select country.code,country.name,country.headofstate,countrylanguage.language
from
country,countrylanguage
where country.code = countrylanguage.countrycode and countrylanguage.isofficial=true
and (country.gnp < country.gnpold or country.gnp < 3000)
)
select OUTERMOST_FOO.AVG from (
select avg(population) AVG,region from
(
select FOO.*,bad_headofstates.headofstate,city.name
from
(select bad_headofstates.code,country.capital,country.region,country.population from
bad_headofstates,country where bad_headofstates.code = country.code) FOO, bad_headofstates,city
where FOO.code = bad_headofstates.code and FOO.capital = city.id) OUTER_FOO
group by region ) OUTERMOST_FOO,bad_headofstates,country
where country.code = bad_headofstates.code and country.region = OUTERMOST_FOO.region
and OUTERMOST_FOO.region = 'Australia and New Zealand' or OUTERMOST_FOO.region = 'Eastern Asia'
order by OUTERMOST_FOO.region
);
select * from bad_headofstates order by region,headofstate;
avg | region | headofstate
-----------------------+---------------------------+----------------
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
4550620.000000000000 | Australia and New Zealand | Elisabeth II
34288500.000000000000 | Eastern Asia | Akihito
34288500.000000000000 | Eastern Asia | Akihito
34288500.000000000000 | Eastern Asia | Chen Shui-bian
34288500.000000000000 | Eastern Asia | Chen Shui-bian
34288500.000000000000 | Eastern Asia | Jiang Zemin
34288500.000000000000 | Eastern Asia | Jiang Zemin
(16 rows)
drop table bad_headofstates;
-- queries with views using CTEs
-- view1 with multiple CTEs being used multiple times
create view view_with_shared_scans as
(
with longlivingregions as
(
select FOO.*,count(distinct language) as "lang_count"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
avg(lifeexpectancy) as "REGION_LIFETIME",region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP",foo."REGION_LIFETIME"),
denseregions as
(
select FOO.*,count(distinct language) as "lang_count",
sum(surfacearea) as "REGION_SURFACE_AREA"
from(
select
sum(population) as "REGION_POP",
sum(gnp) as "REGION_GNP",
region
from
country
group by region
) FOO,countrylanguage,country
where
country.code = countrylanguage.countrycode
and FOO.region = country.region
and FOO."REGION_POP" != 0
group by
FOO.region,foo."REGION_POP",foo."REGION_GNP"
order by sum(surfacearea)/foo."REGION_POP" desc),
allcountrystats as
( select country.code,country.name,count(distinct city.id) CITY_CNT,
count(distinct countrylanguage.language) LANG_CNT
from country,city,countrylanguage
where country.code = city.countrycode
and country.code = countrylanguage.countrycode
group by country.code,country.name
)
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1800 and 1850
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear between 1850 and 1900
UNION ALL
select allcountrystats.CITY_CNT,allcountrystats.LANG_CNT,allcountrystats.name,
"REGION_SURFACE_AREA","REGION_LIFETIME",longlivingregions."REGION_POP",longlivingregions.lang_count,longlivingregions."REGION_GNP",longlivingregions.region
from longlivingregions,denseregions,allcountrystats,country
where longlivingregions.region = denseregions.region and allcountrystats.code = country.code and country.region = longlivingregions.region
and country.indepyear > 1900
);
\d+ view_with_shared_scans;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Non-default collation
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Queries on master-only tables
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Non-default collation
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Non-default collation
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Non-default collation
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Non-default collation
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Queries on master-only tables
View "qp_with_clause.view_with_shared_scans"
Column | Type | Collation | Nullable | Default | Storage | Description
---------------------+------------------+-----------+----------+---------+----------+-------------
city_cnt | bigint | | | | plain |
lang_cnt | bigint | | | | plain |
name | text | | | | extended |
REGION_SURFACE_AREA | numeric | | | | main |
REGION_LIFETIME | double precision | | | | plain |
REGION_POP | bigint | | | | plain |
lang_count | bigint | | | | plain |
REGION_GNP | numeric | | | | main |
region | text | | | | extended |
View definition:
WITH longlivingregions AS (
SELECT foo."REGION_POP",
foo."REGION_GNP",
foo."REGION_LIFETIME",
foo.region,
count(DISTINCT countrylanguage.language) AS lang_count
FROM ( SELECT sum(country_1.population) AS "REGION_POP",
sum(country_1.gnp) AS "REGION_GNP",
avg(country_1.lifeexpectancy) AS "REGION_LIFETIME",
country_1.region
FROM country country_1
GROUP BY country_1.region) foo,
countrylanguage,
country
WHERE country.code = countrylanguage.countrycode AND foo.region = country.region
GROUP BY foo.region, foo."REGION_POP", foo."REGION_GNP", foo."REGION_LIFETIME"
), denseregions AS (
SELECT foo."REGION_POP",
foo."REGION_GNP",
foo.region,
count(DISTINCT countrylanguage.language) AS lang_count,
sum(country.surfacearea) AS "REGION_SURFACE_AREA"
FROM ( SELECT sum(country_1.population) AS "REGION_POP",
sum(country_1.gnp) AS "REGION_GNP",
country_1.region
FROM country country_1
GROUP BY country_1.region) foo,
countrylanguage,
country
WHERE country.code = countrylanguage.countrycode AND foo.region = country.region AND foo."REGION_POP" <> 0
GROUP BY foo.region, foo."REGION_POP", foo."REGION_GNP"
ORDER BY (sum(country.surfacearea) / foo."REGION_POP"::numeric) DESC
), allcountrystats AS (
SELECT country.code,
country.name,
count(DISTINCT city.id) AS city_cnt,
count(DISTINCT countrylanguage.language) AS lang_cnt
FROM country,
city,
countrylanguage
WHERE country.code = city.countrycode AND country.code = countrylanguage.countrycode
GROUP BY country.code, country.name
)
SELECT allcountrystats.city_cnt,
allcountrystats.lang_cnt,
allcountrystats.name,
denseregions."REGION_SURFACE_AREA",
longlivingregions."REGION_LIFETIME",
longlivingregions."REGION_POP",
longlivingregions.lang_count,
longlivingregions."REGION_GNP",
longlivingregions.region
FROM longlivingregions,
denseregions,
allcountrystats,
country
WHERE longlivingregions.region = denseregions.region AND allcountrystats.code = country.code AND country.region = longlivingregions.region AND country.indepyear >= 1800 AND country.indepyear <= 1850
UNION ALL
SELECT allcountrystats.city_cnt,
allcountrystats.lang_cnt,
allcountrystats.name,
denseregions."REGION_SURFACE_AREA",
longlivingregions."REGION_LIFETIME",
longlivingregions."REGION_POP",
longlivingregions.lang_count,
longlivingregions."REGION_GNP",
longlivingregions.region
FROM longlivingregions,
denseregions,
allcountrystats,
country
WHERE longlivingregions.region = denseregions.region AND allcountrystats.code = country.code AND country.region = longlivingregions.region AND country.indepyear >= 1850 AND country.indepyear <= 1900
UNION ALL
SELECT allcountrystats.city_cnt,
allcountrystats.lang_cnt,
allcountrystats.name,
denseregions."REGION_SURFACE_AREA",
longlivingregions."REGION_LIFETIME",
longlivingregions."REGION_POP",
longlivingregions.lang_count,
longlivingregions."REGION_GNP",
longlivingregions.region
FROM longlivingregions,
denseregions,
allcountrystats,
country
WHERE longlivingregions.region = denseregions.region AND allcountrystats.code = country.code AND country.region = longlivingregions.region AND country.indepyear > 1900;
select city_cnt,lang_cnt,name,region from view_with_shared_scans order by name LIMIT 50;
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
city_cnt | lang_cnt | name | region
----------+----------+---------------------------------------+---------------------------
4 | 5 | Afghanistan | Southern and Central Asia
1 | 3 | Albania | Southern Europe
18 | 2 | Algeria | Northern Africa
5 | 9 | Angola | Central Africa
1 | 2 | Antigua and Barbuda | Caribbean
57 | 3 | Argentina | South America
3 | 2 | Armenia | Middle East
14 | 8 | Australia | Australia and New Zealand
6 | 8 | Austria | Western Europe
4 | 4 | Azerbaijan | Middle East
1 | 2 | Bahamas | Caribbean
1 | 2 | Bahrain | Middle East
24 | 7 | Bangladesh | Southern and Central Asia
1 | 2 | Barbados | Caribbean
16 | 4 | Belarus | Eastern Europe
9 | 6 | Belgium | Western Europe
2 | 4 | Belize | Central America
4 | 7 | Benin | Western Africa
1 | 3 | Bhutan | Southern and Central Asia
8 | 4 | Bolivia | South America
3 | 1 | Bosnia and Herzegovina | Southern Europe
2 | 5 | Botswana | Southern Africa
250 | 5 | Brazil | South America
1 | 4 | Brunei | Southeast Asia
10 | 4 | Bulgaria | Eastern Europe
3 | 6 | Burkina Faso | Western Africa
1 | 3 | Burundi | Eastern Africa
3 | 4 | Cambodia | Southeast Asia
7 | 8 | Cameroon | Central Africa
49 | 12 | Canada | North America
1 | 2 | Cape Verde | Western Africa
1 | 6 | Central African Republic | Central Africa
2 | 8 | Chad | Central Africa
29 | 4 | Chile | South America
38 | 5 | Colombia | South America
1 | 5 | Comoros | Eastern Africa
2 | 6 | Congo | Central Africa
18 | 10 | Congo, The Democratic Republic of the | Central Africa
1 | 4 | Costa Rica | Central America
5 | 5 | Cote deIvoire | Western Africa
4 | 2 | Croatia | Southern Europe
14 | 1 | Cuba | Caribbean
2 | 2 | Cyprus | Middle East
10 | 8 | Czech Republic | Eastern Europe
1 | 3 | Djibouti | Eastern Africa
1 | 2 | Dominica | Caribbean
6 | 2 | Dominican Republic | Caribbean
15 | 2 | Ecuador | South America
37 | 2 | Egypt | Northern Africa
7 | 2 | El Salvador | Central America
(50 rows)
select city_cnt,lang_cnt,name,"REGION_POP","REGION_GNP",region from view_with_shared_scans where region = 'Eastern Europe';
INFO: GPORCA failed to produce a plan, falling back to Postgres-based planner
DETAIL: Falling back to Postgres-based planner because GPORCA does not support the following feature: Multiple Distinct Qualified Aggregates are disabled in the optimizer
city_cnt | lang_cnt | name | REGION_POP | REGION_GNP | region
----------+----------+--------------------+------------+------------+----------------
189 | 12 | Russian Federation | 307026000 | 659980.00 | Eastern Europe
3 | 5 | Slovakia | 307026000 | 659980.00 | Eastern Europe
10 | 8 | Czech Republic | 307026000 | 659980.00 | Eastern Europe
44 | 4 | Poland | 307026000 | 659980.00 | Eastern Europe
16 | 4 | Belarus | 307026000 | 659980.00 | Eastern Europe
10 | 4 | Bulgaria | 307026000 | 659980.00 | Eastern Europe
29 | 6 | Romania | 307026000 | 659980.00 | Eastern Europe
57 | 7 | Ukraine | 307026000 | 659980.00 | Eastern Europe
9 | 6 | Hungary | 307026000 | 659980.00 | Eastern Europe
4 | 5 | Moldova | 307026000 | 659980.00 | Eastern Europe
(10 rows)
drop view view_with_shared_scans;
-- start_ignore
drop table if exists tbl87;
NOTICE: table "tbl87" does not exist, skipping
-- end_ignore
create table tbl87(code char(3), n numeric);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'code' as the Greenplum Database data distribution key for this table.
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
insert into tbl87 values ('abc',1);
insert into tbl87 values ('xyz',2);
insert into tbl87 values ('def',3);
with cte as
(
select code, n, x
from tbl87
, (select 100 as x) d
)
select code from tbl87 t where 1= (select count(*) from cte where cte.code::text=t.code::text or cte.code::text = t.code::text);
code
------
abc
xyz
def
(3 rows)
with cte as
(
select count(*) from
(
select code, n, x
from tbl87
, (select 100 as x) d
) FOO
)
select code from tbl87 t where 1= (select * from cte);
code
------
(0 rows)
with cte as
(
select count(*) from
(
select code, n, x
from tbl87
, (select 100 as x) d
) FOO
)
select code from tbl87 t where 1= (select count(*) from cte);
code
------
abc
xyz
def
(3 rows)
--start_ignore
drop table if exists foo;
NOTICE: table "foo" does not exist, skipping
drop table if exists bar;
NOTICE: table "bar" does not exist, skipping
drop table if exists emp;
NOTICE: table "emp" does not exist, skipping
drop table if exists manager;
NOTICE: table "manager" does not exist, skipping
--end_ignore
-------------------------------------------------------------------------------------------------------------------------------
CREATE TABLE foo (key INTEGER, value INTEGER);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'key' as the Greenplum Database data distribution key for this table.
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
INSERT INTO foo SELECT i, i % 10 from generate_series(1, 100) i;
CREATE TABLE bar(bar_key INTEGER, bar_value INTEGER);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'bar_key' as the Greenplum Database data distribution key for this table.
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
INSERT INTO bar SELECT i, i % 5 FROM generate_series(1, 1000) i;
SET enable_hashjoin = OFF;
SET enable_mergejoin = OFF;
SET enable_nestloop = ON;
-------------------
-- QUERY 1
-------------------
-- Query has WITH clause
-- WITH clause subquery used once
-- The subquery in the WITH clause appears under a NLJ in the query plan
-- The sharing of CTE is disabled
SET gp_cte_sharing = OFF;
-- EXPLAIN
-- WITH o AS (SELECT * FROM foo AS o_key)
-- SELECT o.key, bar_value FROM o, bar
-- WHERE
-- o.key != bar_value
-- ORDER BY 1, 2 LIMIT 20;
WITH o AS (SELECT * FROM foo AS o_key)
SELECT o.key, bar_value FROM o, bar
WHERE
o.key != bar_value
ORDER BY 1, 2 LIMIT 20;
key | bar_value
-----+-----------
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
(20 rows)
-- Query has WITH clause
-- WITH clause subquery used once
-- The subquery in the WITH clause appears under a NLJ in the query plan
-- The sharing of CTE is disabled
SET gp_cte_sharing = ON;
-- EXPLAIN
-- WITH o AS (SELECT * FROM foo AS o_key)
-- SELECT o.key, bar_value FROM o, bar
-- WHERE
-- o.key != bar_value
-- ORDER BY 1, 2 LIMIT 20;
WITH o AS (SELECT * FROM foo AS o_key)
SELECT o.key, bar_value FROM o, bar
WHERE
o.key != bar_value
ORDER BY 1, 2 LIMIT 20;
key | bar_value
-----+-----------
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
(20 rows)
-------------------
-- Query 2, Query 3
-------------------
-- Query has WITH clause
-- WITH clause subquery used multiple times
-- The subquery in the WITH clause appears under a NLJ in the query plan
-- The sharing of CTE is disabled
SET gp_cte_sharing = OFF;
-- EXPLAIN
-- WITH o as (SELECT * FROM foo AS o_key)
-- SELECT o1.key, o2.value FROM o o1, o o2
-- WHERE o1.key != o2.value ORDER BY 1, 2 DESC LIMIT 100;
WITH o as (SELECT * FROM foo AS o_key)
SELECT o1.key, o2.value FROM o o1, o o2
WHERE o1.key != o2.value ORDER BY 1, 2 DESC LIMIT 100;
key | value
-----+-------
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
(100 rows)
-- EXPLAIN
-- WITH o AS (SELECT * FROM foo),
-- n AS (SELECT * FROM foo)
-- SELECT o.key , n.value
-- FROM
-- o JOIN n ON o.key = n.key
-- WHERE
-- o.key != n.value
-- ORDER BY 1, 2;
WITH o AS (SELECT * FROM foo),
n AS (SELECT * FROM foo)
SELECT o.key , n.value
FROM
o JOIN n ON o.key = n.key
WHERE
o.key != n.value
ORDER BY 1, 2;
key | value
-----+-------
10 | 0
11 | 1
12 | 2
13 | 3
14 | 4
15 | 5
16 | 6
17 | 7
18 | 8
19 | 9
20 | 0
21 | 1
22 | 2
23 | 3
24 | 4
25 | 5
26 | 6
27 | 7
28 | 8
29 | 9
30 | 0
31 | 1
32 | 2
33 | 3
34 | 4
35 | 5
36 | 6
37 | 7
38 | 8
39 | 9
40 | 0
41 | 1
42 | 2
43 | 3
44 | 4
45 | 5
46 | 6
47 | 7
48 | 8
49 | 9
50 | 0
51 | 1
52 | 2
53 | 3
54 | 4
55 | 5
56 | 6
57 | 7
58 | 8
59 | 9
60 | 0
61 | 1
62 | 2
63 | 3
64 | 4
65 | 5
66 | 6
67 | 7
68 | 8
69 | 9
70 | 0
71 | 1
72 | 2
73 | 3
74 | 4
75 | 5
76 | 6
77 | 7
78 | 8
79 | 9
80 | 0
81 | 1
82 | 2
83 | 3
84 | 4
85 | 5
86 | 6
87 | 7
88 | 8
89 | 9
90 | 0
91 | 1
92 | 2
93 | 3
94 | 4
95 | 5
96 | 6
97 | 7
98 | 8
99 | 9
100 | 0
(91 rows)
-- Query has WITH clause
-- Subquery using WITH clause multiple times
-- The subquery in the WITH clause appears under a NLJ in the query plan
-- The sharing of CTE is enabled
SET gp_cte_sharing = ON;
--EXPLAIN
--WITH o AS (SELECT * FROM foo AS o_key)
-- SELECT o1.key, o2.value FROM o o1, o o2
-- WHERE o1.key != o2.value ORDER BY 1, 2 DESC LIMIT 100;
WITH o AS (SELECT * FROM foo AS o_key)
SELECT o1.key, o2.value FROM o o1, o o2
WHERE o1.key != o2.value ORDER BY 1, 2 DESC LIMIT 100;
key | value
-----+-------
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
(100 rows)
-- EXPLAIN
-- WITH o AS (SELECT * FROM foo),
-- n AS (SELECT * FROM foo)
-- SELECT o.key , n.value
-- FROM
-- o JOIN n ON o.key = n.key
-- WHERE
-- o.key != n.value
-- ORDER BY 1, 2;
WITH o AS (SELECT * FROM foo),
n AS (SELECT * FROM foo)
SELECT o.key , n.value
FROM
o JOIN n ON o.key = n.key
WHERE
o.key != n.value
ORDER BY 1, 2;
key | value
-----+-------
10 | 0
11 | 1
12 | 2
13 | 3
14 | 4
15 | 5
16 | 6
17 | 7
18 | 8
19 | 9
20 | 0
21 | 1
22 | 2
23 | 3
24 | 4
25 | 5
26 | 6
27 | 7
28 | 8
29 | 9
30 | 0
31 | 1
32 | 2
33 | 3
34 | 4
35 | 5
36 | 6
37 | 7
38 | 8
39 | 9
40 | 0
41 | 1
42 | 2
43 | 3
44 | 4
45 | 5
46 | 6
47 | 7
48 | 8
49 | 9
50 | 0
51 | 1
52 | 2
53 | 3
54 | 4
55 | 5
56 | 6
57 | 7
58 | 8
59 | 9
60 | 0
61 | 1
62 | 2
63 | 3
64 | 4
65 | 5
66 | 6
67 | 7
68 | 8
69 | 9
70 | 0
71 | 1
72 | 2
73 | 3
74 | 4
75 | 5
76 | 6
77 | 7
78 | 8
79 | 9
80 | 0
81 | 1
82 | 2
83 | 3
84 | 4
85 | 5
86 | 6
87 | 7
88 | 8
89 | 9
90 | 0
91 | 1
92 | 2
93 | 3
94 | 4
95 | 5
96 | 6
97 | 7
98 | 8
99 | 9
100 | 0
(91 rows)
-------------------
-- Query 4, Query 5
-------------------
-- Query has WITH clause
-- Subquery with nested WITH clause appears under a NLJ in the query plan
-- The sharing of CTE is disabled
SET gp_cte_sharing = OFF;
-- EXPLAIN
-- WITH o AS (SELECT * FROM foo AS o_key),
-- m AS (SELECT * FROM o WHERE o.key < 50)
-- SELECT m.key, bar_value FROM m, bar
-- WHERE
-- m.key != bar_value
-- ORDER BY 1, 2 LIMIT 20;
WITH o AS (SELECT * FROM foo AS o_key),
m AS (SELECT * FROM o WHERE o.key < 50)
SELECT m.key, bar_value FROM m, bar
WHERE
m.key != bar_value
ORDER BY 1, 2 LIMIT 20;
key | bar_value
-----+-----------
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
(20 rows)
-- EXPLAIN
-- WITH o AS (SELECT * FROM foo AS o_key),
-- m AS (SELECT * FROM o WHERE o.key < 50)
-- SELECT m1.key, m2.value FROM m m1, m m2
-- WHERE m1.key != m2.value
-- ORDER BY 1, 2 DESC LIMIT 100;
WITH o AS (SELECT * FROM foo AS o_key),
m AS (SELECT * FROM o WHERE o.key < 50)
SELECT m1.key, m2.value FROM m m1, m m2
WHERE m1.key != m2.value
ORDER BY 1, 2 DESC LIMIT 100;
key | value
-----+-------
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 0
1 | 0
1 | 0
1 | 0
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
2 | 8
2 | 8
2 | 8
2 | 8
2 | 8
2 | 7
2 | 7
2 | 7
2 | 7
2 | 7
2 | 6
2 | 6
2 | 6
2 | 6
2 | 6
2 | 5
2 | 5
2 | 5
2 | 5
2 | 5
2 | 4
2 | 4
2 | 4
2 | 4
2 | 4
2 | 3
2 | 3
2 | 3
2 | 3
2 | 3
2 | 1
2 | 1
2 | 1
2 | 1
2 | 1
2 | 0
2 | 0
2 | 0
2 | 0
3 | 9
3 | 9
3 | 9
3 | 9
3 | 9
3 | 8
3 | 8
3 | 8
3 | 8
3 | 8
3 | 7
3 | 7
(100 rows)
-- Query has WITH clause
-- Subquery with nested WITH clause appears under a NLJ in the query plan
-- The sharing of CTE is enabled
SET gp_cte_sharing = ON;
--EXPLAIN
--WITH o AS (SELECT * FROM foo AS o_key),
--m AS (SELECT * FROM o WHERE o.key < 50)
-- SELECT m.key, bar_value FROM m, bar
--WHERE
--m.key != bar_value
--ORDER BY 1, 2 LIMIT 20;
WITH o AS (SELECT * FROM foo AS o_key),
m AS (SELECT * FROM o WHERE o.key < 50)
SELECT m.key, bar_value FROM m, bar
WHERE
m.key != bar_value
ORDER BY 1, 2 LIMIT 20;
key | bar_value
-----+-----------
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
1 | 0
(20 rows)
-- EXPLAIN
-- WITH o AS (SELECT * FROM foo AS o_key),
-- m AS (SELECT * from o WHERE o.key < 50)
-- SELECT m1.key, m2.value FROM m m1, m m2
-- WHERE m1.key != m2.value
-- ORDER BY 1, 2 DESC LIMIT 100;
WITH o AS (SELECT * FROM foo AS o_key),
m AS (SELECT * from o WHERE o.key < 50)
SELECT m1.key, m2.value FROM m m1, m m2
WHERE m1.key != m2.value
ORDER BY 1, 2 DESC LIMIT 100;
key | value
-----+-------
1 | 9
1 | 9
1 | 9
1 | 9
1 | 9
1 | 8
1 | 8
1 | 8
1 | 8
1 | 8
1 | 7
1 | 7
1 | 7
1 | 7
1 | 7
1 | 6
1 | 6
1 | 6
1 | 6
1 | 6
1 | 5
1 | 5
1 | 5
1 | 5
1 | 5
1 | 4
1 | 4
1 | 4
1 | 4
1 | 4
1 | 3
1 | 3
1 | 3
1 | 3
1 | 3
1 | 2
1 | 2
1 | 2
1 | 2
1 | 2
1 | 0
1 | 0
1 | 0
1 | 0
2 | 9
2 | 9
2 | 9
2 | 9
2 | 9
2 | 8
2 | 8
2 | 8
2 | 8
2 | 8
2 | 7
2 | 7
2 | 7
2 | 7
2 | 7
2 | 6
2 | 6
2 | 6
2 | 6
2 | 6
2 | 5
2 | 5
2 | 5
2 | 5
2 | 5
2 | 4
2 | 4
2 | 4
2 | 4
2 | 4
2 | 3
2 | 3
2 | 3
2 | 3
2 | 3
2 | 1
2 | 1
2 | 1
2 | 1
2 | 1
2 | 0
2 | 0
2 | 0
2 | 0
3 | 9
3 | 9
3 | 9
3 | 9
3 | 9
3 | 8
3 | 8
3 | 8
3 | 8
3 | 8
3 | 7
3 | 7
(100 rows)
-------------------
-- Query 6, Query 7
-------------------
-- Query has WITH clause
-- Subquery with nested WITH clause appears under a NLJ in the query plan
-- Nested WITH clause uses NLJ in query plan
-- The sharing of CTE is disabled
SET gp_cte_sharing = OFF;
-- EXPLAIN
-- WITH o AS (SELECT * FROM foo AS o_key),
-- m AS (SELECT * FROM o join bar ON o.key < bar_value)
-- SELECT m.value, m.bar_key, bar.bar_value
-- FROM m, bar
-- WHERE m.bar_key = bar.bar_value
-- ORDER BY 2, 1 DESC LIMIT 100;
--
-- EXPLAIN
-- WITH o AS (SELECT * FROM foo AS o_key),
-- m AS (SELECT * FROM o join bar ON o.key < bar_value)
-- SELECT m1.key, m2.value FROM m m1, m m2
-- WHERE m1.key != m2.value
-- ORDER BY 1, 2 DESC LIMIT 100;
-- Query has WITH clause
-- Subquery with nested WITH clause appears under a NLJ in the query plan
-- Nested WITH clause involves join in query plan
-- The sharing of CTE is enabled
SET gp_cte_sharing = ON;
--EXPLAIN
--WITH o AS (SELECT * FROM foo AS o_key),
--m AS (SELECT * FROM o join bar ON o.key < bar_value)
-- SELECT m.value, m.bar_key, bar.bar_value
-- FROM m, bar
-- WHERE m.bar_key = bar.bar_value
-- ORDER BY 2, 1 DESC LIMIT 100;
-- EXPLAIN
-- WITH o AS (SELECT * FROM foo AS o_key),
-- m AS (SELECT * FROM o join bar ON o.key < bar_value)
-- SELECT m1.key, m2.value FROM m m1, m m2
-- WHERE m1.key != m2.value
-- ORDER BY 1, 2 DESC LIMIT 100;
-------------------------------------------------------------------------------------------------------------------------------
CREATE TABLE emp (ename CHARACTER VARYING(50), empno INTEGER, mgr INTEGER, deptno INTEGER);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'ename' as the Greenplum Database data distribution key for this table.
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
INSERT INTO emp SELECT i || 'NAME', i, i%6, i%16 FROM generate_series(1, 100) i;
CREATE TABLE manager (dept_mgr_no INTEGER);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'dept_mgr_no' as the Greenplum Database data distribution key for this table.
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
INSERT INTO manager SELECT i FROM generate_series(1, 100) i;
SET enable_hashjoin = OFF;
SET enable_mergejoin = OFF;
SET enable_nestloop = ON;
-------------------
-- Query 8
-------------------
-- The sharing of CTE is disabled
SET gp_cte_sharing = OFF;
-- EXPLAIN
-- WITH dept_count AS
-- (SELECT deptno, COUNT(*) AS dept_count FROM emp
-- GROUP BY deptno) SELECT e.ename AS employee_name,
-- dc.dept_count AS emp_dept_count
-- FROM emp e, dept_count dc
-- WHERE e.deptno = dc.deptno
-- ORDER BY 1, 2 DESC LIMIT 20;
WITH dept_count AS
(SELECT deptno, COUNT(*) AS dept_count FROM emp
GROUP BY deptno) SELECT e.ename AS employee_name,
dc.dept_count AS emp_dept_count
FROM emp e, dept_count dc
WHERE e.deptno = dc.deptno
ORDER BY 1, 2 DESC LIMIT 20;
employee_name | emp_dept_count
---------------+----------------
100NAME | 7
10NAME | 6
11NAME | 6
12NAME | 6
13NAME | 6
14NAME | 6
15NAME | 6
16NAME | 6
17NAME | 7
18NAME | 7
19NAME | 7
1NAME | 7
20NAME | 7
21NAME | 6
22NAME | 6
23NAME | 6
24NAME | 6
25NAME | 6
26NAME | 6
27NAME | 6
(20 rows)
-- The sharing of CTE is enabled
SET gp_cte_sharing = ON;
--EXPLAIN
--WITH dept_count AS
-- (SELECT deptno, COUNT(*) AS dept_count FROM emp
-- GROUP BY deptno) SELECT e.ename AS employee_name,
-- dc.dept_count AS emp_dept_count
--FROM emp e, dept_count dc
--WHERE e.deptno = dc.deptno
--ORDER BY 1, 2 DESC LIMIT 20;
WITH dept_count AS
(SELECT deptno, COUNT(*) AS dept_count FROM emp
GROUP BY deptno) SELECT e.ename AS employee_name,
dc.dept_count AS emp_dept_count
FROM emp e, dept_count dc
WHERE e.deptno = dc.deptno
ORDER BY 1, 2 DESC LIMIT 20;
employee_name | emp_dept_count
---------------+----------------
100NAME | 7
10NAME | 6
11NAME | 6
12NAME | 6
13NAME | 6
14NAME | 6
15NAME | 6
16NAME | 6
17NAME | 7
18NAME | 7
19NAME | 7
1NAME | 7
20NAME | 7
21NAME | 6
22NAME | 6
23NAME | 6
24NAME | 6
25NAME | 6
26NAME | 6
27NAME | 6
(20 rows)
-------------------
-- Query 9
-------------------
-- The sharing of CTE is disabled
SET gp_cte_sharing = OFF;
--EXPLAIN
--WITH dept_count AS ( SELECT deptno, COUNT(*) AS dept_count
-- FROM emp
-- GROUP BY deptno)
--SELECT e.ename AS employee_name,
-- dc1.dept_count AS emp_dept_count,
-- m.ename AS manager_name,
-- dc2.dept_count AS mgr_dept_count
--FROM emp e,
-- dept_count dc1,
-- emp m,
-- dept_count dc2
--WHERE e.deptno = dc1.deptno AND
-- e.mgr = m.empno AND
-- m.deptno = dc2.deptno
--ORDER BY 1, 2, 3, 4 DESC LIMIT 25;
WITH dept_count AS ( SELECT deptno, COUNT(*) AS dept_count
FROM emp
GROUP BY deptno)
SELECT e.ename AS employee_name,
dc1.dept_count AS emp_dept_count,
m.ename AS manager_name,
dc2.dept_count AS mgr_dept_count
FROM emp e,
dept_count dc1,
emp m,
dept_count dc2
WHERE e.deptno = dc1.deptno AND
e.mgr = m.empno AND
m.deptno = dc2.deptno
ORDER BY 1, 2, 3, 4 DESC LIMIT 25;
employee_name | emp_dept_count | manager_name | mgr_dept_count
---------------+----------------+--------------+----------------
100NAME | 7 | 4NAME | 7
10NAME | 6 | 4NAME | 7
11NAME | 6 | 5NAME | 6
13NAME | 6 | 1NAME | 7
14NAME | 6 | 2NAME | 7
15NAME | 6 | 3NAME | 7
16NAME | 6 | 4NAME | 7
17NAME | 7 | 5NAME | 6
19NAME | 7 | 1NAME | 7
1NAME | 7 | 1NAME | 7
20NAME | 7 | 2NAME | 7
21NAME | 6 | 3NAME | 7
22NAME | 6 | 4NAME | 7
23NAME | 6 | 5NAME | 6
25NAME | 6 | 1NAME | 7
26NAME | 6 | 2NAME | 7
27NAME | 6 | 3NAME | 7
28NAME | 6 | 4NAME | 7
29NAME | 6 | 5NAME | 6
2NAME | 7 | 2NAME | 7
31NAME | 6 | 1NAME | 7
32NAME | 6 | 2NAME | 7
33NAME | 7 | 3NAME | 7
34NAME | 7 | 4NAME | 7
35NAME | 7 | 5NAME | 6
(25 rows)
-- The sharing of CTE is enabled
SET gp_cte_sharing = ON;
--EXPLAIN
--WITH dept_count AS ( SELECT deptno, COUNT(*) AS dept_count
-- FROM emp
-- GROUP BY deptno)
--SELECT e.ename AS employee_name,
-- dc1.dept_count AS emp_dept_count,
-- m.ename AS manager_name,
-- dc2.dept_count AS mgr_dept_count
--FROM emp e,
-- dept_count dc1,
-- emp m,
-- dept_count dc2
--WHERE e.deptno = dc1.deptno AND
-- e.mgr = m.empno AND
-- m.deptno = dc2.deptno
--ORDER BY 1, 2, 3, 4 DESC LIMIT 25;
WITH dept_count AS ( SELECT deptno, COUNT(*) AS dept_count
FROM emp
GROUP BY deptno)
SELECT e.ename AS employee_name,
dc1.dept_count AS emp_dept_count,
m.ename AS manager_name,
dc2.dept_count AS mgr_dept_count
FROM emp e,
dept_count dc1,
emp m,
dept_count dc2
WHERE e.deptno = dc1.deptno AND
e.mgr = m.empno AND
m.deptno = dc2.deptno
ORDER BY 1, 2, 3, 4 DESC LIMIT 25;
employee_name | emp_dept_count | manager_name | mgr_dept_count
---------------+----------------+--------------+----------------
100NAME | 7 | 4NAME | 7
10NAME | 6 | 4NAME | 7
11NAME | 6 | 5NAME | 6
13NAME | 6 | 1NAME | 7
14NAME | 6 | 2NAME | 7
15NAME | 6 | 3NAME | 7
16NAME | 6 | 4NAME | 7
17NAME | 7 | 5NAME | 6
19NAME | 7 | 1NAME | 7
1NAME | 7 | 1NAME | 7
20NAME | 7 | 2NAME | 7
21NAME | 6 | 3NAME | 7
22NAME | 6 | 4NAME | 7
23NAME | 6 | 5NAME | 6
25NAME | 6 | 1NAME | 7
26NAME | 6 | 2NAME | 7
27NAME | 6 | 3NAME | 7
28NAME | 6 | 4NAME | 7
29NAME | 6 | 5NAME | 6
2NAME | 7 | 2NAME | 7
31NAME | 6 | 1NAME | 7
32NAME | 6 | 2NAME | 7
33NAME | 7 | 3NAME | 7
34NAME | 7 | 4NAME | 7
35NAME | 7 | 5NAME | 6
(25 rows)
-------------------
-- Query 10
-------------------
-- The sharing of CTE is disabled
SET gp_cte_sharing = OFF;
--EXPLAIN
--WITH dept_count AS
-- (SELECT deptno, COUNT(*) AS dept_count
-- FROM emp
-- GROUP BY deptno) SELECT e.ename AS employee_name,
-- dc1.dept_count AS emp_dept_count,
-- m.ename AS manager_name
--FROM emp e,
-- dept_count dc1, emp m
--WHERE e.deptno = dc1.deptno AND
-- e.mgr = m.empno
--ORDER BY 1, 2, 3 ASC LIMIT 20;
WITH dept_count AS
(SELECT deptno, COUNT(*) AS dept_count
FROM emp
GROUP BY deptno) SELECT e.ename AS employee_name,
dc1.dept_count AS emp_dept_count,
m.ename AS manager_name
FROM emp e,
dept_count dc1, emp m
WHERE e.deptno = dc1.deptno AND
e.mgr = m.empno
ORDER BY 1, 2, 3 ASC LIMIT 20;
employee_name | emp_dept_count | manager_name
---------------+----------------+--------------
100NAME | 7 | 4NAME
10NAME | 6 | 4NAME
11NAME | 6 | 5NAME
13NAME | 6 | 1NAME
14NAME | 6 | 2NAME
15NAME | 6 | 3NAME
16NAME | 6 | 4NAME
17NAME | 7 | 5NAME
19NAME | 7 | 1NAME
1NAME | 7 | 1NAME
20NAME | 7 | 2NAME
21NAME | 6 | 3NAME
22NAME | 6 | 4NAME
23NAME | 6 | 5NAME
25NAME | 6 | 1NAME
26NAME | 6 | 2NAME
27NAME | 6 | 3NAME
28NAME | 6 | 4NAME
29NAME | 6 | 5NAME
2NAME | 7 | 2NAME
(20 rows)
-- The sharing of CTE is enabled
SET gp_cte_sharing = ON;
--EXPLAIN
--WITH dept_count AS
-- (SELECT deptno, COUNT(*) AS dept_count
-- FROM emp
-- GROUP BY deptno) SELECT e.ename AS employee_name,
-- dc1.dept_count AS emp_dept_count,
-- m.ename AS manager_name
--FROM emp e,
-- dept_count dc1, emp m
--WHERE e.deptno = dc1.deptno AND
-- e.mgr = m.empno
--ORDER BY 1, 2, 3 ASC LIMIT 20;
WITH dept_count AS
(SELECT deptno, COUNT(*) AS dept_count
FROM emp
GROUP BY deptno) SELECT e.ename AS employee_name,
dc1.dept_count AS emp_dept_count,
m.ename AS manager_name
FROM emp e,
dept_count dc1, emp m
WHERE e.deptno = dc1.deptno AND
e.mgr = m.empno
ORDER BY 1, 2, 3 ASC LIMIT 20;
employee_name | emp_dept_count | manager_name
---------------+----------------+--------------
100NAME | 7 | 4NAME
10NAME | 6 | 4NAME
11NAME | 6 | 5NAME
13NAME | 6 | 1NAME
14NAME | 6 | 2NAME
15NAME | 6 | 3NAME
16NAME | 6 | 4NAME
17NAME | 7 | 5NAME
19NAME | 7 | 1NAME
1NAME | 7 | 1NAME
20NAME | 7 | 2NAME
21NAME | 6 | 3NAME
22NAME | 6 | 4NAME
23NAME | 6 | 5NAME
25NAME | 6 | 1NAME
26NAME | 6 | 2NAME
27NAME | 6 | 3NAME
28NAME | 6 | 4NAME
29NAME | 6 | 5NAME
2NAME | 7 | 2NAME
(20 rows)
-------------------
-- Query 11
-------------------
-- The sharing of CTE is disabled
SET gp_cte_sharing = OFF;
--EXPLAIN
--WITH dept_count AS (SELECT deptno, COUNT(*) AS dept_count
-- FROM emp
-- GROUP BY deptno),
--mgr_count AS (SELECT dept_mgr_no, COUNT(*) AS mgr_count
-- FROM manager
-- GROUP BY dept_mgr_no)
--SELECT e.ename AS employee_name,
-- dc1.dept_count AS emp_dept_count,
-- m.ename AS manager_name,
-- dmc1.mgr_count AS mgr_dept_count
--FROM emp e,
-- dept_count dc1, emp m,
-- mgr_count dmc1
--WHERE e.deptno = dc1.deptno AND
-- e.mgr = m.empno AND
-- m.deptno = dmc1.dept_mgr_no
--ORDER BY 1, 2, 3, 4 DESC LIMIT 25;
WITH dept_count AS (SELECT deptno, COUNT(*) AS dept_count
FROM emp
GROUP BY deptno),
mgr_count AS (SELECT dept_mgr_no, COUNT(*) AS mgr_count
FROM manager
GROUP BY dept_mgr_no)
SELECT e.ename AS employee_name,
dc1.dept_count AS emp_dept_count,
m.ename AS manager_name,
dmc1.mgr_count AS mgr_dept_count
FROM emp e,
dept_count dc1, emp m,
mgr_count dmc1
WHERE e.deptno = dc1.deptno AND
e.mgr = m.empno AND
m.deptno = dmc1.dept_mgr_no
ORDER BY 1, 2, 3, 4 DESC LIMIT 25;
employee_name | emp_dept_count | manager_name | mgr_dept_count
---------------+----------------+--------------+----------------
100NAME | 7 | 4NAME | 1
10NAME | 6 | 4NAME | 1
11NAME | 6 | 5NAME | 1
13NAME | 6 | 1NAME | 1
14NAME | 6 | 2NAME | 1
15NAME | 6 | 3NAME | 1
16NAME | 6 | 4NAME | 1
17NAME | 7 | 5NAME | 1
19NAME | 7 | 1NAME | 1
1NAME | 7 | 1NAME | 1
20NAME | 7 | 2NAME | 1
21NAME | 6 | 3NAME | 1
22NAME | 6 | 4NAME | 1
23NAME | 6 | 5NAME | 1
25NAME | 6 | 1NAME | 1
26NAME | 6 | 2NAME | 1
27NAME | 6 | 3NAME | 1
28NAME | 6 | 4NAME | 1
29NAME | 6 | 5NAME | 1
2NAME | 7 | 2NAME | 1
31NAME | 6 | 1NAME | 1
32NAME | 6 | 2NAME | 1
33NAME | 7 | 3NAME | 1
34NAME | 7 | 4NAME | 1
35NAME | 7 | 5NAME | 1
(25 rows)
-- The sharing of CTE is enabled
SET gp_cte_sharing = ON;
--EXPLAIN
--WITH dept_count AS (SELECT deptno, COUNT(*) AS dept_count
-- FROM emp
-- GROUP BY deptno),
--mgr_count AS (SELECT dept_mgr_no, COUNT(*) AS mgr_count
-- FROM manager
-- GROUP BY dept_mgr_no)
--SELECT e.ename AS employee_name,
-- dc1.dept_count AS emp_dept_count,
-- m.ename AS manager_name,
-- dmc1.mgr_count AS mgr_dept_count
--FROM emp e,
-- dept_count dc1, emp m,
-- mgr_count dmc1
--WHERE e.deptno = dc1.deptno AND
-- e.mgr = m.empno AND
-- m.deptno = dmc1.dept_mgr_no
--ORDER BY 1, 2, 3, 4 DESC LIMIT 25;
WITH dept_count AS (SELECT deptno, COUNT(*) AS dept_count
FROM emp
GROUP BY deptno),
mgr_count AS (SELECT dept_mgr_no, COUNT(*) AS mgr_count
FROM manager
GROUP BY dept_mgr_no)
SELECT e.ename AS employee_name,
dc1.dept_count AS emp_dept_count,
m.ename AS manager_name,
dmc1.mgr_count AS mgr_dept_count
FROM emp e,
dept_count dc1, emp m,
mgr_count dmc1
WHERE e.deptno = dc1.deptno AND
e.mgr = m.empno AND
m.deptno = dmc1.dept_mgr_no
ORDER BY 1, 2, 3, 4 DESC LIMIT 25;
employee_name | emp_dept_count | manager_name | mgr_dept_count
---------------+----------------+--------------+----------------
100NAME | 7 | 4NAME | 1
10NAME | 6 | 4NAME | 1
11NAME | 6 | 5NAME | 1
13NAME | 6 | 1NAME | 1
14NAME | 6 | 2NAME | 1
15NAME | 6 | 3NAME | 1
16NAME | 6 | 4NAME | 1
17NAME | 7 | 5NAME | 1
19NAME | 7 | 1NAME | 1
1NAME | 7 | 1NAME | 1
20NAME | 7 | 2NAME | 1
21NAME | 6 | 3NAME | 1
22NAME | 6 | 4NAME | 1
23NAME | 6 | 5NAME | 1
25NAME | 6 | 1NAME | 1
26NAME | 6 | 2NAME | 1
27NAME | 6 | 3NAME | 1
28NAME | 6 | 4NAME | 1
29NAME | 6 | 5NAME | 1
2NAME | 7 | 2NAME | 1
31NAME | 6 | 1NAME | 1
32NAME | 6 | 2NAME | 1
33NAME | 7 | 3NAME | 1
34NAME | 7 | 4NAME | 1
35NAME | 7 | 5NAME | 1
(25 rows)
-- Test that SharedInputScan within the same slice is always executed
set gp_cte_sharing=on;
-- start_ignore
CREATE TABLE car (a int, b int);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' as the Greenplum Database data distribution key for this table.
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
CREATE TABLE zoo (c int, d int);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'c' as the Greenplum Database data distribution key for this table.
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
insert into car select i, (i+1) from generate_series(1,10) i;
insert into zoo values (4,4);
-- end_ignore
WITH c as (SELECT sum(a) as a_sum, b FROM car GROUP BY b)
SELECT * FROM c as c1, zoo WHERE zoo.c != 4 AND c1.b = zoo.c
UNION ALL
SELECT * FROM c as c1, zoo WHERE zoo.c = c1.b;
a_sum | b | c | d
-------+---+---+---
3 | 4 | 4 | 4
(1 row)
-- Test that nested CTE generates a valid plan
EXPLAIN (COSTS OFF) WITH q AS (SELECT * FROM (WITH cte AS (SELECT * FROM car) SELECT * FROM car WHERE a > 7) t) SELECT * FROM q;
QUERY PLAN
------------------------------------------
Gather Motion 3:1 (slice1; segments: 3)
-> Seq Scan on car
Filter: (a > 7)
Optimizer: GPORCA
(4 rows)
WITH q AS (SELECT * FROM (WITH cte AS (SELECT * FROM car) SELECT * FROM car WHERE a > 7) t) SELECT * FROM q;
a | b
----+----
8 | 9
9 | 10
10 | 11
(3 rows)
-- start_ignore
drop schema qp_with_clause cascade;
NOTICE: drop cascades to table zoo
NOTICE: drop cascades to table car
NOTICE: drop cascades to table manager
NOTICE: drop cascades to table emp
NOTICE: drop cascades to table bar
NOTICE: drop cascades to table foo
NOTICE: drop cascades to table tbl87
NOTICE: drop cascades to append only columnar table countrylanguage_co
NOTICE: drop cascades to append only columnar table country_co
NOTICE: drop cascades to append only columnar table city_co
NOTICE: drop cascades to append only table countrylanguage_ao
NOTICE: drop cascades to append only table country_ao
NOTICE: drop cascades to append only table city_ao
NOTICE: drop cascades to table countrylanguage
NOTICE: drop cascades to table country
NOTICE: drop cascades to table city
-- end_ignore
RESET optimizer_trace_fallback;