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:
pythonquery = "SELECT COUNT(*) FROM users"
The result of the executed code:
python{'COUNT(*)': 6}
Example
Now let's count all those whose salary is equal to 900:
pythonquery = "SELECT COUNT(*) FROM users WHERE salary=900"
The result of the executed code:
python{'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:
pythonquery = "SELECT COUNT(salary) FROM users"
The result of the executed code:
python{'COUNT(salary)': 6}
Practical tasks
Задание 1
Count all users whose age is less than 30.
Задание 2
Count all users with a salary equal to 500 and an age greater than 23.