Query to seed many to many table How can I write a query to seed a many to many table. Let's say I have an Employee table, a Branch table, and I am introducing an EmployeeBranch table for the many to many relationship.

I need to insert records for every employee and branch combination into the EmployeeBranch table. So if there are 100 employees and 10 branches, I would end up with 1000 records; each employee will have 10 records for each branch.
If you're using SQL Server, You can use CROSS APPLY to return 1 Branch row for each Employee row:

SELECT e.ID AS EmployeeID,
  e.Name AS EmployeeName,
  b.BranchID,
  b.BranchName
FROM Employee e
CROSS APPLY (SELECT ID AS BranchID, BranchName FROM Branch) b
ORDER BY EmployeeID


See working db&lt;>fiddle.

For Postgres, similar query but using LEFT JOIN LATERAL instead:

SELECT e.ID AS EmployeeID,
  e.Name AS EmployeeName,
  b.ID AS BranchID,
  b.BranchName
FROM Employee e
LEFT JOIN LATERAL (SELECT ID, BranchName FROM Branch) b ON true
ORDER BY EmployeeID


Working db&lt;>fiddle here.