In this article, I will go through the main idea of retrieving data with a SELECT statement.
After reading this article you will be able to :
- Write a SELECT statement.
- Tell a database where the data will come FROM.
- SELECT either all or particular columns from a table in a query.
- Limit the amount of data returned inside a query.
Returning column data using a SELECT query.
There are two pieces of information needed for a SELECT statement. The first is the column name and the second is where you want to select it FROM.
# column name
SELECT prod_name
# table name
FROM Products
Retrieving more than a single column can be done using the following:
# column names
SELECT prod_name,
prod_id,
prod_price
# table name
FROM Products;
You will have noticed that the formatting has changed, you can write the query in the same line, but this way since is easier to read.
Retrieving Multiple Columns Using a Wildcard
You can request all columns by using the asterisk.
# column names
SELECT *
# table name
FROM Products;
Why Limit Results?
You can imagine big tables might display thousands or millions of rows of data. So we can LIMIT our data.
# column name
SELECT prod_name
# table name
FROM Products
# limit data
LIMIT 5
This syntax for limiting data changes slightly based on the different systems used. The example above uses SQLite. If you know which system you are using, a simple google search will help you find what you need.