30/01/2022
The syntax for the SELECT statement in SQL is:
SELECT expressions
FROM tables
[WHERE conditions]
[ORDER BY expression [ ASC | DESC ]];
Parameters or Arguments
expressions
The columns or calculations that you wish to retrieve. Use * if you wish to select all columns.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
WHERE conditions
Optional. The conditions that must be met for the records to be selected. If no conditions are provided, then all records will be selected.
ORDER BY expression
Optional. The expression used to sort the records in the result set. If more than one expression is provided, the values should be comma separated.
ASC
Optional. ASC sorts the result set in ascending order by expression. This is the default behavior, if no modifier is provider.
DESC
Optional. DESC sorts the result set in descending order by expression.
DDL/DML for Examples
If you want to follow along with this tutorial, get the DDL to create the tables and the DML to populate the data. Then try the examples in your own database!
Get DDL/DML
Example - Select All Fields from a Table
Let's look at an example that shows how to use the SQL SELECT statement to select all fields from a table.
In this example, we have a table called customers with the following data:
customer_id last_name first_name favorite_website
4000 Jackson Joe abc.com
5000 Smith Jane def.com
6000 Ferguson Samantha abcd.com
7000 Reynolds Allen abcdef.com
8000 Anderson Paige NULL
9000 Johnson Derek abc.com
Now let's demonstrate how the SELECT statement works by selecting all columns from the customers table. Enter the following SELECT statement:
Try It
SELECT *
FROM customers
WHERE favorite_website = 'abc.com'
ORDER BY last_name ASC;
There will be 2 records selected. These are the results that you should see:
customer_id last_name first_name favorite_website
4000 Jackson Joe abc.com
9000 Johnson Derek abc.com
In this example, we've used * to signify that we wish to view all fields from the customers table where the favorite_website is 'abc.com'. The result set is sorted by last_name in ascending order.