SQL statements can be used to update, insert, delete, and query data. We will
narrow our focus to querying data, and not drill down to the acute specifics.
Whenever you want to obtain data from a table in your database, you need to use
a SELECT statement, which has the flowing form:
SELECT
selectList FROM TableName [ WHERE searchClause] [ ORDER BY orderExpression
[ASC ; DESC]]
Although there are optional clauses of the SELECT statement, a SELECT statement
must consist of at least SELECT List FROM Table Name. Each capitalized keyword
in a SELECT statement is referred to as a clause. A SELECT statement must contain
a SELECT and FROM clause, whereas the WHERE and ORDER by BY clauses are optional.
The
select List contains a comma-delimited list of the columns that want to return,
whereas the Table Name is the name of the table from which you want to retrieve
data.
If
you wanted to create a SQL statement that would retrieve every column from the
products table, the select List would need to contain each column of the table,
each separated by a comma. The following example returns all the rows and columns
in the products table:
SELECT
Product ID, Name, Price, Inventory FROM Products
If
you want to obtain all the columns, you can use the asterisk (*) in the select
List to denote that you want to include all the columns. The following two SQL
statements are functionally equivalent:
1.
SELECT Product ID, Name, Price, Inventory FROM Products 2. SELECT * FROM Products
Note
that line 2 uses an asterisk in place of all the column names. Both SQL statements
return all the columns and rows of the products table.
If you wanted to
retrieve just the Name of all products, you could modify the SQL statements
select List to contain just the Name column:
SELECT
Name FROM Products
If
you wanted to obtain just the Name and Price of all products, you could add the
Price column to the select List: