Exploring SELECT Statements
Exploring SELECT Statements
Welcome back! In the previous lesson, you learned the basics of databases and SQL, focusing on using PostgreSQL. You also wrote some initial SQL queries to get familiar with listing tables and retrieving data. In this lesson, you will learn how to retrieve specific data using the SELECT statement.
Understanding the SELECT Statement
The SELECT statement is one of the fundamental building blocks of SQL. Its primary purpose is to retrieve data from a database table. Here's a breakdown of a basic SELECT statement:
SELECT: This keyword initiates the retrieval operation.column1, column2, ...: These are the columns you want to retrieve from the table.FROM: This keyword specifies the table from which to retrieve the data.table_name: The name of the table to retrieve data from.
Basic SELECT Statement
In the last lesson, we learned how to use * to select all columns from a table. The asterisk (*) is a wildcard character that tells SQL to select all columns from the movies table. To review:
This code outputs:
Notice that the output contains all 4 column names.
Selecting Specific Columns
Now, let's say we only want to retrieve the movie_name and release_date columns from the movies table. Here's how you can do it:
- The
SELECTstatement now specifiesmovie_nameandrelease_dateexplicitly, meaning only these columns will be retrieved. - The
FROMclause still indicates themoviestable as the source.
