You are on page 1of 2

Database Management Systems

Laboratory Activity #04a

During this activity, you should: enhance your previous knowledge on creating correct queries using SQL SELECT statements apply the use of subqueries in SQL SELECT statements, specifically in the WHERE clause Subqueries 1. A subquery is a SELECT statement that is nested within another SQL statement (i.e., the main query). The results of a subquery (which executes first) are subsequently used in the main query. 2. A subquery can be placed in the WHERE, HAVING and FROM clauses of a SELECT statement. A subquery in a SELECT statement is used when the condition used to select the rows for the main query depends on data that can only be determined through another query. Q1. Suppose it is required to find all cities with populations that exceed the population of Baguio City. This information can be obtained through a sequence of two queries. Since the population of Baguio is not known, the statement below can be used to determine this.

SELECT population FROM city WHERE name = 'Baguio';


What is the result (i.e., the actual row) of the query? Use this result to come up with the second SELECT statement, which should provide the required information. Heading: Name, Population Q2. The sequence of statements in Q1 can be written as a single statement by using a subquery. The equivalent statement is shown below. How many rows are returned by the query? This should have the same result as the final query in Q1.

SELECT name, population FROM city WHERE population > (SELECT population FROM city WHERE name = 'Baguio');
3. Subqueries may return only one row (single-row subqueries) or return more than one row (multiple-row subqueries). Single-row subqueries are used with the basic comparison operators (>, =, >=, <, <>, <=). Q3. Modify the query above to determine only the populations of Philippine cities with populations that exceed the population of Baguio City. Heading: Name, Population Result: 31 rows (1st row Quezon, 2173831) Required: last row Note: The main query must use a join between city and country. Q4. Analyze the SQL statements below and the respective results. Describe the information that each of the queries provide.

a) SELECT region, name FROM country WHERE region = (SELECT region FROM country WHERE name = Congo); b) SELECT name FROM country WHERE (indepyear, region) = (SELECT indepyear, region FROM country WHERE name ='Congo'); c) SELECT name FROM country WHERE (indepyear) = (SELECT indepyear FROM country WHERE name ='Congo') AND region = (SELECT region FROM country WHERE name = Mali');

Q5. Provide the SQL statement to determine the countries that are younger than Singapore (in terms of independence). Sort the data from the youngest to the oldest country. Heading: Country, Year of Independence Result: 64 rows (1st row Palau, 1994) Required: last row Q6. Modify the query above to determine the countries that are younger than Singapore and have the same form of government as Singapore. Do not include a sort order. Heading: Country, Region, Year of Independence Result: 42 rows (1st row Angola, Central Africa, 1975) Required: last row Q7. Consider the SQL statement below. Indicate the resulting number of rows and the information provided by the query.

SELECT co.name, co.continent, co.region, co.population FROM country co WHERE co.population < (SELECT MAX(city.population) FROM city);
Q8. Provide the SQL statement that will determine the countries whose population exceeds the largest population in Africa. Heading: Country, Population, Continent Result: 9 rows (1st row - Bangladesh, 129155000, Asia) Required: 2nd row Q9. Modify the query above so that only countries in Asia are shown. Sort the data in descending order based on population. Heading: Country, Population Result: 6 rows Required: last row 4. All previous queries make use of subqueries in the WHERE clause. Below is an illustration of how a subquery is used in the HAVING clause. Q10. Consider the SQL statement below. Indicate the resulting number of rows and the information provided by the query.

SELECT region, sum(population) FROM country GROUP BY region HAVING sum(population) < (SELECT AVG(population) FROM country WHERE continent = 'Europe') AND sum(population) > 0;
Q11. Provide the SQL statement that will determine the regions whose average surface area is less than the largest surface area in South East Asia. Sort the data in ascending order based on the average surface area. Heading: Region, Average Surface Area Result: 23 rows (1st row: Micronesia/Caribbean, 16.00) Required: last row Q12. Extra!!! Determine the countries where the number of spoken languages does not fall below the number of languages spoken in the Philippines. The Philippines should not be included in the list. Heading: Name, Number of Languages Result: 14 rows (1st row: Canada, 12) Required: 5th row

You might also like