Select Statement SQL
The SQL SELECT statement is used to retrieve data from one or more tables in a database. It is one of the fundamental SQL commands and is structured as follows:
SQL
SELECT column1, column2, ...
FROM table_name;
- SELECT: This keyword is used to indicate that data is being retrieved from the database.
- column1, column2, ...: These are the names of the columns that you want to retrieve data from. If you want to retrieve data from all columns, you can use the asterisk (*) wildcard.
- FROM: This keyword specifies the table or tables from which you want to retrieve the data.
Example
SELECT first_name, last_name
FROM employees;
This SQL statement retrieves the first_name and last_name columns from the employees table.
You can also add additional clauses to the SELECT statement to filter or modify the data being retrieved:
- WHERE: This clause is used to specify conditions that must be met for the rows to be included in the result set.
- ORDER BY: This clause is used to sort the result set based on one or more columns.
- GROUP BY: This clause is used to group rows that have the same values into summary rows.
- HAVING:This clause is used in combination with the GROUP BY clause to specify conditions for which group of rows will be included in the result set.
- LIMIT: This clause is used to limit the number of rows returned by the query.
Example with WHERE clause:
Example
SELECT first_name, last_name
FROM employees
WHERE department_id = 10;
This SQL statement retrieves the first_name and last_name columns from the employees table where the department_id is equal to 10.