sqliteHow can I use the GROUP BY clause in SQLite?
The GROUP BY clause in SQLite is used to group together rows of data based on a given field or expression. It is used in conjunction with aggregate functions such as COUNT, SUM, AVG, MIN and MAX.
For example, the following query groups the data by the 'department' field and returns the total number of employees in each department:
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
Output example
department COUNT(*)
---------- --------
sales 10
marketing 5
engineering 15
The query consists of the following parts:
-
SELECT: The SELECT clause specifies the fields to be returned in the result set. In this case, it is the 'department' field and the COUNT aggregate function.
-
FROM: The FROM clause specifies the table from which to retrieve the data. In this case, it is the 'employees' table.
-
GROUP BY: The GROUP BY clause specifies the field or expression by which to group the data. In this case, it is the 'department' field.
-
COUNT: The COUNT aggregate function returns the number of rows in each group.
Helpful links
More of Sqlite
- How can SQLite and ZFS be used together for software development?
- How do I use the SQLite ZIP VFS to compress a database?
- How do I use SQLite with Visual Studio?
- How do I import data from a SQLite zip file?
- How do I use the SQLite zfill function?
- How do I call sqlitepcl.raw.setprovider() when using SQLite?
- How can I use SQLite to query for records between two specific dates?
- How do I show the databases in SQLite?
- How do I use the SQLite VARCHAR data type?
- How do I use SQLite with Zephyr?
See more codes...