SQL SELECT - Basic Queries-020425-064414
SQL SELECT - Basic Queries-020425-064414
I. Learning Expectations:
The SELECT statement is the fundamental SQL command used to retrieve data from one or more tables in a database.
It allows you to specify which data you want to retrieve and how you want it to be organized.
Syntax:
Example:
Consider the employees table in the company_db database, which has columns like emp_id , first_name , last_name ,
email , hire_date , etc.
This will return all rows and all columns from the employees table.
Selecting Specific Columns:
To retrieve specific columns, list the column names separated by commas after the SELECT keyword.
Syntax:
Example:
To retrieve only the first_name , last_name , and email columns from the employees table:
This will return only the specified columns for all employees.
1 SELECT project_name
2 FROM projects
3 WHERE phase != 'Research';
4 -- Or
5 SELECT project_name
6 FROM projects
7 WHERE phase <> 'Research';
Logical Operators:
To retrieve employees from the "Engineering" department with a salary greater than $80,000 (assuming salary information is in a
salaries table):
The ORDER BY clause is used to sort the result set in ascending ( ASC ) or descending ( DESC ) order based on one or more columns.
Syntax:
HR Department:
Retrieve all employees: SELECT * FROM employees;
Retrieve employee names and their department:
Retrieve employees hired after January 1, 2023: SELECT first_name, last_name FROM employees WHERE hire_date > '2023-
01-01';
Retrieve employees sorted by hire date (most recent first): SELECT first_name, last_name, hire_date FROM employees ORDER
BY hire_date DESC;
Project Management:
Retrieve all projects: SELECT * FROM projects;
Retrieve project names and start dates: SELECT project_name, start_date FROM projects;
Retrieve projects in the "Completed" phase: SELECT project_name FROM projects WHERE phase = 'Completed'; (Assuming
'Completed' is a value in a phase column)
Retrieve projects sorted by start date: SELECT project_name, start_date FROM projects ORDER BY start_date;