Use Sophia to knock out your gen-ed requirements quickly and affordably. Learn more
×

SELECT to Display Data

Author: Sophia

what's covered
This tutorial shows how to limit your results to only the columns of information you desire. You will learn how to write an SQL query that displays only your chosen columns in two parts:
  1. Using SELECT to Display Data
  2. Displaying Multiple Columns

1. Using SELECT to Display Data

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:

schema

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:

table

2. Displaying Multiple Columns

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:

table

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;

table

You could even have the same column twice:


SELECT email, email
FROM customer;

This query returns:

table

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

Video Transcript

try it
Your turn! Open the SQL tool by clicking on the LAUNCH DATABASE button below. Then enter in one of the examples above and see how it works. Next, try your own choices for which columns you want the query to provide.

term to know

Schema Browser
A list of table names, column names, and data types contained within a database.
summary
In this tutorial, you learned how to write a SELECT statement to choose specific columns from your database table to display in the results.

Source: Authored by Vincent Tran