GROUP BY Clause in SQLite

SQLite GROUP BY Clause
To group similar elements, from a table after fetching the records the GROUP BY clause is used in the SELECT statement. It is used with the WHERE clause before the ORDER BY clause.

Syntax:

SELECT column-list  
FROM table_name  
WHERE conditions 
GROUP BY column1, column2...column_N  
ORDER BY column1, column2...column_N [ASC | DESC];

Example 1:
TEACHERS Table:

IDNAMESALARYSUBJECT
1Jim10000Geology
2John20000Geology
3Watson15000Physics
4Holmes25000Chemistry
5Tony30000Physics
SELECT SUBJECT, SUM(SALARY) AS “SUM SALARY”
FROM TEACHERS 
GROUP BY SUBJECT;

Output:

SUBJECTSUM SALARY
Geology30000
Physics45000
Chemistry25000

Explanation:
In the above example, the SUM of the salary is grouped by the SUBJECT Column.

Example 2:
TEACHERS Table:

IDNAMESALARYSUBJECT
1Jim10000Geology
2John20000Geology
3Watson15000Physics
4Holmes25000Chemistry
5Tony30000Physics

SELECT SUBJECT, SUM(SALARY) 

FROM TEACHERS 
GROUP BY SUBJECT
ORDER BY SUBJECT ASC;

Output:

SUBJECTSUM SALARY
Chemistry25000
Geology30000
Physics45000

Explanation:
In the above example, the SUM of the salary is grouped by the SUBJECT Column and is then ordered in Ascending order by the SUBJECT Column.

Please Share