I worked with the world database using various SQL queries and database functions. I learned how to query data, filter results, group records, and use window functions to analyze data. This retrospective documents my journey through Task 2: Query the world database.
In this task, I queried the world database using various SELECT statements and database functions.
First, I needed to verify that the world database was available. I entered the following command in the terminal:
I confirmed that a database named world was available. If the world database had not been available, I would have contacted my instructor.
To review the table schema, data, and number of rows in the country table, I entered the following query:
To return a list of records where the Region is Australia and New Zealand, I ran the following query. This query includes an ORDER BY clause that arranges the results by Population in descending order:
I learned that I can use the GROUP BY clause to group related records together. The following example starts by filtering records using a condition where the region is equal to Australia and New Zealand. The results are then grouped together by using a GROUP BY clause. The SUM() function is then applied to the grouped results to generate a total population for that region. I ran the following query in my terminal:
This query returned a SUM() of the Population for the Australia and New Zealand region. Because the WHERE clause is filtered by Region, only the Australia and New Zealand records are aggregated.
The following example uses a windowing function to generate a running total by adding the Population of the first record to the Population of the second record and subsequent records. This query uses the OVER() clause to group the records by Region and uses the SUM() function to aggregate the records. The output displays the population of a country along side a running total of the region. I ran the following query in my terminal:
The following query groups the records by Region and orders them by Population with the OVER() clause. This query also includes the RANK() function to generate a rank number indicating the position of each record in the result set. The RANK() function is useful when dealing with large groups of records. I ran the following query in my terminal:
I had to write a query to rank the countries in each region by their population from largest to smallest.
I had to determine whether to use either the GROUP BY or OVER grouping clause and either the SUM() or RANK() function. After analyzing the requirements, I created the following query:
I found that using the OVER clause with RANK() was the most appropriate for this task since I needed to rank countries within each region while maintaining all the individual country data.
I have now successfully:
I gained valuable experience with SQL queries and learned how different clauses and functions can be combined to extract meaningful insights from a database.