SQL order of execution

 

SQL order of execution is: "FM WG HAVING SOL"

1: from

2: where

3: group by

4: having

5: select

6:order by

7:limit

eg: Write an SQL Query find number of Employees with GENDERaccording to gender whose DOB is between '01/01/1996' to '31/12/1999'



select count(*) as NO OF EMPLYEES,Gender from Employees WHERE DOB BETWEEN '01/01/1996' AND '31/12/1999'

GROUP BY GENDER

STEPS:

1.FROM  CHOOSE AND JOIN TABLE TO GET BASE DATA

Here "EMPLOYEES" is Selected.


2.WHERE  Filters the DATA

WHERE DOB BETWEEN '01/01/1996' AND '31/12/1999'

3.GROUP BY AGGREGATES THE BASE DATA

GROUP BY GENDER MALE,FEMALE

MALE: 1

FEMALE: 3,4

4.HAVING FILTERS THE AGGREGATED DATA

EG: HAVING 
EMPID < 100

5.SELECT : RETURNS FINAL DATA

6.ORDER BY : SORTS THE FINAL DATA

EG ORDER BY NO OF EMPLYEES ASC


7.LIMIT : LIMIT THE RETURN DATA TO ROW COUNT



select count(*) as NO OF EMPLYEES,Gender from Employees WHERE DOB BETWEEN '01/01/1996' AND '31/12/1999' LIMIT 2

OR TOP IN SQL SERVER

select TOP 2 count(*) as NO OF EMPLYEES,Gender from Employees WHERE DOB BETWEEN '01/01/1996' AND '31/12/1999' LIMIT 2







Comments

Popular posts from this blog

SQL SERVER INTERVIEW QUESTION AND ANSWERS PART 1

Selenium questions