Upload
azukadevil6667485
View
215
Download
0
Embed Size (px)
Citation preview
7/28/2019 LabAct04a
1/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 theWHERE clause
Subqueries
1. A subquery is a SELECT statement that is nested within another SQL statement (i.e., the main query). The resultsof a subquery (which executes first) are subsequently used in the main query.
2. A subquery can be placed in theWHERE, HAVING and FROMclauses of a SELECT statement. A subquery in aSELECT 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.
3. Subqueries may return only one row (single-rowsubqueries) or return more than one row (multiple-rowsubqueries). Single-row subqueries are used with the basic comparison operators (>, =, >=,
7/28/2019 LabAct04a
2/2
4. All previous queries make use of subqueries in theWHERE clause. Below is an illustration of how a subquery isused in the HAVING clause.
Q5. Provide the SQL statement to determine the countries that are younger than Singapore (in terms ofindependence). Sort the data from the youngest to the oldest country.
Heading: Country, Year of IndependenceResult: 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 thesame form of government as Singapore. Do not include a sort order.
Heading: Country, Region, Year of IndependenceResult: 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 informationprovided 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, ContinentResult: 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 orderbased on population.
Heading: Country, PopulationResult: 6 rows
Required: last row
Q10.Consider the SQL statement below. Indicate the resulting number of rows and the informationprovided 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 thelargest surface area in South East Asia. Sort the data in ascending order based on the averagesurface area.
Heading: Region, Average Surface AreaResult: 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 thenumber of languages spoken in the Philippines. The Philippines should not be included in the list.
Heading: Name, Number of LanguagesResult: 14 rows (1st row: Canada, 12)Required: 5th row