Selection fields in SQL query in Python
In the previous lessons, when selecting from the DB, all fields (columns) of the table were included in the result. This is not actually necessary - you can specify which specific fields we need.
To do this, instead of *
after the command SELECT
, the names of the required fields are listed separated by commas.
Let's look at an example. Let's extract only the employee's name and age from our users
table:
query = "SELECT name, age FROM users WHERE id >= 3"
Select from the users
table the name, age, and salary for each employee.
Select id
, name
and age
users whose salary is greater than 400
but less than 900
.