Class-16

Solving the Customer List Challenge in PostgreSQL with pgAdmin

In this tutorial, we will walk through how to solve the challenge of retrieving a list of all customers from a database. Our goal is to extract the first name, last name, and email address of all customers in an online movie rental shop’s database. Let’s go step by step using pgAdmin.


Step 1: Identifying the Right Table

Before writing our query, we first need to find the correct table that contains the customer data. Here’s how we do that:

  1. Open pgAdmin and navigate to the Green Cycles database.
  2. Expand the public schema where the tables are stored.
  3. Look for a table named customer. Since its name suggests it stores customer-related information, it is most likely the table we need.
  4. Expand the customer table to view its columns.

Alternatively, we can verify the table structure using a simple query:

SELECT * FROM customer;

Executing this query will display all columns in the customer table. From the output, we can confirm that the customer table contains the first_name, last_name, and email columns.


Step 2: Writing the SQL Query

Now that we have identified the correct table, we can refine our query to extract only the necessary columns:

SELECT first_name, 
       last_name, 
       email 
FROM customer;
  • SELECT is used to specify the columns we want to retrieve.
  • We list first_name, last_name, and email, separating them with commas.
  • FROM customer; tells PostgreSQL to fetch the data from the customer table.

Step 3: Executing the Query in pgAdmin

  1. Open the Query Tool in pgAdmin by right-clicking on the Green Cycles database and selecting Query Tool.
  2. Enter the query above in the editor.
  3. Click on the Execute (Play) button or press F5.
  4. The result will display a list of all customers with their first name, last name, and email address.

Step 4: Handling Common Errors

One of the most common errors beginners encounter is an extra comma at the end of the column list. For example:

SELECT first_name, 
       last_name, 
       email,  -- Incorrect! Extra comma here
FROM customer;

This will result in a syntax error. PostgreSQL will highlight the issue, often pointing to a syntax error near FROM. To fix it, simply remove the extra comma.


Step 5: Exporting the Customer List

Once we have retrieved the customer list, we may need to share it with other departments, such as the Marketing Team. A common format for data export is CSV (Comma-Separated Values).

To export the data:

  1. Click on the Save Data to File button in the pgAdmin results window.
  2. Choose a location and save the file as a .csv file.
  3. The file can now be opened in Excel or any other spreadsheet software for further analysis.

Next Steps: Sorting the Data

Now that we have extracted the customer list, the next step is to learn how to sort the data based on a specific column. In the upcoming lessons, we will explore how to order results using the ORDER BY clause.

Stay tuned for the next tutorial where we will discuss sorting techniques in PostgreSQL!


Was this article helpful?