Friday, 7 June 2013

Study SQL Commands

1. Group By
2. Having
3. Order By
4. Top


1. Group By :- Group By command is used along with select command . Generally, this command is used to group the desired result by eliminating the duplicate Column-Values and displaying them one time. The Syntax of Group By command is :-

SYNTAX :-

Select Column-name1, Column-name2
from Table-Name
Group By Column-name

EXAMPLE

Select Emp_Name, Emp_designation
From employee
group by Emp_designation

2. HAVING :- HAVING is used with select command and GroupBy command . Having is used in place of where clause if Aggregate value like aggregate salary or age related query is to be made to database. The Syntax  is as follows :-

SYNTAX :-


Select Column-name1, Column-name2
from Table-Name
Group By Column-name
Having function-Name ( Column-Name) Operator Value


EXAMPLE :-

Select Emp_Name, Emp_designation

From employee
group by Emp_designation
Having count(Emp_Salary) > 20000

3. ORDER BY :- ORDER BY Clause is used to Sort The result to be displayed. This Clause is used along with select Clause. If we use ORDER BY Clause then ByDefault it will display the data in Ascending order i.e. from lower value to higher value. But we can also display data in Descending order i.e. from Higher to Lower Value by using keyword 'Desc'

SYNTAX :-

Select Column-Name
From Table-Name
Order By Column-Name

EXAMPLE :-

1. 
Select Emp_Name 
From  employee
order By Emp_Salary  

2. 
Select Emp_Name 
From  employee
order By Emp_Salary  Desc


4. TOP :- TOP Clause or Command is used to display specified n number of rows from given  Table -Name. It is very usefull to use with large Table . As it consist of large no or rows so to display all rows is Time consuming task . So with TOP command we can display only required n number of columns .

SYNTAX:-

SELECT top *n
from Table-Name

EXAMPLE :-

1. 
Select top 2*  
From employee
It will display top two rows from table

2. 
Select top 2*  
From employee
order by Emp_designation

3. 
Select top 2*  
From employee
order by Emp_designation Desc

No comments:

Post a Comment