A query usually refers to a SELECT statement, but may also refer to data modifications as well -- UPDATE, INSERT, DELETE, and MERGE are common DML ("data modification language") statements. Personally, I would call these four operations DML statements and reserve query for SELECT statements; I find this a useful distinction.
Databases implement a set of properties called ACID properties. These basically say that any SQL statement sees consistent data, regardless of what else is going on in the database. A simple way to think of about these is that all operations are serialized -- one statement completes before another begins, even in a multi-user environment. Serialization guarantees that operations are isolated from each other. In practice, serialization is very expensive and databases have other mechanisms to ensure integrity, but it is a useful abstraction when learning about the concepts.
What are transactions? Transactions are the mechanism that databases use to ensure the integrity of the data when data is being modified. Transactions often consist of one statement that modifies the data. But that is not necessary. In fact, you can have a complex set of data transformations within a single transaction.
The three key operations on a transactions are:
- BEGIN TRANSACTION: Tell the database that a transaction is beginning. All changes within the transaction are invisible to other users while the transaction is "active".
- COMMIT TRANSACTION: Make all the changes visible in the database. Conceptually, this happens instantaneously. The database guarantees that other users will not see partial changes to the data (i.e. enforces data integrity).
- ROLLBACK TRANSACTION: Undo all the work. No other users ever see the changes.
Note: Many databases have options to weaken the data integrity. These can be useful for performance reasons for users who know what they are doing.
I should note that if all operations on a database are SELECT statements, then transactions are not necessary. The data is not changing, so the view of the data is consistent. So transactions are generally associated with DML statements.