This SQLite post explains how to use the SQLite SELECT statement with syntax and examples.
The SQLite SELECT statement is used to retrieve records from one or more tables in SQLite.
In its simplest form, the syntax for the SQLite SELECT statement is:
However, the full syntax for the SQLite SELECT statement is:
Optional. If specified, it returns all matching rows.
Optional. If specified, it removes duplicates from the result set. Learn more about the DISTINCT clause.
The columns or calculations that you wish to retrieve. Use * if you wish to select all columns.
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
Optional. The conditions that must be met for the records to be selected.
Optional. It collects data across multiple records and groups the results by one or more columns. Learn more about the GROUP BY clause.
Optional. It is used in combination with the GROUP BY to restrict the groups of returned rows to only those whose the condition is TRUE. Learn more about the HAVING clause.
Optional. It is used to sort the records in your result set. Learn more about the ORDER BY clause.
Optional. If LIMIT is provided, it controls the maximum number of records to retrieve. At most, the number of records specified by number_rows will be returned in the result set. The first row returned by LIMIT will be determined by offset_value.
Let's look at how to use a SQLite SELECT query to select all fields from a table.
In this SQLite SELECT statement example, we've used * to signify that we wish to select all fields from the employees table where the employee_id is less than 50. The result set is sorted by last_name in ascending order.
You can also use the SQLite SELECT statement to select individual fields from the table, as opposed to all fields from the table.
For example:
This SQLite SELECT example would return only the employee_id, last_name, and first_name fields from the employees table where the employee_id is less than 50. The results are sorted by last_name in ascending order and then employee_id in descending order.
You can also use the SQLite SELECT statement to retrieve fields from multiple tables.
This SQLite SELECT example joins two tables together to gives us a result set that displays the employee_id, last_name, and title fields where the employee_id value matches in both the employees and positions table. The results are sorted by title in ascending order.