I have these three tables
CREATE TABLE "Countries"
(
name text NOT NULL,
pop integer,
CONSTRAINT country PRIMARY KEY (name),
CONSTRAINT pop_check CHECK (pop > 0)
)
CREATE TABLE "Cities"
( name text NOT NULL, pop integer,
country_name text,
CONSTRAINT "City_ref" PRIMARY KEY (name),
CONSTRAINT country_ref FOREIGN KEY (country_name)
REFERENCES "db1001002_Countries" (name) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT "Population" CHECK (pop > 0)
);
CREATE TABLE " Company"
(
"City_name" text NOT NULL,
"Company_Name" text NOT NULL,
no_of_employee integer,
CONSTRAINT "City_company" PRIMARY KEY ("City_name", "Company_Name"),
CONSTRAINT city_ref FOREIGN KEY ("City_name")
REFERENCES "Cities" (name) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT employee_check CHECK (no_of_employee > 0)
)
my problem is that I want to use a nested query in the format
select field(s) from table(s)
where field(s) in (select field(s) from table(s) condition)
group by field(s)
to obtain a new table with the fields
country, min_pop, max_pop, average_pop
for cities with pop > 500000
I tried writing some query but I keep getting errors of too many fields in your sub query. I am using postgresql 9.3.
when I use this query, I get some result.
SELECT country_name, min_pop, max_pop, avg_popo
FROM (SELECT country_name, min(pop),max(pop),avg(pop)
FROM
"Cities"
GROUP BY country_name)
AS popcal(country_name,min_pop, max_pop, avg_popo)
WHERE min_pop>500000
Is there no way of writing the query in this format
select field(s) from table(s)
where field(s) in (select field(s) from table(s) condition)
group by field(s)
?