The database operations team has created a relational database named world containing three tables: city, country, and countrylanguage. Based on specific use cases defined in the lab exercise, I wrote a few queries using database operators and the SELECT statement.
I demonstrate how to use some common database operators and the SELECT statement.
After completion I was able to:
Sample data is taken from Statistics Finland, general regional statistics, February 4, 2022.
In this task, I queried the world database using various SELECT statements and database operators.
To show the existing databases, I entered the following command in the terminal.
I verified that a database named world was available.
To list all rows and columns in the country table, I ran the following query.
To query the number of rows in a table, I can use the COUNT() function in a SELECT statement. To count all the rows in table, I can use COUNT(*). To count the number of rows that have a value in a specific column, include the column name as a parameter in the COUNT() function: for example, COUNT(Population). To list the number of rows in the country table, I ran the following query.
To list all columns in the country table, I ran the following query. I ran this query to understand the table schema.
To query specific columns in the world table, I ran the following query to return a result set that includes the Name, Capital, Region, SurfaceArea, and Population columns.
Database column names are sometimes not user friendly. To add a more descriptive column name to the query output, I can use the AS option. I ran the following query that includes this option.
If required, I scrolled to the top of the query results, and observed that the SurfaceArea column is displayed as Surface Area.
Ordered result sets are easier to view and work with. If I would like to order the output based on a column, I can use the ORDER BY option. In this example, I ordered the output based on the population.
The ORDER BY option orders data in ascending order.
To order data in descending order, I used the DESC option with ORDER BY. I ran the following command with this option.
I can add conditions to SELECT statements by using the WHERE clause. For example, to list all rows with a population greater than 50,000,000, I ran the following query.
I have used the > comparison operator. Similarly, I can use other comparison operators to compare values.
I can construct a WHERE clause by using a number of conditions and operators.
The following query uses two conditions: all rows with a population greater than 50,000,000 and all rows with a population less than 100,000,000. The query includes the AND operator to indicate that both the conditions must be true. I ran the following query in my terminal.
I queried the country table to return a set of records based on the following question.
Which country in Southern Europe has a population greater than 50,000,000?
I have now successfully: