You are on page 1of 5

Clauses

Clauses are in-built functions that help us to access a particular set of


records from a database table. It is used to request any information
or data in the database. It helps us examine, filter and analyze data
quickly stored in the database. If there is large amount of data stored
in the database clauses help to query and modify the data easily
according to the user.
Consider following database table for Example:
StudentID Name DeptID Marks Age
1 Alice 101 55 20
2 Bob 102 34 20
3 Rachel 103 67 17
4 Carry 104 73 18
5 Skye 105 56 16
6 Alex 102 78 20
7 Jamie 104 82 18

Types Of Clauses:
Where Clause
Where clause is used to specify conditions on rows so that records
get displayed as per requirement. It is used to restrict the number of
records retrieved by the table. Where clause used with select,
update, delete statements, aggregate functions and logical
operators, etc.
Syntax:
select column_name
from table_name
where condition;

Fundamentals Of SQL
Examples:
1. select *
from Student
where DeptID = 102;
Output:
StudentID Name DeptID Marks Age
2 Bob 102 34 20
6 Alex 102 78 20

2. select StudentID, Name


from Student
where Age>=18 and Marks<60;

Output: StudentID Name


1 Alice
2 Bob
Group By
Group By clause divides the outcome into groups based on the
values specified in it. It is used with aggregate functions and is used
to group rows that have identical data in the result set.
Syntax:
select column_name
from table_name
where condition
group by column_name;

Examples:
1. select DeptID, count(*)
from Student
group by DeptID;

Fundamentals Of SQL
DeptID Count(*)
Output: 101 1
102 2
103 1
104 2
105 1

2. select Age, count(Age)


from Student
group by Age;

Output: Age count(Age)


20 3
18 2
17 1
16 1

Order By
Order By Clause is used for sorting the query output in either
ascending or descending order. By default, the data gets sorted in
ascending order or by using the keyword ASC. The data gets sorted in
descending order by using the keyword DESC.
Syntax:
select column_name,…
from table_name
where condition
order by column_name,… asc/desc;

Fundamentals Of SQL
Examples:
1. select * OR select *
from Student from Student
order by Name; order by Name asc;

Output: StudentID Name DeptID Marks Age


6 Alex 102 78 20
1 Alice 101 55 20
2 Bob 102 34 20
4 Carry 104 73 18
7 Jamie 104 82 18
3 Rachel 103 67 17
5 Skye 105 56 16

2. select *
from Student
order by Name desc;

Output: StudentID Name DeptID Marks Age


5 Skye 105 56 16
3 Rachel 103 67 17
7 Jamie 104 82 18
4 Carry 104 73 18
2 Bob 102 34 20
1 Alice 101 55 20
6 Alex 102 78 20

Fundamentals Of SQL
Having
Having clause is applied to the query output with group by clause. It
is used to limit the query output returned by group by clause. It is
similar to where clause.

Syntax:
select column_name,..
from table_name
where condition
group by column_name,…
having condition;

Examples:
1. select DeptID, count(*)
from Student
where DeptID
having count(*) > 1;

Output: DeptID count(*)


102 2
104 2

2. select Age, count (*)


from Student
group by Age
having count(*) > 1;

Output: Age count(Age)


20 3
18 2

Fundamentals Of SQL

You might also like