sql2 (Answers)

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

download Appserve:

https://www.youtube.com/watch?v=jczx_0L1PaQ
https://www.appserv.org/en/download/

############################################################################

create table City


(
ID int,
Name char(20),
CountryCode char(20),
District char(20),
Population int
)

insert into City (ID, Name, CountryCode, District, Population)


values
(1, 'New York', 'USA', 'New York', 8537673),
(2, 'Los Angeles', 'USA', 'California', 3976322),
(3, 'Chicago', 'USA', 'Illinois', 2704958),
(4, 'Houston', 'USA', 'Texas', 2303482),
(5, 'Phoenix', 'USA', 'Arizona', 1660272),
(6, 'San Antonio', 'USA', 'Texas', 1532233),
(7, 'San Diego', 'USA', 'California', 1425976),
(8, 'Dallas', 'USA', 'Texas', 1345047),
(9, 'San Jose', 'USA', 'California', 1030119),
(10, 'Austin', 'USA', 'Texas', 964254),
(11, 'Jacksonville', 'USA', 'Florida', 903889),
(12, 'Fort Worth', 'USA', 'Texas', 895008),
(13, 'Columbus', 'USA', 'Ohio', 892533),
(14, 'San Francisco', 'USA', 'California', 883305),
(15, 'London', 'GBR', 'England', 8982000),
(16, 'Tokyo', 'JPN', 'Tokyo', 13929286),
(17, 'Paris', 'FRA', 'Île-de-France', 2140526),
(18, 'Berlin', 'DEU', 'Berlin', 3748148),
(19, 'Moscow', 'RUS', 'Moscow', 12615882),
(20, 'Sydney', 'AUS', 'New South Wales', 5312000),
(21, 'Toronto', 'CAN', 'Ontario', 2731571),
(22, 'Mumbai', 'IND', 'Maharashtra', 18414288),
(23, 'Rio de Janeiro', 'BRA', 'Rio de Janeiro', 6747815),
(24, 'Cairo', 'EGY', 'Cairo', 9813807),
(25, 'Dubai', 'ARE', 'Dubai', 3137000),
(26, 'Seoul', 'KOR', 'Seoul', 9720846),
(27, 'Rome', 'ITA', 'Lazio', 2870500),
(28, 'Bangkok', 'THA', 'Bangkok', 8305218);

Q1:Query all columns (attributes) for every row in the CITY table.

select * from city


Q2: Query all columns for all American cities in the CITY table with populations
larger than 1000000.
The CountryCode for America is USA.

select * from city


where CountryCode ='USA' And Population>1000000

Q3:Query the NAME field for all American cities in the CITY table with populations
larger than 1200000.

select Name from City


where CountryCode ='USA' And Population > 1200000

Q4:Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE
for Japan is JPN.

select * from City


where CountryCode ='JPN'

Q5:Query all columns for a city in CITY with the ID 16.

select * from City


where ID=16

Q6:Query the CountryCode field for all cities in the CITY table with populations
smaller than 8982000
and larger than 3137000 in any order, but exclude duplicates.

select distinct CountryCode from City


where Population < 8982000 and Population > 3137000

Q7:Query all cities. Print the results sorted by population.

select * from city


order by population

Q8:Find the difference between the total number of CountryCode entries in the table
and the number of distinct CountryCode entries in the table.

select count(CountryCode)-count(distinct CountryCode) from city


Q9:Find the average population of American cities.

select avg(Population) from city


where CountryCode='USA'

Q10:Find the total population for each country based on their 'CountryCode' .

select sum(Population) from city


group by CountryCode

Q11:Find the number of cities in each country based on their 'CountryCode' .

select count(*) from city


group by CountryCode

Q12:Find the total population for each country provided that


the number of cities in each country exceeds 3, arranged in descending order.

select sum(Population),CountryCode from city


group by CountryCode
having count(*) > 3
order by sum(Population) desc

In operator:

select Population,CountryCode from city


where Population=2704958 or Population=1532233 or Population=2870500

select Population,CountryCode from city


where Population in (2704958 ,1532233 ,2870500)

You might also like