Help Center/
GaussDB/
Developer Guide(Centralized_8.x)/
SQL Reference/
SQL Syntax/
S/
SELECT/
Group Queries
Updated on 2024-06-03 GMT+08:00
Group Queries
A group query is usually used together with aggregate functions to query category statistics. Common aggregate functions include count(), sum(), avg(), min(), and max(). GROUP BY is often used in group queries.
-- Create a table and insert data into the table. gaussdb=# CREATE TABLE student (id INT,name VARCHAR(20),class INT); gaussdb=# INSERT INTO student VALUES (1, 'Scott', 1), (2, 'Ben', 1); gaussdb=# INSERT INTO student VALUES (3, 'Jack', 2),(4, 'Anna', 2),(5, 'Judy', 2); gaussdb=# INSERT INTO student VALUES (6, 'Sally', 3), (7, 'Jordan', 3); -- Use the aggregate function COUNT() to count the number of people in each class. gaussdb=# SELECT class, count(*) AS cnt FROM student GROUP BY class; class | cnt -------+----- 2 | 3 1 | 2 3 | 2 (3 rows) -- Use the HAVING clause to retrieve data of classes with less than three people. gaussdb=# SELECT class, COUNT(*) AS num FROM student GROUP BY class HAVING COUNT(*) < 3; class | num -------+----- 1 | 2 3 | 2 (2 rows) -- Delete. gaussdb=# DROP TABLE student;
Parent topic: SELECT
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot