Sunteți pe pagina 1din 2

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 the WHERE 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) or ALL operators. The NOT operator can be used with the IN, ANY, and ALL operators. When using SOME or ANY, use the DISTINCT keyword to prevent rows from appearing several times.

>ALL means more than the maximum value returned by the subquery <ALL means less than the minimum value returned by the subquery <ANY means less than the maximum value returned by the (sub)query >ANY means more than the minimum value returned by the (sub)query =ANY is equivalent to IN
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 than all maximum life expectancies of all European regions. Heading: Country, LifeExpectancy Result: 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 such that only regions with maximum populations that exceed all the lowest populations in European regions are shown. Heading: Region, Maximum Population Result: 20 rows (1st row: Australia and New Zealand, 18886000) Required: 3rd row

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 result should appear only once. Heading: Country Result: 25 rows Required: 16th row 2. Some seemingly correct queries may not return the correct results. If the subquery returns no rows, the main query will return no rows. Q6. Analyze the query below and indicate the information that it is intended to provide. Why does it not 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 the query.

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 be a logical solution. However the second query provides the correct information. Indicate the number of rows returned by each query and explain the error in the first statement.

a) SELECT ci.id, ci.name FROM city ci WHERE ci.id NOT IN (SELECT capital FROM country); b) SELECT ci.id, ci.name FROM city ci WHERE 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 not match any of the years of independence of countries in Africa. Heading: Country, Independence Year Result: 40 rows (2nd row: United Arab Emirates, 1971) Required: 5th row

S-ar putea să vă placă și