Using SQL JOINs Statements
March 21, 2020
CROSS JOIN
- Returns a Cartesian product of two or more tables
- Does not have an
ON
operator - Does not have a
WHERE
clause (However, if used it behaves like anINNER JOIN
)
The syntax for the CROSS JOIN
:
SELECT <select list>
FROM <first_table>
CROSS JOIN <second_table>
Let’s apply it to our example.
METHOD 1: Non-Aliasing Tables
SELECT dbo.Films.*, dbo.BoxOffice.Gross FROM dbo.Films CROSS JOIN dbo.BoxOffice
METHOD 2: Aliasing Tables
SELECT film.*, bo.Gross FROM dbo.Films film CROSS JOIN dbo.BoxOffice bo
RESULT: A Cartesian Table with 16 Rows (4 x 4) similar to below.
TABLE: Combining FirstName and LastName columns into a single column “Star”
FilmID | FilmName | YearMade | Gross |
1 | My Fair Lady | 1964 | 72000000 |
2 | The Terminator | 1984 | 72000000 |
3 | Superman III | 1983 | 72000000 |
4 | Forest Gump | 1994 | 72000000 |
2 | My Fair Lady | 1964 | 38371200 |
2 | The Terminator | 1984 | 38371200 |
2 | Superman III | 1983 | 38371200 |
2 | Forest Gump | 1994 | 38371200 |
3 | My Fair Lady | 1964 | 59950623 |
3 | The Terminator | 1984 | 59950623 |
3 | Superman III | 1983 | 59950623 |
3 | Forest Gump | 1994 | 59950623 |
4 | My Fair Lady | 1964 | 329694499 |
4 | The Terminator | 1984 | 329694499 |
4 | Superman III | 1983 | 329694499 |
4 | Forest Gump | 1994 | 329694499 |
If you switch tables in CROSS JOIN
:
SELECT film.*, bo.Gross FROM dbo.BoxOffice bo CROSS JOIN dbo.Films film
TABLE: Combining FirstName and LastName columns into a single column “Star”
FilmID | FilmName | YearMade | Gross |
1 | My Fair Lady | 1964 | 72000000 |
1 | My Fair Lady | 1964 | 38371200 |
1 | My Fair Lady | 1964 | 59950623 |
1 | My Fair Lady | 1964 | 329694499 |
2 | The Terminator | 1984 | 72000000 |
2 | The Terminator | 1984 | 38371200 |
2 | The Terminator | 1984 | 59950623 |
2 | The Terminator | 1984 | 329694499 |
3 | Superman III | 1983 | 72000000 |
3 | Superman III | 1983 | 38371200 |
3 | Superman III | 1983 | 59950623 |
3 | Superman III | 1983 | 329694499 |
4 | Forest Gump | 1994 | 72000000 |
4 | Forest Gump | 1994 | 38371200 |
4 | Forest Gump | 1994 | 59950623 |
4 | Forest Gump | 1994 | 329694499 |
If a WHERE
clause is added, the CROSS JOIN
behaves as an INNER JOIN
. For example, the following Transact-SQL queries produce the same result set.
USE Movies GO SELECT * FROM dbo.Films CROSS JOIN dbo.Actors WHERE dbo.Films.FilmID = dbo.Actors.FilmID SELECT * FROM dbo.Films INNER JOIN dbo.Actors ON dbo.Films.FilmID = dbo.Actors.FilmID
UNION
UNION
is a special operator you can use to cause two or more queries to generate one result set.
When dealing with queries that use a UNION
, there are just a few key points:
- All the
UNION
ed queries must have the same number of columns in theSELECT
list. If your first query has three columns in theSELECT
list, the second (and any subsequent queries beingUNION
ed) must also have three columns. If the first has five, the second must have five, too. Regardless of how many columns are in the first query, there must be the same number in the subsequent query(s). - The headings returned for the combined result set will be taken only from the first of the queries. If your first query has a
SELECT
list that looks likeSELECT Col1, Col2 AS Second, Col3 FROM...
, regardless of how your columns are named or aliased in the subsequent queries, the headings on the columns returned from theUNION
will be Col1, Second, and Col3 respectively. - The data types of each column in a query must be implicitly compatible with the data type in the same relative column in the other queries. Note that I’m not saying they have to be the same data type — they just have to be implicitly convertible. If the second column in the first query were of type
char(20)
, it would be fine if the second column in the second query werevarchar(50)
. However, because things are based on the first query, any rows longer than 20 would be truncated for data from the second result set. - Unlike non-
UNION
queries, the default return option forUNION
isDISTINCT
rather thanALL
. This can really be confusing to people. In your other queries, all rows were returned regardless of whether they were duplicated with another row, but the results of aUNION
do not work that way. Unless you use theALL
keyword in your query, only one of any repeating rows will be returned.
SELECT dbo.Films.FilmID, dbo.Films.FilmName, dbo.Films.YearMade FROM dbo.Films UNION SELECT dbo.BoxOffice.FilmID, dbo.BoxOffice.FilmName, dbo.BoxOffice.Gross FROM dbo.BoxOffice
RESULT:
TABLE: Combining FirstName and LastName columns into a single column “Star”
FilmID | FilmName | YearMade |
1 | My Fair Lady | 1964 |
1 | My Fair Lady | 72000000 |
2 | The Terminator | 1984 |
2 | The Terminator | 329694499 |
3 | Superman III | 1983 |
3 | Superman III | 59950623 |
4 | Forest Gump | 1994 |
4 | Forest Gump | 329694499 |