adsense


Showing posts with label SQL GROUP BY. Show all posts
Showing posts with label SQL GROUP BY. Show all posts

Tuesday, 3 November 2015

SQL GROUP BY PAGE 21

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



SQL GROUP BY






SQL Group By 


The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups. 
The GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause. 
Syntax: 
The basic syntax of GROUP BY clause is given below. The GROUP BY clause must follow the conditions in the WHERE clause and must precede the ORDER BY clause if one is used. 
SELECT column1, column2 
FROM table_name 
WHERE [ conditions ] 
GROUP BY column1, column2 
ORDER BY column1, column2 
Example: 
Consider the CUSTOMERS table having the following records: 
+----+----------+-----+-----------+----------+ 
| ID | NAME     | AGE | ADDRESS   | SALARY   | 
+----+----------+-----+-----------+----------+ 
|  1 | Ramakrishna  |  32 | Ahmedabad |  2000.00 | 
|  2 | Mdhu sudhan rao   |  25 | Delhi     |  1500.00 | 
|  3 | Sai  |  23 | Kota      |  2000.00 | 
|  4 | Kishor  |  25 | Mumbai    |  6500.00 | 
|  5 | Ranjith    |  27 | Bhopal    |  8500.00 | 
|  6 | Abhi    |  22 | MP        |  4500.00 | 
|  7 | Kumar     |  24 | Indore    | 10000.00 | 
+----+----------+-----+-----------+----------+   
If you want to know the total amount of salary on each customer, then GROUP BY query would be as follows: 
SQL> SELECT NAME, SUM(SALARY) FROM CUSTOMERS 
     GROUP BY NAME; 
This would produce the following result: 
CHAPTER 21
22 
+----------+-------------+ 
| NAME     | SUM(SALARY) | 
+----------+-------------+ 
| Kishor  |     6500.00 | 
| Ranjith    |     8500.00 | 
| Sai  |     2000.00 | 
| Mdhu sudhan rao   |     1500.00 | 
| Abhi    |     4500.00 | 
| Kumar     |    10000.00 | 
| Ramakrishna  |     2000.00 | 
+----------+-------------+ 
Now, let us have following table where CUSTOMERS table has the following records with duplicate names: 
+----+----------+-----+-----------+----------+ 
| ID | NAME     | AGE | ADDRESS   | SALARY   | 
+----+----------+-----+-----------+----------+ 
|  1 | Ramakrishna  |  32 | Ahmedabad |  2000.00 | 
|  2 | Ramakrishna  |  25 | Delhi     |  1500.00 | 
|  3 | Sai  |  23 | Kota      |  2000.00 | 
|  4 | Sai  |  25 | Mumbai    |  6500.00 | 
|  5 | Ranjith    |  27 | Bhopal    |  8500.00 | 
|  6 | Abhi    |  22 | MP        |  4500.00 | 
|  7 | Kumar     |  24 | Indore    | 10000.00 | 
+----+----------+-----+-----------+----------+ 
Now again, if you want to know the total amount of salary on each customer, then GROUP BY query would be as follows: 
SQL> SELECT NAME, SUM(SALARY) FROM CUSTOMERS 
     GROUP BY NAME; 
This would produce the following result: 
+---------+-------------+ 
| NAME    | SUM(SALARY) | 
+---------+-------------+ 
| Ranjith   |     8500.00 | 
| Sai |     8500.00 | 
| Abhi   |     4500.00 | 
| Kumar    |    10000.00 | 
| Ramakrishna |     3500.00 | 
+---------+-------------+