Table of Contents
Introduction to Clauses in SQL
Clauses in SQL is used to restrict the data of the various format without using clauses in SQL we can retrieve data from the database. Like SELECT is also the clause in SQL with the help of this select clause we can easily select the column which we want to retrieve.
Types of Clauses in SQL
- Select Clause.
- Form Clause.
- Where Clause.
- Order by Clause.
- Group by Clause.
- Having Clause.
Select Clause in SQL
A select clause in SQL is used to select the columns from the table to perform some task or retrieve purpose.
A select clause is a type of Data Manipulation Language in which we can easily manipulate the column which is selected.
Syntax:-
Select [column1,column2]
From Table name;
For example:-
Select first_name, last_name
From employees;
Here we retrieve the columns first_name and last_name.
From Clause
This Clause is used to call the table from the database.
Where Clause
Where clause is used to pass the condition on the selected rows basically its is used to restrict the rows from the column.
Syntax:-
Syntax:-
Select [column1,column2]
From Table name
Where (condition);
For Example:-
Select first_name, last_name
From employees
Where department_id=100;
Here we retrieve the data first_name, last_name but only for department 100.
Order by Clause
This clause is used to make the data in the order and sequence like ascending and descending format always use order by at last position.
Syntax:-
Select [column1,column2]
From Table name
Where (condition)
Order by column_name;
For Example:-
Select first_name, last_name
From employees
Where department_id=100
Order by department_id;
Here we retrieve the data first_name, last_name but only for department 100 and order it into ascending order
If you want to make it in descending order you have to give desc at last position after column.
Group By Clause
This clause is used to perform the operation on group function or group data. Multiple Inputs and provide single Output
- AVG
- COUNT
- MAX
- MIN
- STDDEV functions
- SUM
- VARIANCE
Here We use Group by Clause.
when we are dealing with a group of data at that time we have to use group by function. At the time of the condition of providing a condition on a group of data, we have to use the Having Clause.
Syntax:-
Select [columns]
From [Tables]
Group by[Column]
Having [Condition];
Display the avg salary of department_50?
SQL> select department_id,Avg(salary)
2 from employees
3 where department_id=50
4 group by department_id;
DEPARTMENT_ID AVG(SALARY)
------------- -----------
50 3475.55556
This clause is used to give a condition on the group function.
Syntax:-
Select [columns]
From [Tables]
Group by[Column]
Having [Condition];
For Example:-
display department wise avg salary where avg salary must be greter than 2000?
select department_id,avg(salary)
from employees
group by department_id
having avg(salary)>2000;
DEPARTMENT_ID AVG(SALARY)
10 4400
Conclusion
In this blog, you will get in-depth knowledge of Clauses in SQL and their types.