简体   繁体   中英

What is the basic functioning of group by in SQL?

What is the behaviour of group by ?

I have two tables:

create table department
(
     dep_id int primary key,
     dep_id varchar(20),
     dep_location varchar(20)
)

and

create table employees_dep 
(
    emp_id int primary key,
    emp_name varchar(20),
    job_name varchar(20),
    manager_id int,
    hire_date date,
    salary decimal(10, 2),
    commision decimal(7, 2),
    dep_id int 
        foreign key references department(dep_id)
)

With data as:

insert into department values (1001, 'finance', 'sydney')
insert into department values (2001, 'audit', 'melbourne')
insert into department values (3001, 'marketing', 'perth')
insert into department values (4001, 'production', 'brisbane')

insert into employees_dep 
values (68319, 'kayling', 'president', null, '11-18-1991', 6000, 0, 1001)
insert into employees_dep 
values (66928, 'blaze', 'manager', 68319, '05-01-1991', 2750, 0, 3001)
insert into employees_dep 
values (67832, 'clare', 'manager', 68319, '06-09-1991', 2550, 0, 1001)
insert into employees_dep 
values (65646, 'jonas', 'manager', 68319, '04-02-1991', 2957, 0, 2001)
insert into employees_dep 
values (67858, 'scarlet', 'analyst', 65646, '04-19-1991', 3100, 0, 2001)
insert into employees_dep  
values (69062, 'frank', 'analyst', 65646, '12-03-1991', 3100, 0, 2001)
insert into employees_dep 
values (63679, 'sandrine', 'clerk', 69062, '12-18-1991', 900, 0, 2001)
insert into employees_dep  
values (64989, 'adelyn', 'salesman', 66928, '02-20-1991', 1700, 400, 3001)
insert into employees_dep 
values (65271, 'wade', 'salesman', 66928, '02-22-1991', 1350, 600, 3001)
insert into employees_dep 
values (66564, 'madden', 'salesman', 66928, '09-28-1991', 1350, 1500, 3001)
insert into employees_dep 
values (68454, 'tucker', 'salesman', 66928, '09-08-1991', 1600, 0, 3001)
insert into employees_dep 
values (68736, 'andres', 'clerk', 67858, '05-23-1997', 1200, 0, 2001)
insert into employees_dep 
values (69000, 'julius', 'clerk', 66928, '12-03-1991', 1050, 0, 3001)
insert into employees_dep 
values (69324, 'marker', 'clerk', 67832, '01-23-1992', 1400, 0, 1001)

The question: when I write a query as:

select  
    d.dep_name,
    count(d.dep_name) as no_of_employees 
from 
    employees_dep e, department d  
group by 
    d.dep_name

The output is:

  • all the department names with no_of_employees as 14 in all the rows

Why is this so because the group by clause selects one data and make group of it so it should work as select the first department name and then group all the rows in employee table with same department id and then count the no of rows in each group when i write query as -

select  
    d.dep_name,
    count(d.dep_name) as no_of_employees 
from 
    employees_dep e, department d 
where 
    e.dep_id = d.dep_id  
group by 
    d.dep_name 

Then it returns the correct output with the correct number of occurrences of each department in the table.

Please explain this behaviour of group by....

Use inner join. hope this help

select d.dep_name, count(d.dep_name) as no_of_employees from employees_dep e inner join department d on e.dep_id = d.dep_id group by d.dep_name

You much use a joining condition in your first query. Else it will do a cross join. Using proper joining condition will give you the proper output.

select  d.dep_name,count(1) as no_of_employees
from employees_dep e join department d  
On e.dep_id=d.dep_id
group by d.dep_name

You are using implicit join syntax and are creating a Cartesian Join (Cross Join) which means every possible combination of the 2 tables is then being counted. So if you have 14 departments every employee will look as if it is in 14 of them. Researching how to do joins in SQL and use explicit join syntax will help out. In this case INNER JOIN as others have also pointed out will be the key to correcting the issue.

SELECT
    d.dep_name
    ,count(emp_id) as no_of_employees
FROM
    department d
    INNER JOIN employees_dep e
    ON e.dep_id = d.dep_id
GROUP BY
    d.dep_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM