How to Join Unrelated SQL Tables

Paopatsin/ August 22, 2020/ Database, MS SQL, Technology

Please read our Disclaimer

Works With
-Microsoft SQL Server 2016 (RTM) – 13.0.1601.5 (X64)

Command:

SELECT d.*, p.* FROM [AdventureWorks2016].[HumanResources].[Department] d JOIN [AdventureWorks2016].[Person].[Person] p ON 1 = 1

Steps and Sample Usage:

  1. Open and log into your SQL Management Studio
  2. Open a New Query Windows or (CTRL+N)
  3. Run the following commands: (Copy and Paste the code block to your Query Window)

SELECT d.DepartmentID, d.Name, p.rowguid, p.FirstName, p.LastName
FROM [AdventureWorks2016].[HumanResources].[Department] d
JOIN [AdventureWorks2016].[Person].[Person] p
ON 1 = 1
WHERE p.rowguid = ‘0AEB5713-8BB0-4B8D-BF81-777158E5FF09’ –filter

  1. Execute the query by pressing F5.

Use Case:

  1. You want to export data into a single output.

Caveat:

  1. The output can be exponentially big and may result to a bottleneck, you must execute with caution.

Sample Data:
-AdventureWorks2016 Database

Share this Post