SELECT
Example
Section titled “Example”SELECT firstName, lastNameFROM EmployeesWHERE department = 'Sales';Output
Section titled “Output”| firstName | lastName |
|---|---|
| John | Doe |
| Jane | Smith |
Explanation
Section titled “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
Section titled “Example”SELECT name, age FROM Users WHERE age > 30;Output
Section titled “Output” name | age-------|----- John | 32 Alice | 35 Bob | 45Explanation
Section titled “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
Section titled “Example”Here is the simple SQL query:
SELECT *FROM Employees;Output
Section titled “Output”Assume that a table named Employees contains the following data:
| EmployeeID | FirstName | LastName ||------------|-----------|----------|| 1 | John | Doe || 2 | Jane | Doe || 3 | Bob | Smith |Explanation
Section titled “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
Section titled “Example”SELECT first_name, last_nameFROM employeesWHERE employee_id = 100;Output
Section titled “Output”FIRST_NAME | LAST_NAME--------------------Steven | KingExplanation
Section titled “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
Section titled “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
Section titled “Output”id | name | position | salary----|----------------|-----------------|------1 | John Doe | Manager | 50002 | Jane Smith | Sales Associate | 30003 | Alice Johnson | Accountant | 4000Explanation
Section titled “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.