Question: Hello! Help with SQL , please ! We have the following database: world. CREATE TABLE city ( id int 4 NOT NULL, name text NOT
Hello! Help with SQLplease
We have the following database: world.
CREATE TABLE city
id int NOT NULL,
"name" text NOT NULL,
countrycode bpchar NOT NULL,
district text NOT NULL,
population int NOT NULL,
CONSTRAINT citypkey PRIMARY KEY id
;
CREATE TABLE country
code bpchar NOT NULL,
"name" text NOT NULL,
continent text NOT NULL,
region text NOT NULL,
surfacearea float NOT NULL,
indepyear int NULL,
population int NOT NULL,
lifeexpectancy float NULL,
gnp numeric NULL,
gnpold numeric NULL,
localname text NOT NULL,
governmentform text NOT NULL,
headofstate text NULL,
capital int NULL,
code bpchar NOT NULL,
CONSTRAINT countrycontinentcheck CHECK continent 'Asia'::text OR continent 'Europe'::text OR continent 'North America'::text OR continent 'Africa'::text OR continent 'Oceania'::text OR continent 'Antarctica'::text OR continent 'South America'::text
CONSTRAINT countrypkey PRIMARY KEY code
;
country foreign keys
ALTER TABLE country ADD CONSTRAINT countrycapitalfkey FOREIGN KEY capital REFERENCES cityid;
city foreign keys
ALTER TABLE city ADD CONSTRAINT cityfk FOREIGN KEY countrycode REFERENCES countrycode;
In this task, students will be asked to rewrite existing queries using different SQL elements.
Use the world database to test your code.
The first query:
The following query finds cities with high population m which are not capitals.
Rewrite this query without a subquery:
select from city as ct
where not exists select from country as c
where ccapitalctid
and population ;
The second query:
You have a query to find cities that have population greater than average city population in the same country.
Rewrite this query using a derived table in FROM. The new version of the query should not include a subquery in where.
select cname countryname, ctname cityname, ctpopulation
as citypopulation
from city as ct
join country as c on ccode ctcountrycode
where ctpopulation
select avgpopulation as avgpopulation
from city
where countrycode ctcountrycode
order by c"name" ct"name";
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
