SQL SELECT Statement
The SQL SELECT statement is used to retrieve data from a database table. It is one of the most commonly used SQL commands and is an essential part of any SQL query.
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example
SELECT *
FROM customers
WHERE city = 'New York';
This example selects all columns (*) from the "customers" table where the city is "New York".
Options
- SELECT: The columns to be retrieved. Use * to select all columns.
- FROM: The table from which the data will be retrieved.
- WHERE: The condition that must be met for a row to be selected. This is optional and can be omitted if you want to select all rows from the table.
- ORDER BY: The column or columns to sort the results by. This is optional and can be used to sort the results in ascending or descending order. For example:
SELECT * FROM customers ORDER BY last_name ASC
- GROUP BY: The column or columns to group the results by. This is optional and can be used to group the results by a particular column or set of columns. For example:
SELECT * FROM orders GROUP BY customer_id
- HAVING: A condition that must be met for a group to be selected. This is similar to the WHERE clause, but is used with the GROUP BY clause to filter groups rather than individual rows.
- LIMIT: The maximum number of rows to return. This is optional and can be used to limit the number of results returned by the query. For example:
SELECT * FROM customers LIMIT 10
There are many other options and clauses that can be used with the SELECT statement, such as JOIN, UNION, and DISTINCT. You can learn more about these and other advanced topics in our SQL tutorials and references.