SQLite Group By


SQLite gives the user a new set of clauses, one of which is a GROUP BY. In essence, the GROUP BY statement is an additional component of the select query that works in conjunction with other select commands to organize content differently. The GROUP BY statement is being used to create a set of rows based on the user’s query. It also provides single or multiple rows for every group. We may employ accrual accounting methods like SUM and COUNT as parameters in the GROUP BY command to provide more information about the given table. The FROM component of the SELECT query is followed by the GROUP BY statement. If a query includes a WHERE command, the GROUP BY command has to be placed after it.

If the SELECT query is an accumulated statement with a GROUP BY command, then each of the provided variables defined as an element of the GROUP BY command is evaluated for every column of the data. Each entry is then assigned to a “collection” based on the results. Entries with the same GROUP BY command results are assigned to a related group. Empty values are considered identical for aggregating rows. When evaluating a given argument in a GROUP BY command, the usual criteria for selecting a grouping configuration for analyzing textual values are applicable. The GROUP BY command’s provided argument will not have to be a parameter that appears in the output. In a GROUP BY command, the given argument cannot be an accumulated defined expression. We are going to explore how to utilize the GROUP BY command to develop a set of summarized rows from a series of values in this article.

Create Table:

First, we have to create a table named ‘Doctor’. There are different attributes of this table. The columns include Doc_id, Doc_fname, Doc_lname, Salary, and City. The attributes have different data types. The column ‘Doc_id’ has an integer data type, ‘Doc_fname’, ‘Doc_lname’, and ‘City’ has a TEXT data type. Whereas, the attribute ‘Salary’ contains the NUMERIC data type.

CREATE TABLE Doctor (
Doc_id INTEGER PRIMARY KEY,
Doc_fname TEXT,
Doc_lname TEXT,
Salary NUMERIC,
City TEXT
);

The output displays the effective execution of the above program.

Insert table:

In the next step, we have been using the INSERT query to insert the data of different doctors in the columns (Doc_id, Doc_fname, Doc_lname, Salary, and City) of the table ‘Doctor’.

INSERT INTO Doctor (Doc_id, Doc_fname, Doc_lname, Salary, City) VALUES (764, ‘Asma’, ‘Muneeb’, ‘40000’, ‘Islamabad’),
(381, ‘Laiba’, ‘Ajmal’, ‘90000’, ‘Swat’),
(904, ‘Muhammad’, ‘Ahmed’, ‘50000’, ‘Multan’),
(349, ‘Noor’, ‘Zain’, ‘120000’, ‘Karachi’),
(557, ‘Kashaan’, ‘Khalid’, ‘70000’, ‘Islamabad’);

We successfully run the query of INSERT as shown in the figure below.

SELECT query:

We can get the entire data of the table or acquire data from just a few columns. Whenever we want to obtain the information of some columns, we have to specify the names of that column in the SELECT query.

After executing the SELECT query, we get the entire data of the table ‘Doctor’. The table contains the ids, first names, last names, salaries, and cities of doctors.

Use GROUP BY clause:

In SQLite queries, the GROUP BY clause has been utilized in association with the SELECT command to organize similar data. In a SELECT query, the GROUP BY clause comes after the WHERE statement and just before the ORDER BY statement. In this instance, we have applied the GROUP BY clause on City.

>> SELECT * FROM Doctor GROUP BY City;

The data in the output is aggregated by the column of ‘City’.

Here, we just want to acquire the data of ids, first names, and salaries of doctors. The GROUP BY and ORDER BY clauses are employed on the column ‘Doc_id’.

>> SELECT Doc_id, Doc_fname, Salary FROM Doctor GROUP BY Doc_id ORDER BY Doc_id DESC;

In the resultant table, only data of the column ‘Doc_id’, ‘Doc_fname’, and salary will be obtained. The data in the table is sorted by using the ids of doctors in descending order.

Use GROUP BY command with SUM:

For the table ‘Doctor’, we are going to demonstrate using Group By statement along with the SUM function. Whether we want to consider the exact amount of income paid to every doctor, we will utilize the GROUP BY command as seen below. Here, the GROUP BY clause is employed on the column ‘Doc_fname’.

>> SELECT Doc_fname, SUM(salary) FROM Doctor GROUP BY Doc_fname;

The table of the outcome will be grouped by column ‘Doc_fname’.

Use GROUP BY command with COUNT:

We will modify the above query slightly by just adding the COUNT(Doc_lname) in the SELECT statement like the following screenshot to observe how to utilize the GROUP BY command with the COUNT method.

>> SELECT Doc_id, Doc_fname, Doc_lname, Salary, SUM(salary), COUNT(Doc_lname) FROM Doctor GROUP BY Doc_lname;

If we look at the result, it calculates the number of doctors in the table ‘Doctor’ that has the same last name. To determine the sum for every group, we will employ the SUM function.

Use the GROUP BY command for multiple columns:

We will also employ the GROUP BY statement for several columns. Let’s have a look at the instance in which we utilize the GROUP BY statement for two columns.

>> SELECT * FROM Doctor GROUP BY Salary, City;

The query returns the entire data of the table ‘Doctor’. The GROUP BY clause is applied to Salary and City both.

Conclusion:

With the help of several illustrations, we have explained when and how to utilize the Group by clause in the preceding article. The GROUP BY statement in SQLite is being applied to combine data in a single record wherein the result of one or even more defined columns is duplicated. This functionality will be used to reduce the extent of entries found by merely looking for distinct column values. We also noticed how the Group by statement could be used to do numerous operations dependent on our needs. We can utilize the GROUP BY command along with the COUNT and SUM functions. And we employ the GROUP BY command for multiple columns.



Source link