Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

SQL Group By Clause

 


SQL Group By Clause

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 with some repetition:

+----+-------------+------+-----------------+---------------+

| ID  | NAME      | AGE  | ADDRESS      | SALARY       |

+----+-------------+------+-----------------+---------------+

| 1    | Ramesh   | 32    | Ahmedabad   | 2000.00      |

| 2    | Ramesh   | 25    | Delhi                | 1500.00      |

| 3    | kaushik   | 23    | Kota                 | 2000.00      |

| 4    | Chaitali    | 25   | Mumbai          | 6500.00       |

| 5    | Chaitali    | 27    | Bhopal            | 8500.00       |

| 6    | Komal      | 22    | MP                   | 4500.00       |

| 7    | Muffy       | 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,

ORDER BY NAME;

 

+------------+--------------------+

| NAME      | SUM(SALARY)  |

+------------+--------------------+

| Chaitali   | 15000.00           |

| kaushik  | 2000.00              |

| Komal    | 4500.00               |

| Muffy     | 10000.00             |

| Ramesh | 4500.00               |

+-----------+---------------------+

 





आशा करते है कि हमारे द्वारा लिखी हुई इस Post को आप Like, Share and Comment करेंगे | और हमें Comment करके बताये की आपको ये Post कैसी लगी | इसी तरह की Post और News पाने के लिए हमारे सभी Social Media Channels को रेगुलर Follow करे| 


Thanks
Amar Digital World
(Always Ready To Digital Serve)

Post a Comment

0 Comments