SELECT
SELECT is a SQL command used to retrieve data from a database. The data returned is stored in a result table, also known as the result-set. It is one of the most fundamental components in all of SQL, serving as the main component in queries which allows precise and specific data retrieval.
Example
SELECT firstName, lastNameFROM EmployeesWHERE department = 'Sales';
Output
firstName | lastName |
---|---|
John | Doe |
Jane | Smith |
Explanation
The SELECT statement retrieves data from the ‘Employees’ table. Only employees whose department is ‘Sales’ are included, and only their First and Last names are displayed.
Example
SELECT name, age FROM Users WHERE age > 30;
Output
name | age-------|----- John | 32 Alice | 35 Bob | 45
Explanation
The provided SQL command selects and displays the ‘name’ and ‘age’ of users from the ‘Users’ table where the ‘age’ is greater than 30.
Example
Here is the simple SQL query:
SELECT *FROM Employees;
Output
Assume that a table named Employees contains the following data:
| EmployeeID | FirstName | LastName ||------------|-----------|----------|| 1 | John | Doe || 2 | Jane | Doe || 3 | Bob | Smith |
Explanation
The SELECT
statement is used to select data from a database. The data returned is stored in a result table, called the result-set. It is the most commonly used statement in SQL. ”*” is used to select all columns present in a table. Here, SELECT * FROM Employees;
retrieves all data from the ‘Employees’ table.
Example
SELECT first_name, last_nameFROM employeesWHERE employee_id = 100;
Output
FIRST_NAME | LAST_NAME--------------------Steven | King
Explanation
In the example, the SELECT statement is used to retrieve specific data from a database, which in this case is the first_name
and last_name
columns from the employees
table. The WHERE clause is used to filter the records and only include employees whose employee_id
is 100. Therefore, the output reflects the first name and last name of the employee with the ID of 100.
Example
CREATE TABLE employees ( id INTEGER PRIMARY KEY, name TEXT, position TEXT, salary INTEGER);
INSERT INTO employees (name, position, salary)VALUES ('John Doe', 'Manager', 5000), ('Jane Smith', 'Sales Associate', 3000), ('Alice Johnson', 'Accountant', 4000);
SELECT * FROM employees;
Output
id | name | position | salary----|----------------|-----------------|------1 | John Doe | Manager | 50002 | Jane Smith | Sales Associate | 30003 | Alice Johnson | Accountant | 4000
Explanation
The SELECT * FROM employees;
command is used to query all records from the employees table. It returned a table with all employees with their respective id, name, position, and salary. The command SELECT *
is used to select all columns from the specified table.