Prerequisites
To follow along with this blog, you should have a basic understanding of SQL syntax and database concepts.
The Scenario
Let's imagine we have two tables named "Customers" and "Orders." The "Customers" table contains information about customers, while the "Orders" table stores data about customer orders. Both tables have a common column named "customer_id," which we will use for joining the tables.
Customers Table :
Orders Table :
Different Types of SQL JOIN
Now, let's explore the different types of JOIN operations and see how they affect our data.
INNER JOIN: The INNER JOIN combines rows from both tables where the join condition is satisfied. It returns only the matching rows.
SELECT * FROM customers c INNER JOIN orders o ON c.customer_id = o.customer_id
Output:
LEFT JOIN: The left join returns all rows from the left table (the table specified before the JOIN keyword) and the matching rows from the right table. If no match is found, NULL values are returned for the columns of the right table.
SELECT * FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id
Output:
RIGHT JOIN: The right join returns all rows from the right table (the table specified after the JOIN keyword) and the matching rows from the left table. If no match is found, NULL values are returned for the columns of the left table.
SELECT * FROM customers c RIGHT JOIN orders o ON c.customer_id = o.customer_id
Output:
Full OUTER JOIN: The Full Outer Join, also known as Full Join, combines the result sets of both the Left Join and Right Join. It returns all rows from both tables and includes NULL values for non-matching rows. Full Outer Join is helpful when you need to retrieve all records from both tables, regardless of matches.
SELECT * FROM customers c FULL JOIN orders o ON c.customer_id = o.customer_id
Output:
The concept of Primary key and Foreign key.
Primary Key: A primary key is a column or group of columns that uniquely identifies each row in a table.
Foreign Key: A foreign key is a column or group of columns in one table that refers to the primary key in another table.
Primary keys and foreign keys are used to enforce data integrity in a relational database. Data integrity means that the data in the database is consistent and accurate.
For example, let's say in a table called customers
with the following columns:
customer_id
: The unique identifier for each customer.name
: The customer's name.email
: The customer's email address.
We could use the customer_id
column as the primary key for this table. This would ensure that each customer in the table has a unique identifier.
Now, ln another table called orders
with the following columns:
order_id
: The unique identifier for each order.customer_id
: The ID of the customer who placed the order.product
: The name of the product that was ordered.quantity
: The quantity of the product that was ordered.
We could use the customer_id
column in the orders
table as a foreign key to the customer_id
column in the customers
table. This would ensure that each order in the orders
table is associated with a valid customer in the customers
table.
In other words, the customer_id
column in the orders
table could only contain values that are also present in the customer_id
column in the customers
table.
Primary keys and foreign keys are an important part of data integrity in relational databases. They help to ensure that the data in the database is consistent and accurate.
Here are some additional benefits of using primary keys and foreign keys:
They can help to prevent data duplication.
They can help to enforce referential integrity.
They can help to improve the performance of queries.
Code for creating the Primary Key and Foreign Key
CREATE TABLE customers (
customer_id INT ,
name VARCHAR(255) ,
email VARCHAR(255) ,
PRIMARY KEY (customer_id)
);
CREATE TABLE orders (
order_id INT NOT NULL AUTO_INCREMENT,
customer_id INT NOT NULL,
product VARCHAR(255) NOT NULL,
quantity INT NOT NULL,
PRIMARY KEY (order_id),
FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
);
Primary key constraints
Primary key constraints ensure that each row in a table has a unique value in the primary key column or columns. This prevents duplicate data from being inserted into the table.
For example, a table of customers has a primary key column called customer_id
. This column would contain a unique identifier for each customer. If two customers tried to be inserted into the table with the same customer_id
value, the second insert would fail because it would violate the primary key constraint.
Foreign key constraints
Foreign key constraints ensure that the data in one table is related to the data in another table. This is done by linking the foreign key column in one table to the primary key column in another table.
For example, a table of orders has a foreign key column called customer_id
. This column would reference the customer_id
column in the table of customers. This would ensure that each order in the orders table is associated with a valid customer in the customer's table.
If a user tried to insert an order into the orders table with a customer_id
the value that does not exist in the customer's table, the insert would fail because it would violate the foreign key constraint.
orders
table. This is because customer_id values of 21 and 27, which are referenced from the customers
table, are not present. The insertion of order_id 9 and order_id 10 serves as an illustration of the concept of LEFT JOIN and RIGHT JOIN."Conclusion:
In this blog post, we explored the different types of SQL JOIN operations and how they affect data. We also discussed the concept of primary keys and foreign keys, and how they can be used to enforce data integrity in relational databases.
The code provided in this blog post can be used to create two tables, customers
and orders
, and to join them together using a variety of JOIN operations. The code also shows how to create a primary key and foreign key in each table, which can help to ensure that the data in the database is consistent and accurate.
Here are some of the key takeaways from this blog post:
There are four main types of SQL JOIN operations: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
The INNER JOIN returns only the matching rows from both tables.
The LEFT JOIN returns all rows from the left table and the matching rows from the right table.
The RIGHT JOIN returns all rows from the right table and the matching rows from the left table.
The FULL OUTER JOIN returns all rows from both tables, including NULL values for non-matching rows.
Primary keys and foreign keys can be used to enforce data integrity in relational databases.
Primary keys uniquely identify each row in a table.
Foreign keys reference the primary key of another table.
I hope this summary was helpful. Please let me know if you have any other questions.
Make sure to check up with the next blog post where we will talk about how SQL JOINS work with duplicate values and NULL values.
please find the GitHub reposit link given below for table creation and queries.