SQL-Questions & Answer
SQL-Questions & Answer
Input Format
Answer.
Query
SELECT NAME FROM CITY WHERE COUNTRYCODE = ‘USA’ AND POPULATION > 120000
Output
Id Name CountryCode District Population
3878 Scottsdale USA Arizona 202705
3965 Corona USA California 124966
3973 Concord USA California 121780
3977 Cedar Rapids USA Iowa 120758
2. Query all columns for a city in CITY with the ID 1661.
Input Format
Answer.
Query
SELECT * FROM CITY WHERE ID = 1661
Output
Id Name CountryCode District Population
Input Format
Answer.
Query
SELECT NAME FROM CITY WHERE COUNTRYCODE = ‘JPN’
Output
Name
Sayama
Neyagawa
Ageo
Omuta
Tokuyama
4. Query a list of CITY names from STATION with even ID numbers only. You may print the results
Input Format
where LAT_N is the northern latitude and LONG_W is the western longitude.
Answer.
Query
SELECT DISTINCT CITY FROM STATION WHERE ID%2 = 0
Output
CITY
Brownstown
Hayneville
Mesick
5. Query lists the name and district of city. Whose population is less than
100000.
Input Format
Answer.
Query
select name, district from city
where population < 100000
Output
name district
Fairfield California
Boulder Colorado
Fall River Massachusetts
6. List of city names from station that do not start with vowels or do
not end with vowels. Cannot contain duplicates.
Input Format
where LAT_N is the northern latitude and LONG_W is the western longitude.
Answer.
Query
SELECT DISTINCT CITY FROM STATION
WHERE
CITY NOT LIKE ‘A%’ AND
CITY NOT LIKE ‘E%’ AND
CITY NOT LIKE ‘I%’ AND
CITY NOT LIKE ‘O%’ AND
CITY NOT LIKE ‘U%’ AND
CITY NOT LIKE ‘%A’ AND
CITY NOT LIKE ‘%E’ AND
CITY NOT LIKE ‘%I’ AND
CITY NOT LIKE ‘%O’ AND
CITY NOT LIKE ‘%U’
Output
City
Beverly
Brownstown
Gatewood
Madden
Mesick
Yoder
7. Query that prints list of name from the city table in alphabetical
order.
Input Format
Query
SELECT NAME FROM CITY
ORDER BY NAME ASC
Output
Name
Ageo
Boulder
Cedar Rapids
Concord
Coral Springs
Corona
Fairfield
Fall River
Neyagawa
Omuta
Rotterdam
Sayama
Scottsdale
Tokuyama
8. Query to get the first five name of city and order them in
ascending.
Input Format
Answer.
Query
Output
Name
Ageo
Boulder
Cedar Rapids
Concord
Coral Springs
9. Query to get the total population of USA.
Input Format
Answer.
Query
Output
(No column name)
961807
10. Query to get the distinct district. Display the name of the city
and district.
Input Format
Answer.
Query
SELECT DISTINCT DISRICT, NAME FROM CITY
Output
district name
Arizona Scottsdale
California Concord
California Corona
California Fairfield
Colorado Boulder
Florida Coral Springs
Fukuoka Omuta
Iowa Cedar Rapids
Massachusetts Fall River
Osaka Neyagawa
Saitama Ageo
Saitama Sayama
Yamaguchi Tokuyama
Zuid-Holland Rotterdam
11. Query to get the population less than 500000 and greater than
200000. Display the name, countrycode and population of the city
table.
Input Format
Query
SELECT NAME, COUNTRYCODE, POPULATION FROM CITY
WHERE POPULATION > 200000
AND
POPULATION < 500000
Output
NAME COUNTRYCODE POPULATION
Scottsdale USA 202705
Neyagawa JPN 257315
Ageo JPN 209442
12. Query to get the country who has the lowest population in city
table. Also display the countrycode, name , population.
Input Format
Query
Output
Input Format
where LAT_N is the northern latitude and LONG_W is the western longitude.
Query
SELECT city FROM STATION
WHERE CITY LIKE 'B%'
Output
City
Brownstown
Beverly
14. Query to get the average Latitude North (Lat_N).
Input Format
where LAT_N is the northern latitude and LONG_W is the western longitude.
Query
SELECT AVG(LAT_N) FROM STATION
Output
68.7092666666667
15. Query to perform Inner join on city and station hint : find the relation between two tables
where LAT_N is the northern latitude and LONG_W is the western longitude.
Query
SELECT NAME, DISTRICT FROM CITY
INNER JOIN STATION
ON
CITY.ID = STATION.CITY_ID
Output
name district
Corona California
Fairfield California
Fairfield California
Boulder Colorado
Boulder Colorado
Ageo Saitama
Tokuyama Yamaguchi
Tokuyama Yamaguchi
where LAT_N is the northern latitude and LONG_W is the western longitude.
Query
SELECT NAME, DISTRICT FROM CITY
LEFT JOIN STATION
ON
CITY.ID = STATION.CITY_ID
Output
name district
Scottsdale Arizona
Corona California
Concord California
Cedar Rapids Iowa
Coral Springs Florida
Rotterdam Zuid-Holland
Fairfield California
Fairfield California
Boulder Colorado
Boulder Colorado
Fall River Massachusetts
Sayama Saitama
Neyagawa Osaka
Ageo Saitama
Omuta Fukuoka
Tokuyama Yamaguchi
Tokuyama Yamaguchi
Table 1
Table 2
where LAT_N is the northern latitude and LONG_W is the western longitude.
Answer.
To use this UNION clause, each SELECT statement must have