Grouping Result in Python
To group the result obtained from the database, you should use the command GROUP
BY
.
Let's select the names and ages of users and group them by age:
query = "SELECT name, age FROM users GROUP BY age"
The result of the executed code:
{'name': 'user1', 'age': 23}
{'name': 'user2', 'age': 25}
{'name': 'user5', 'age': 27}
{'name': 'user6', 'age': 28}
{'name': 'user4', 'age': 30}
Output the values of id
and salary
users, grouping the result by salary size.