Q:

Query examples in a many-to-many relationship

Wow, it's hard to find a simple explanation to this topic. A simple many-to-many relationship.
Three tables, tableA, tableB and a junction tableA_B. 
I know how to set up the relationship, with keys and all, but I get a little confused when time comes to perform INSERT, UPDATE and DELETE queries....
Basically, what I am looking for is an example that shows:

How to get all records in TableA, based on an ID in TableB
How to get all records in TableB, based on an ID in TableA

3 How to INSERT in either TableA or TableB, and then make the appropriate INSERT in the junction table to make the connection..
I'm not looking for a solution to a specific project, just a few general examples that can be applied. Maybe you have something lying around?

A:

The first thing I would do is recommend using an ORM like Linq-To-Sql or NHibernate which will give you object representations of your data-model which make it much simpler to handle complex things like many-to-many CRUD operations.
If an ORM isn't part of your tool set then here is how this would look in SOL. 

Users       UserAddresses     Addresses
=======     =============     =========
Id          Id                Id
FirstName   UserId            City
LastName    AddressId         State
                              Zip

Our tables are joined like this:

   Users.Id -> UserAddresses.UserId
   Addresses.Id -> UserAddresses.AddressId

All records in Users based on Addresses.Id

SELECT        Users.*
FROM            Addresses INNER JOIN
                         UserAddresses ON Addresses.Id = UserAddresses.AddressId INNER JOIN
                         Users ON UserAddresses.UserId = Users.Id
WHERE        (Addresses.Id = @AddressId)

All records in Addresses based on Users.Id

SELECT        Addresses.*
FROM            Addresses INNER JOIN
                         UserAddresses ON Addresses.Id = UserAddresses.AddressId INNER JOIN
                         Users ON UserAddresses.UserId = Users.Id
WHERE        (Users.Id = @UserId)

