SQL SELECT Statement

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

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:

EmployeeIDNameDepartmentSalaryJoiningDate
1Tony StarkSales500002020-01-10
2Jane SmithHR600002019-04-25
3Emily DavisIT700002021-07-15
4Michael BrownMarketing550002020-09-01
5Sarah WilsonIT750002018-11-20
6Alex JohnsonSales520002021-03-12
7Olivia LeeHR580002022-06-23

SQLl SELECT Statement with WHERE Clause

Syntax

Query

Assuming you want to retrieve the details of employees from the IT department:

Output

EmployeeIDNameDepartmentSalaryJoiningDate
3Emily DavisIT700002021-07-15
5Sarah WilsonIT750002018-11-20

SQL SELECT Statement with ORDER BY clause

Syntax

Query

To retrieve all employees ordered by their Salary in descending order:

Output

EmployeeIDNameDepartmentSalaryJoiningDate
5Sarah WilsonIT750002018-11-20
3Emily DavisIT700002021-07-15
2Jane SmithHR600002019-04-25
7Olivia LeeHR580002022-06-23
4Michael BrownMarketing550002020-09-01
6Alex JohnsonSales520002021-03-12
1Tony StarkSales500002020-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.

DepartmentTotalSalary
HR118000
IT145000
Marketing55000
Sales102000

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.

DepartmentMinimumSalary
HR58000
IT70000
Marketing55000

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.