Alright, let's begin retrieving data. As we saw in the previous chapter, relational databases store information neatly organized into tables, like spreadsheets with rows and columns. To get that information out for analysis, reporting, or any other purpose, Structured Query Language (SQL) provides the SELECT
statement. This is arguably the most fundamental and frequently used command in SQL. Think of it as your primary tool for asking questions of your database.
The most basic form of the SELECT
statement requires two main components:
SELECT
: Followed by a list of the columns you want to retrieve data from.FROM
: Followed by the name of the table where these columns reside.The structure looks like this:
SELECT column_name1, column_name2
FROM table_name;
Let's break this down:
SELECT
and FROM
are SQL keywords. They tell the database engine what kind of operation you intend to perform. While SQL is generally case-insensitive (meaning SELECT
, select
, and SeLeCt
are often treated the same), it's common practice and good for readability to write SQL keywords in uppercase.column_name1, column_name2
represent the specific columns you're interested in. If you want more than one column, you separate them with commas.table_name
is the identifier for the table containing the data you wish to query.;
) at the end signifies the end of the SQL statement. While not strictly required by all database systems or interfaces, using it is standard practice and helps avoid ambiguity, especially when writing multiple statements together.Imagine we have a simple table named Products
that stores information about items in an inventory:
product_id | product_name | category | price |
---|---|---|---|
101 | Widget | Gadget | 19.99 |
102 | Gizmo | Gadget | 25.50 |
103 | Sprocket | Part | 5.75 |
104 | Thingamajig | Gadget | 12.00 |
If you wanted to get a list of just the names of all the products, you would write the following query:
SELECT product_name
FROM Products;
Executing this query against the database would return a result set containing only the values from the product_name
column:
product_name |
---|
Widget |
Gizmo |
Sprocket |
Thingamajig |
If you needed both the name and the price of each product, you would list both columns after the SELECT
keyword, separated by a comma:
SELECT product_name, price
FROM Products;
This query would produce a result set with two columns:
product_name | price |
---|---|
Widget | 19.99 |
Gizmo | 25.50 |
Sprocket | 5.75 |
Thingamajig | 12.00 |
This basic SELECT ... FROM ...
structure is the foundation for nearly all data retrieval in SQL. In the following sections, we'll build upon this by learning how to select all columns, rename columns in the output, limit the number of results, and eventually filter, sort, and combine data in much more sophisticated ways. For now, focus on understanding this core syntax: specify what you want (SELECT column(s)
) and where it is (FROM table
).
© 2025 ApX Machine Learning