Counting Quantity via SQL Query in Python
The COUNT command can be used to count the number of rows in a selection. The asterisk symbol specified in the command parameter means that all records from the table are selected.
Example
Let's, for example, count all users in a table:
query = "SELECT COUNT(*) FROM users"
The result of the executed code:
{'COUNT(*)': 6}
Example
Now let's count all those whose salary is equal to 900:
query = "SELECT COUNT(*) FROM users WHERE salary=900"
The result of the executed code:
{'COUNT(*)': 2}
Example
You can also specify a field name in the COUNT command parameter. And the command will count all records that are not equal to NULL:
query = "SELECT COUNT(salary) FROM users"
The result of the executed code:
{'COUNT(salary)': 6}
Practical tasks
Count all users whose age is less than 30.
Count all users with a salary equal to 500 and an age greater than 23.