-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11-Group_by.sql
More file actions
153 lines (121 loc) · 5.06 KB
/
11-Group_by.sql
File metadata and controls
153 lines (121 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Group by clause is used to group a selected set of rows into a set of summary rws by the values of
one or more column or expresions. It is always used in conjunction with one or more aggregate function
SELECT city, sum(salary) as Totalsalary From tblemployee Group by city
Note: If you omit , the GROUP by clause and try to execute the query, you ger an error
Column 'tblEmployee.city' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Filtering Groups :
WHERE clause is used to filter rows before aggregation, where as HAVIG clause is used
to filter groups after aggregation, The following 2 queris produce the same result.
Select city, SUM(salary) as TOTALSalary from tblEmployee where city = 'London' group by city
Select city, SUM(Salary) as TotalSalary from tblEmployee group BY city having city = 'London'
NOTE: From a perfomance standpoint, You cannot say that one method is less efficiecnt than
the other. SQL server optimizer analyses each statement and selects an efficient way of executing it.
as a best practice use the syntax that clearly describes the desired result. Try to eleminate rows that
you wouldn't need as early as possible
*/
--Create table with column ID, name, gender, salary, city,
use My_new_database
go
Create table tblEmployee
(
ID int identity(1,1) primary key,
Name nvarchar(50) NOT NULL,
Gender nvarchar(10) NULL default 'Unknown',
Salary int NOT NULL,
city nvarchar(50) NOT NULL
)
--showing result into console
use My_new_database
GO
select *from tblEmployee
order by Salary
--insert data into tblEmployee table
insert into tblEmployee(Name, Gender, Salary, city) values('Tom', 'Male', 4000, 'London');
insert into tblEmployee(Name, Gender, Salary, city) values('Pam', 'Female', 3000, 'New York');
insert into tblEmployee(Name, Gender, Salary, city) values('John', 'Male', 3500, 'London');
insert into tblEmployee(Name, Gender, Salary, city) values('Sam', 'Male', 4500, 'London');
insert into tblEmployee(Name, Gender, Salary, city) values('Todd', 'Male', 2800, 'Sydney');
insert into tblEmployee(Name, Gender, Salary, city) values('Ben', 'Male', 7000, 'New York');
insert into tblEmployee(Name, Gender, Salary, city) values('Sara', 'Female', 4800, 'Sydney');
insert into tblEmployee(Name, Gender, Salary, city) values('Valarie', 'Female', 5500, 'New York');
insert into tblEmployee(Name, Gender, Salary, city) values('James', 'Male', 6500, 'London');
insert into tblEmployee(Name, Gender, Salary, city) values('Russel', 'Male', 8800, 'London');
insert into tblEmployee(Name, Gender, Salary, city) values('Firoz', 'Male', 0, 'Dhaka');
--Group by city
Select city, SUM(salary) as Totalsalary
From tblEmployee GROUP BY CITY
--showing result into console
use My_new_database
GO
select *from tblEmployee
order by Salary
--total sum of a column "Use of SUM"
Select sum(salary) as TOTAL_SALARY from tblEmployee
--Use of MIN
Select MIN(salary) as MIN_SALARY from tblEmployee
--Use of MAX
Select MAX(salary) as MAX_SALARY from tblEmployee
--Group by city "Use of sUM"
Select city, SUM(salary) as Totalsalary
From tblEmployee GROUP BY CITY
--Group by city "Use of MIN"
Select city, MIN(salary) as MIN_SALARY
From tblEmployee GROUP BY CITY
--Group by city "USE of MAX"
Select city, MAX(salary) as MAX_SALARY
From tblEmployee GROUP BY CITY
--Group by city and than gender
Select city, Gender, SUM(salary) as MAX_SALARY
From tblEmployee GROUP BY city, Gender order by city
--Total number of records
Select COUNT(ID)as Total_employee from tblEmployee
--Group by city and than gender
Select city, Gender,
SUM(salary)
as TOTA_SALARY,
count(ID) as [Total employee]
From tblEmployee
GROUP BY city,
Gender order by city
--Group by city and than gender
Select city, Gender,
SUM(salary)
as TOTA_SALARY,
count(ID) as [Total employee]
From tblEmployee
where Gender = 'Male'
GROUP BY city,
Gender order by city
--use having class
--Group by city and than gender
Select city, Gender,
SUM(salary)
as TOTA_SALARY,
count(ID) as [Total employee]
From tblEmployee
GROUP BY city,Gender
HAVING Gender = 'Male'
/*
DIfference - Where and Having
1. WHERE clasuse can be used with- Select, insert, and Update statements
Where as Having clasuse can only be used with the SelectSatment.
2. WHERE filters rows befor aggregation(GROUPING), Where as Having filters
, After the aggregation are Performed
3. Aggregate function cannot be used un the WHERE clause, unless it is
in sub query cantained in a HAVING clause, wheres, aggegate functions
can be used in Having clause
*/
/*
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a
HAVING clause or a select list, and the column being aggregated is an outer reference.
*/
select * from tblEmployee where SUM(Salary) > 4000
--Group by city and than gender
Select city, Gender,
SUM(salary)
as TOTA_SALARY,
count(ID) as [Total employee]
From tblEmployee
GROUP BY city,Gender
HAVING SUM(Salary) > 4000