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

SQL-Questions & Answer

The document contains 17 SQL queries and their outputs related to CITY and STATION tables. The queries perform operations like selection, filtering, aggregation, joining and set operations on the tables.

Uploaded by

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

SQL-Questions & Answer

The document contains 17 SQL queries and their outputs related to CITY and STATION tables. The queries perform operations like selection, filtering, aggregation, joining and set operations on the tables.

Uploaded by

Raviraj Varma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

1. Query the names of all American cities in CITY with populations larger than 120000.

The CountryCode for America is USA.

Input Format

The CITY table is described as follows:

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

The CITY table is described as follows:

Answer.
Query
SELECT * FROM CITY WHERE ID = 1661

Output
Id Name CountryCode District Population

1661 Sayama JPN Saitama 162472


3. Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN.

Input Format

The CITY table is described as follows:

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

in any order, but must exclude duplicates from your answer.

Input Format

The STATION table is described as follows:

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

The CITY table is described as follows:

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

The STATION table is described as follows:

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

The CITY table is described as follows:


Answer.

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

The CITY table is described as follows:

Answer.

Query

SELECT TOP (5) NAME FROM CITY


ORDER BY NAME ASC

Output

Name
Ageo
Boulder
Cedar Rapids
Concord
Coral Springs
9. Query to get the total population of USA.

Input Format

The CITY table is described as follows:

Answer.

Query

SELECT SUM (POPULATION) FROM CITY


WHERE COUNTRYCODE = ‘USA’

Output
(No column name)
961807
10. Query to get the distinct district. Display the name of the city
and district.

Input Format

The CITY table is described as follows:

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

The CITY table is described as follows:


Answer.

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

The CITY table is described as follows:


Answer.

Query

SELECT MIN (POPULATION) AS POPULATION , COUNTRYCODE, NAME FROM CITY


GROUP BY POPULATION, COUNTRYCODE, NAME

Output

POPULATION COUNTRYCODE NAME


90555 USA Fall River
91238 USA Boulder
92256 USA Fairfield
107078 JPN Tokuyama
117549 USA Coral Springs
120758 USA Cedar Rapids
121780 USA Concord
124966 USA Corona
142889 JPN Omuta
162472 JPN Sayama
202705 USA Scottsdale
209442 JPN Ageo
257315 JPN Neyagawa
593321 NLD Rotterdam
13. Query to get the name of the City starting with letter ‘B’.
Display the city column with result.

Input Format

The STATION table is described as follows:

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

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

Query
SELECT AVG(LAT_N) FROM STATION

Output

(No column name)

68.7092666666667
15. Query to perform Inner join on city and station hint : find the relation between two tables

Left Side Table

The CITY table is described as follows:

Right Side Table

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

Fall River Massachusetts

Ageo Saitama

Tokuyama Yamaguchi

Tokuyama Yamaguchi

16. Query to perform a LEFT JOIN.


Left side City table, right side Station table.

Left Side Table

The CITY table is described as follows:

Right Side Table

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

17. Query to perform a UNION.


Left side City table, right side Station table.

Table 1

The CITY table is described as follows:

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

 The same number of columns selected


 The same number of column expressions
 The same data type and
 Have them in the same order

You might also like