Working of Order by and Group by on multiple columns explained

Working of Order by and Group by on multiple columns explained  :
[Thanks to stackoverflow]


--Note : When multiple columns are mentioned in the order by clause then the data returned if first orders by employee name and the if any row have the same employee name then it orders by employee id .
/*

The GROUP BY clause is used in conjunction with the aggregate functions to group the result-set by one or more columns. e.g.:

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

Remember this order:

1) SELECT (is used to select data from a database)

2) FROM (clause is used to list the tables)

3) WHERE (clause is used to filter records)

4) GROUP BY (clause can be used in a SELECT statement to collect data across multiple records and group the results by one or more columns)

5) HAVING (clause is used in combination with the GROUP BY clause to restrict the groups of returned rows to only those whose the condition is TRUE)

6) ORDER BY (keyword is used to sort the result-set
________________________________________________

More on working of group by on multiple columns :

Group By X means put all those with the same value for X in the one group.

Group By X, Y means put all those with the same values for both X and Y in the one group.

To illustrate using an example, let's say we have the following table, to do with who is attending what subject at a university:

Table: Subject_Selection

Subject   Semester   Attendee
---------------------------------
ITB001    1          John
ITB001    1          Bob
ITB001    1          Mickey
ITB001    2          Jenny
ITB001    2          James
MKB114    1          John
MKB114    1          Erica

When you use a group by on the subject column only; say:

select Subject, Count(*)
from Subject_Selection
group by Subject
You will get something like:

Subject    Count
------------------------------
ITB001     5
MKB114     2

...because there are 5 entries for ITB001, and 2 for MKB114

If we were to group by two columns:
select Subject, Semester, Count(*)
from Subject_Selection
group by Subject, Semester
we would get this:

Subject    Semester   Count
------------------------------
ITB001     1          3
ITB001     2          2
MKB114     1          2

This is because, when we group by two columns, it is saying "Group them so that all of those with the same Subject and Semester are in the same group, and then calculate all the aggregate functions (Count, Sum, Average, etc.) for each of those groups". In this example, this is demonstrated by the fact that, when we count them, there are three people doing ITB001 in semester 1, and two doing it in semester 2. Both of the people doing MKB114 are in semester 1, so there is no row for semester 2 (no data fits into the group "MKB114, Semester 2")

*/

Comments

Popular Posts