Yes, you can! The longer answer is yes, there are a few ways to combine two tables without a common column, including CROSS JOIN (Cartesian product) and UNION. The latter is technically not a join but can be handy for merging tables in SQL. In this article, I’ll guide you through the different solutions with examples. Show
When Do you Need to Join Tables Without a Common Column?Beginners just getting started with SQL are usually introduced to standard equi joins, in which two tables are combined by a common column. However, the real-world tasks of a data analyst are usually more diverse and complex. For example, there are many use cases for a non-equi join, when two tables are combined using conditional operators other than the equal sign. If you are not familiar with this type of JOIN, check out our interactive SQL JOINs course. It offers over 90 hands-on exercises on different kinds of JOINs. In other cases, you may want to have every combination of the rows from the two tables. As an example, if you run a restaurant, you might want to see all possible combinations of wines from your wine card and main courses from your menu. Or, imagine that the information about your suppliers are stored in different tables. They may even have different column names by product type (i.e., wines, juices, fruits, etc.). You might want to combine all the information to generate a single table with all suppliers. In these cases, you would want to combine two or more tables without requiring a common column. It sounds like it should be a pretty straightforward SQL assignment. Well, of course, it is! I’ll suggest a couple of solutions in this guide. But first, let’s explore the data we’ll use for our examples. Getting to Know the DataWe run a restaurant and have relevant data stored in multiple tables, among which are The
The
Let’s see how we can combine these tables to get the results we want. How to Combine two Tables Without a Common ColumnIn our first example, we want to see all possible combinations of wines and main courses from our menu. There are at least two approaches to combining the Using the “FROM Table1, Table2” SyntaxOne way to join two tables without a common column
is to use an obsolete syntax for joining tables. With this syntax, we simply list the tables that we want to join in the If what we want is every combination of rows from two tables, there is no need to include any joining conditions. We can use a query like this: SELECT w.name AS wine, m.name AS main_course FROM wine w, main_course m; The query will return a Cartesian product (a cross join), whose result set has the total number of rows equal to the number of rows in the first table multiplied by the number of rows in the second table.
We got the result we wanted. This is a workable solution for joining two tables when the objective is to get a result set with all possible combinations of the rows. However, this is not the best approach. SQL practitioners prefer to use the explicit Read more about the benefits of the new syntax for joining tables in our article What's the Difference Between JOIN and Multiple Tables in FROM? Using the CROSS JOIN OperatorAs you may have already guessed, the second approach to getting all possible
combinations of the rows from two tables is by using the SELECT w.name AS wine, m.name AS main_course FROM wine w CROSS JOIN main_course m; This query outputs the exact same result set as shown earlier. However, this syntax is preferable as it clearly states the intent to perform a Learn more about cross joins in our Illustrated guide to SQL CROSS JOIN. Using UNION or UNION ALLThere are other use cases for combining two tables without a common column. As in the example mentioned earlier, you may want to consolidate all the supplier information stored in multiple tables. You don’t want a Cartesian product in this case. So, how do you combine the tables? In this case, you use a Simply put, So, if we want to generate a combined list of the
supplier IDs from the tables SELECT w.supplier_id FROM wine w UNION SELECT m.major_supplier_id FROM main_course m ORDER BY supplier_id; Here, we first select the supplier IDs from the
Note the following when using
The result of this query will include the
If neither Time to Practice CROSS JOINs and UNIONs!You now know a lot about combining tables without a common column. You’ll be prepared for the question on how to combine two tables without a common column. This is a very common question in SQL job interviews! If you want to learn more about However, you probably already know that the shortest path to becoming an SQL expert is through lots of practice writing SQL queries. LearnSQL.com offers a comprehensive course on SQL JOINs with 93 interactive
exercises. In this course, you’ll have the opportunity to practice all kinds of basic Happy learning! How can I get data from two tables without joining?You could try something like this: SELECT ... FROM ( SELECT f1,f2,f3 FROM table1 UNION SELECT f1,f2,f3 FROM table2 ) WHERE ...
Can we write join without on?Omit the ON clause from the JOIN statement
In MySQL, it's possible to have a JOIN statement without ON as ON is an optional clause.
How can I join two tables in MySQL?Ans: Joining two tables in SQL can be done in four major ways: Inner Join (returns rows with matching columns), Left Join (ALL records in the left table and matching records in the right table), Right Join (ALL records in the right table and matching records in the left table), and Union (removes duplicates).
Can two tables be join without any relation?The answer to this question is yes, you can join two unrelated tables in SQL, and in fact, there are multiple ways to do this, particularly in the Microsoft SQL Server database. The most common way to join two unrelated tables is by using CROSS join, which produces a cartesian product of two tables.
How do you join two tables without common key fields between them?3 Answers. We can use the Cartesian product, union, and cross-product to join two tables without a common column.. Cartesian product means it matches all the rows of table A with all the rows of table B. ... . Union returns the combination of result sets of all the SELECT statements.. |