I worked with a relational database named world containing three tables: city, country, and countrylanguage. Based on specific use cases in the project, I wrote queries using database functions with the SELECT statement and WHERE clause.
Throughout, I successfully:
In this task, I queried the world database using various SELECT statements and database functions. I used functions to process and manipulate data in my queries. There are a wide range of SQL functions, and this project covered a subset of commonly used functions.
First, I needed to show the existing databases to verify the world database was available. I entered the following command in the terminal:
I verified that a database named world was available.
To review the table schema, data, and number of rows in the country table, I ran the following query:
The following query demonstrated how to use aggregate functions SUM(), MIN(), MAX(), and AVG() to summarize data. Because the query did not include a WHERE condition, the functions aggregated data from all records in the country table:
This query performed the following operations:
In some cases, I needed to split a string. The following query used SUBSTRING_INDEX() to split a string where a space occurs:
After running the query, I noticed that the second column included the beginning of each region name.
Sometimes I needed to search rows using a string fragment. The following query included SUBSTRING_INDEX() as part of a condition in the WHERE clause to filter records that include Southern in the first part of the region name:
I used the LENGTH() and TRIM() functions to determine how many characters are in a string. TRIM() cleared leading and trailing blank spaces, and the LENGTH() function returned a count of the remaining characters. The next example returned only regions that have fewer than 10 characters in their names:
I noticed duplicate records in the previous example. I used the DISTINCT() function to filter the duplicates:
I was tasked with querying the country table to return a set of records based on the following requirement:
Write a query to return rows that have Micronesian/Caribbean as the name in the region column. The output should split the region as Micronesia and Caribbean into two separate columns: one named Region Name 1 and one named Region Name 2.
My solution:
I successfully completed all the objectives: