0% found this document useful (0 votes)
54 views

Lab 2: SQL Queries: 1 Capitals

This document contains 7 SQL queries performed on a database of city data: 1. The number of cities classified as capitals is 284, with the average population of capital cities being 2,282,823. 2. Cities with "city" in their name are listed, such as Salt Lake City and Mexico City. 3. Cities with over 10 million people are listed, including London, Seoul, and Tokyo. 4. Cities between 2 and 5 million people are listed, including Warsaw, Budapest, and Montreal. 5. Cities within 500km of Barcelona are listed, such as Bordeaux, Toulouse, and Valencia. 6. The average distance is calculated between

Uploaded by

OsamaKhalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Lab 2: SQL Queries: 1 Capitals

This document contains 7 SQL queries performed on a database of city data: 1. The number of cities classified as capitals is 284, with the average population of capital cities being 2,282,823. 2. Cities with "city" in their name are listed, such as Salt Lake City and Mexico City. 3. Cities with over 10 million people are listed, including London, Seoul, and Tokyo. 4. Cities between 2 and 5 million people are listed, including Warsaw, Budapest, and Montreal. 5. Cities within 500km of Barcelona are listed, such as Bordeaux, Toulouse, and Valencia. 6. The average distance is calculated between

Uploaded by

OsamaKhalid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 2: SQL Queries

Osama Khalid
September 29, 2017

1 Capitals
SELECT count(*)
FROM cities
WHERE capital = ’Y’;

Total number of Capitals: 284

2 Average Population of Capital Cities


SELECT sum(population)/count(capital) as AveragePop
FROM cities
WHERE capital = ’Y’;

Average Population of Capital Cities: 2262823

3 Cities with the word "city’ in their names


SELECT name as Name
FROM cities
WHERE name LIKE ’%City%’;

’Name’
’Salt Lake City’
’Kansas City’
’Oklahoma City’
’Mexico City’
’Ho Chi Minh City’
’Quezon City’
’Salt Lake City’
’Kansas City’
’Oklahoma City’
’Mexico City’
’Ho Chi Minh City’
’Quezon City’

4 Cities with over 10mil people


SELECT Name
FROM cities
WHERE Population > 10000000;

’Name’
’London’

1
’Seoul’
’Tokyo’
’Osaka’
’Calcutta’
’Mexico City’
’Rio de Janeiro’
’Sao Paulo’
’Buenos Aires’
’Moskva’
’New York’

5 Cities between 2 mil and 5 mil people


SELECT Name
FROM cities
WHERE Population >= 2000000 AND Population <=5000000;

’Name’
’Nizhniy Novgorod’
’Hamburg’
’Manchester’
’Birmingham’
’Warsaw’
’Essen’
’Bruxelles’
’Kiev’
’Donets’k’
’Budapest’
’Harbin’
’Montreal’
’Milano’
’Minneapolis’
’Bucuresti’
’Toronto’
’Detroit’
’Boston’
’Shenyang’
’Cleveland’
’Pittsburgh’
’Madrid’
’Baku’
’Ankara’
’Tianjin’
’Washington D.C.’
’St. Louis’
’Taegu’
’Yokohama’
’Pusan’
’Nagoya’
’Xian’
’Baghdad’
’Dallas’
’Nanjing’
’Lahore’
’Wuhan’
’Houston’
’Chongqing’

2
’Miami’
’Monterrey’
’Dhaka’
’Guangzhou’
’Ahmadabad’
’Guadalajara’
’Hyderabad’
’Rangoon’
’Madras’
’Bangalore’
’Ho Chi Minh City’
’Caracas’
’Medellin’
’Bogota’
’Recife’
’Belo Horizonte’
’Johannesburg’
’Porto Alegre’
’Santiago’
’Sydney’
’Melbourne’
’San Francisco’
’Kinshasa’
’Alexandria’
’Algiers’
’Athinai’
’Barcelona’
’Casablanca’
’Colombo’
’Havana’
’Lagos’
’Lima’
’Lisboa’
’Napoli’
’Roma’
’Salvador’
’Surabaja’
’Toshkent’
’Seattle’
’San Diego’
’Singapore’

6 Cities within 500 km from Barcelona


SELECT C2.name as Name,ST_DISTANCE_SPHERE(C1.the_geom,C2.the_geom) as Distance
FROM Cities as C1, Cities as C2
WHERE C1.Name=’Barcelona’ and

ST_DISTANCE_SPHERE(C2.the_geom,C1.the_geom) <5000000 and C2.Name <>’Barcelona’

’Name’,’Distance’
’Bordeaux’,’431217.965154376’
’Toulouse’,’237845.665940777’
’Bilbao’,’464991.920211631’
’Zaragoza’,’253844.5793665’
’Valencia’,’313740.220031306’

3
’Marseille’,’328862.627440211’

7 Cities within 1000 km from Bruxelles and Capitals and


have a population over 6Million
SELECT AVG(ST_DISTANCE_SPHERE(C1.the_geom,C2.the_geom))/1000 as Distance
FROM okhalid.Cities as C1, okhalid.Cities as C2
WHERE C1.Name=’Bruxelles’ and ST_DISTANCE_SPHERE(C2.the_geom,C1.the_geom) <1000000

and C2.population > 6000000 and C2.capital = ’Y’

Avg Distance: ’291.479875239883’

You might also like