SELECT Command in SQL
The SELECT command in SQL is used to retrieve data from one or more tables in a database. The basic syntax of the SELECT statement allows you to specify which columns we want to retrieve, from which table, and any conditions that must be met for a row to be included in the result set.
SQL Syntax
FROM table_name
WHERE condition;
Examples of Using the SELECT Command 1. Selecting All Columns from a Table To select all columns from a table, we use the * wildcard:
SQL
This query retrieves all rows and all columns from the employees table. 2. Selecting Specific Columns from a Table To select specific columns, we list them explicitly:
SQL
This query retrieves the first_name, last_name, and email columns from the employees table.
3. Using the WHERE Clause To filter results based on a condition, use the WHERE clause:
SQL
FROM employees
WHERE department_id = 10;
This query retrieves the first_name, last_name, and email of employees who work in the department with department_id 10. We can see one thing that in the select column it is not necessary to include the department_id column to filter on basis of this column.
To select rows from the employees table where the department_id is equal to employee_id, you can modify our SELECT statement to include this condition in the WHERE clause.
SQL Syntax
FROM employees
WHERE department_id = employee_id;