SQL SELECT Statement
A SELECT statement in SQL is a powerful tool used to retrieve specific data from one or more tables within a database. It allows users to specify the exact columns and conditions for the data they wish to see, making it an essential command for database management and data analysis.
Syntax
The basic syntax for using SELECT statement in sql is to specify the column names we want to retrieve, followed by the FROM clause to designate the name of the database table:
In case we want to retrieve all the columns, we can replace writing individual column names with *.
Demo Database
Let's have a demo table named Employees with the following data:
EmployeeID | Name | Department | Salary | JoiningDate |
---|---|---|---|---|
1 | Tony Stark | Sales | 50000 | 2020-01-10 |
2 | Jane Smith | HR | 60000 | 2019-04-25 |
3 | Emily Davis | IT | 70000 | 2021-07-15 |
4 | Michael Brown | Marketing | 55000 | 2020-09-01 |
5 | Sarah Wilson | IT | 75000 | 2018-11-20 |
6 | Alex Johnson | Sales | 52000 | 2021-03-12 |
7 | Olivia Lee | HR | 58000 | 2022-06-23 |
SQLl SELECT Statement with WHERE Clause
Syntax
Query
Assuming you want to retrieve the details of employees from the IT department:
Output
EmployeeID | Name | Department | Salary | JoiningDate |
---|---|---|---|---|
3 | Emily Davis | IT | 70000 | 2021-07-15 |
5 | Sarah Wilson | IT | 75000 | 2018-11-20 |
SQL SELECT Statement with ORDER BY clause
Syntax
Query
To retrieve all employees ordered by their Salary in descending order:
Output
EmployeeID | Name | Department | Salary | JoiningDate |
---|---|---|---|---|
5 | Sarah Wilson | IT | 75000 | 2018-11-20 |
3 | Emily Davis | IT | 70000 | 2021-07-15 |
2 | Jane Smith | HR | 60000 | 2019-04-25 |
7 | Olivia Lee | HR | 58000 | 2022-06-23 |
4 | Michael Brown | Marketing | 55000 | 2020-09-01 |
6 | Alex Johnson | Sales | 52000 | 2021-03-12 |
1 | Tony Stark | Sales | 50000 | 2020-01-10 |
SQL SELECT Statement with GROUP BY Clause
Syntax
Query
Let's find overall salary commitments the company has made across its various departments:
Output
This SQL query selects the Department and calculates the total sum of salaries within each department SUM(Salary)from the Employees table. It then groups the results by the Department column, allowing you to see the total salary expenses distributed across the different departments in the organization.
Department | TotalSalary |
---|---|
HR | 118000 |
IT | 145000 |
Marketing | 55000 |
Sales | 102000 |
SQL SELECT Statement with HAVING Clause
Syntax
Query
To identify departments where the minimum salary is above a certain threshold:
Output
This query selects each department and the minimum salary within those departments from the Employees table, grouping the results by the department. It filters these groups to include only those where the minimum salary is greater than 50,000.
Department | MinimumSalary |
---|---|
HR | 58000 |
IT | 70000 |
Marketing | 55000 |
Conclusion
- SELECT statement in SQL is the most commonly used command since it provides the most fundamental functionality for retrieving data from a table.
- The SELECT statement in SQL is used in conjunction with a multitude of other commands for obtaining and analysing data.
- One of the most important procedures is to SELECT all of the table's columns and rows, which may be done with *.
- The usage of the SELECT statement in SQL has been demonstrated in multiple cases with WHERE, ORDER BY, GROUP BY, HAVING .
- Usage of the SELECT statement in SQL can be altered to meet specific needs, schema and scenarios.