JOIN Clause
Introduction
The JOIN clause is used to combine rows from two or more tables in a SELECT query, based on a related column between them. The rows from all the tables are generated together with the common column merged.
Before we learn more about the JOIN clause, let’s understand a few key terms related to joins:
- Primary Key: A primary key is a unique identifier of a record in a table. A table can have one column as its primary key column, and the value for each record in this column must be unique, is mandatory, and cannot be duplicated.
- Foreign Key: A foreign key column in one table uniquely identifies a row in another table or the same table. The foreign key of the second table refers to the primary key of the first table.
- Parent Table: In the JOIN clause, the parent table is the base table that the join is performed based on.
- Join Table: The join table is the secondary table or the child table which is being merged with the parent table in the JOIN clause.
In our example, the MovieID column is the primary key of the Movies table as it holds the unique identification number of a movie.
Example Database:
Let’s create a table named “Theaters” in the ticket booking application which specifies the locations of all the theaters that are registered with the application.
Sample records from the Theaters table are given below:
TheaterID | TheaterName | Location |
---|---|---|
047 | The Express Cinemas | New York City |
048 | ANC Cinemas | Rochester |
052 | Cosmos Theater | Albany |
053 | FunTime Cinemas | Buffalo |
The TheaterID column is the primary key of the Theaters table as it holds the unique identification numbers of the theaters. The TheaterID column in the Movies table is the foreign key to the TheaterID column in the Theaters table.
If you don’t specify the table name with the column name in a query, then by default it is considered to be a column in the base table.
Types of Joins
There are two types of Joins that can be performed in ZCQL.
INNER JOIN
The INNER JOIN returns the records that have matching values in both the parent table and the join table. When you use an inner join on two tables, only the records whose values match for the specified columns in both the tables are produced as the output.
The syntax for using an INNER JOIN is:
copySELECT column_name(s) FROM parent_table_name INNER JOIN join_table_name ON parent_table_name.column_name = join_table_name.column_name
Example:
To view a list of the movie names, show dates, and show times from the Movies table along with the theater names from the Theaters table, execute the following query:
copySELECT Movies.MovieName, Theatres.TheaterName, Movies.ShowDate, Movies.ShowTime, FROM Movies INNER JOIN Theatres ON Movies.TheaterID = Theatres.TheaterID
This will generate the following output:
MovieName | TheaterName | ShowDate | ShowTime |
---|---|---|---|
Ant-Man and the Wasp | The Express Cinemas | 2018-07-13 | 13:30:00 |
Hotel Transylvania 3: Summer Vacation | Cosmos Theater | 2018-07-13 | 14:20:00 |
The First Purge | Cosmos Theater | 2018-07-14 | 17:00:00 |
The First Purge | FunTime Cinemas | 2018-07-14 | 21:30:00 |
LEFT JOIN
The LEFT JOIN returns all the records from the parent table and the matched records from the join table. When you use a left join on two tables, all records from the parent table are returned, even if they do not have matches from the join table. The result is displayed as ‘NULL’ for records that do not have a match in the join table.
The syntax for using a LEFT JOIN is:
copySELECT column_name(s) FROM parent_table_name LEFT JOIN join_table_name ON parent_table_name.column_name = join_table_name.column_name
Example:
To view a list of theaters and their locations from the Theaters table along with the movies that are being screened in the theaters from the Movies table, execute the following query:
copySELECT Theaters.TheaterName, Theaters.Location, Movies.MovieName FROM Theaters LEFT JOIN Movies ON Theaters.TheaterID = Movies.TheaterID
This will generate the following output:
TheaterName | Location | MovieName |
---|---|---|
The Express Cinemas | New York City | The First Purge |
ANC Cinemas | Rochester | NULL |
Cosmos Theater | Albany | Hotel Transylvania 3: Summer Vacation |
FunTime Cinemas | Buffalo | Skyscraper |
Since there are no matching records for ANC Cinemas in the Movies table, the result is displayed as ‘NULL’.
Multiple Joins
You can combine a maximum of four joins in a single ZCQL query. However, you can only write one JOIN condition for each join clause. This will display the results from four different tables, where one is linked to another using an INNER JOIN or a LEFT JOIN.
Example Database:
Let’s create a table called ‘Pricing’ in the ticket booking application which displays the ticket price in dollars of a single ticket for a particular movie and theater. This table references TheaterID from the Theaters table and MovieID from the Movies table as the foreign keys.
Sample records from the Pricing table are given below:
TheaterID | MovieID | Price |
---|---|---|
047 | 2056 | 9.20 |
048 | NULL | NULL |
052 | 2057 | 8.64 |
052 | 2058 | 11.50 |
053 | 2059 | 7.44 |
You can execute a query to display the TheaterName from the Theaters table, MovieName, ShowDate, and ShowTime from the Movies table, and Price from the Pricing table, by executing the following query:
copySELECT Theaters.TheaterName,Movies.MovieName, Movies.ShowDate, Movies.ShowTime, Pricing.Price FROM Pricing LEFT JOIN Movies ON Theaters.TheaterID = Movies.TheaterID INNER JOIN Theaters ON Movies.MovieID = Pricing.MovieID
This will generate the following output:
TheaterName | MovieName | ShowDate | ShowTime | Price |
---|---|---|---|---|
The Express Cinemas | The First Purge | 2018-07-13 | 13:00:00 | 9.20 |
ANC Cinemas | NULL | NULL | NULL | NULL |
Cosmos Theater | Ant-Man and the Wasp | 2018-07-13 | 14:20:00 | 8.64 |
Cosmos Theater | Hotel Transylvania 3: Summer Vacation | 2018-07-14 | 17:00:00 | 11.50 |
FunTime Cinemas | Skyscraper | 2018-07-14 | 21:30:00 | 7.44 |
-
To utilize all the features of the JOIN clause, we recommend you migrate your codebase from ZCQL V1 to ZCQL V2.
-
From December 01st, 2024, all your current projects in all your Orgs present in the Development Environment will be automatically mapped to ZCQL V2 Parser.
-
From April 01st, 2025, all the projects present in all Orgs that have already been mapped to ZCQL V2 Parser in Development Environment will be automatically mapped to ZCQL V2 Parser in the Production Environment, if and when production is enabled for the project.
-
You can learn more on the enhancements and features of ZCQL V2 from this help document.
Last Updated 2024-09-03 16:31:06 +0530 +0530
Yes
No
Send your feedback to us