Labact04b Proper

Embed Size (px)

Citation preview

  • 7/28/2019 Labact04b Proper

    1/2

    Q1. Indicate the number of rows and the information provided by each of the queries below:SELECT ci.name, ci.district, ci.population FROM city ci

    WHERE ci.population < all (SELECT country.population

    FROM country

    WHERE continent = 'europe');

    SELECT ci.name, ci.district, ci.population FROM city ci

    WHERE ci.population >ANY(SELECT country.population

    FROM country

    WHERE continent = 'europe');SELECT ci.name, ci.district, ci.population FROM city ci

    WHERE ci.population IN(SELECT MIN(country.population)

    FROM country GROUP BY region);

    SELECT ci.name, ci.district, ci.population FROM city ci

    WHERE ci.population > ALL (SELECT MIN(country.population)

    FROM country GROUP BY region);

    SELECT ci.name, ci.district, ci.population FROM city ci

    WHERE ci.population NOT IN (SELECT MIN(country.population)

    FROM country GROUP BY region);

    Q2. Provide the SQL statement to list all non-European countries whose life expectancies are less thanall maximum life expectancies of all European regions.

    Heading: Country, LifeExpectancyResult: 98 rows (1st row: Afghanistan, 45.9 )Required: 5th row

    Q3. Provide the SQL statement to list the maximum populations in each region. Filter the results suchthat only regions with maximum populations that exceed all the lowest populations in Europeanregions are shown.

    Heading: Region, Maximum PopulationResult: 20 rows (1st row: Australia and New Zealand, 18886000)

    Required: 3rd row

    Database Management Systems Laboratory Activity #4b

    During this activity, you should:

    enhance your previous knowledge on using subqueries in SQL SELECT statements apply the use of subqueries in SQL SELECT statements, specifically in theWHERE and HAVING clauses distinguish between single-row subqueries and multiple-row subqueries

    More concepts on subqueries:1. Multiple-row subqueries are expected to return more than one row and are used with the IN,ANY (works in the

    same way as SOME) orALL operators. TheNOT operator can be used with the IN,ANY, andALL operators.

    When using SOME orANY, use the DISTINCT keyword to prevent rows from appearing several times.

    >ALL means more than the maximum value returned by the subquery

  • 7/28/2019 Labact04b Proper

    2/2

    Q4. Provide the SQL statement to list all countries that speak any of the languages of Madagascar.Madagascar should not be included in the list.

    Heading: country, language, region

    Result: 26 rows (1st row: Andorra, French, Southern Europe)Required: 3rd row (country, language)

    Q5. Modify the query above so that only the countries are displayed. Each country in the resultshould appear only once.

    Heading: CountryResult: 25 rowsRe uired: 16th row

    2. Some seemingly correct queries may not return the correct results. If the subquery returns no rows, the main querywill return no rows.

    Q6. Analyze the query below and indicate the information that it is intended to provide. Why does itnot return any rows?SELECT name, continent, region

    FROM country

    WHERE region = (SELECT region FROM country

    WHERE name = 'Russia');

    Q7. The query below correctly displays all capital cities. Indicate the number of rows returned by thequery.SELECT ci.id, ci.name

    FROM city ci

    WHERE ci.id IN (SELECT capital FROM country);

    Q8. Suppose, it is required to display all cities that are not capitals. The first query below seems to bea logical solution. However the second query provides the correct information. Indicate thenumber of rows returned by each query and explain the error in the f irst statement.

    a) SELECT ci.id, ci.nameFROM city ci

    WHERE ci.id NOT IN (SELECT capital FROM country);

    b) SELECT ci.id, ci.name FROM city ciWHERE ci.id NOT IN (SELECT capital FROM country)

    WHERE capital IS NOT NULL);;

    Q9. Provide the SQL statement to list all all countries in Asia whose year of independence does notmatch any of the years of independence of countries in Africa.

    Heading: Country, Independence Year

    Result: 40 rows (2nd row: United Arab Emirates, 1971)

    Required: 5

    th

    row