The SELECT
statement in SQL is used to query and retrieve data from one or more tables in a database. It is one of the most fundamental and widely used commands in SQL. Below is a comprehensive overview of the SELECT
statement, including its syntax, various clauses, and examples of how to use it.
The basic syntax of the SELECT
statement is as follows:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
SELECT
: Specifies the columns to be retrieved.FROM
: Indicates the table from which to retrieve the data.WHERE
: (Optional) Filters records based on specified conditions.To select all columns from a table, use the asterisk (*
):
SELECT * FROM table_name;
Example:
SELECT * FROM cats;
To select specific columns, list them after the SELECT
keyword:
SELECT column1, column2 FROM table_name;
Example: