There are instances where you may not need or want to see all the data in a table at one time. For example, you may want to just see customers' email addresses to send out a marketing campaign email. To select only email addresses from the customer table in our Postgres tool, we can write a SELECT statement that lists the column that we would like to display instead of using the *. Looking at the column list under the schema browser, we can see the list of columns:
The eighth column contains the email addresses that we want to display. Using the SELECT statement, we can alter the SELECT clause to display just the email addresses instead of all columns by replacing the * with the specific column name. The query statement would be changed to:
SELECT email
FROM customer;
Running this statement displays only email addresses:
Perhaps we would like to also include the customers' first and last names. If we want to select more than one column, we need to separate out the column list using commas. For example:
SELECT first_name, last_name, email
FROM customer;
This would return the following result set:
If you want the column list to display in a different order, you can reorder the SELECT clause:
SELECT email, last_name, first_name
FROM customer;
You could even have the same column twice:
SELECT email, email
FROM customer;
This query returns:
This may not appear useful now, but when we focus on calculations in SELECT statements, we will revisit the purpose of having columns listed more than once.
Source: Vincent Tran
Instead of selecting All Columns utilizing the star, you can also specify the exact column names that split up, utilizing a comma. For example, if you want to get the address, the first name, and last name from the customer, this would only return, the columns associated with every single customer.
So go ahead and click on Run. You'll see the query results, with still 59 rows. However, we're only returning the columns for the address, the first name, and the last name of the customer.
Source: Authored by Vincent Tran