The SQL SELECT
statement is used to retrieve data from one or more tables in a database. It is one of the most fundamental and commonly used SQL commands.
Here's the basic syntax of the SELECT
statement:
SELECT column1, column2, ...
FROM table_name;
In this syntax:
column1, column2, ...
: Columns you want to retrieve data from. You can specify one or more columns separated by commas, or you can use *
to select all columns.table_name
: The name of the table from which you want to retrieve data.Example:
Let's say we have a table named employees
with columns employee_id
, first_name
, last_name
, and salary
, and we want to retrieve data from all columns:
SELECT *
FROM employees;
This query will return all rows and columns from the employees
table.
You can also specify specific columns to retrieve:
SELECT employee_id, first_name, last_name
FROM employees;
This query will return only the employee_id
, first_name
, and last_name
columns from the employees
table.
Additionally, you can use the DISTINCT
keyword to retrieve unique rows:
SELECT DISTINCT column1, column2, ...
FROM table_name;
For example, to retrieve unique department
values from the employees
table:
SELECT DISTINCT department
FROM employees;
The SELECT
statement is versatile and allows you to filter, sort, and aggregate data using various clauses such as WHERE
, ORDER BY
, GROUP BY
, HAVING
, and more. It's fundamental for querying and retrieving data from a database in SQL.