Joining Tables for Richer Queries in SQL

by | SQL

Setting Up Your Environment

PostgresQL

1. Install PostgreSQL

Download and install PostgreSQL from the official website: https://www.postgresql.org/download/

2. Start PostgreSQL Service

# On Debian-based systems
sudo service postgresql start

# On RedHat-based systems
sudo systemctl start postgresql

3. Access PostgreSQL

sudo -i -u postgres
psql

SQLite

1. Install SQLite

Download and install SQLite from the official website: https://www.sqlite.org/download.html

2. Verify SQLite Installation

sqlite3 --version

Set Up Your Databases

1. PostgreSQL Database Creation

-- Switch to the required user
sudo -i -u postgres

-- Open PostgreSQL prompt
psql

-- Create your database
CREATE DATABASE mydatabase;

-- Connect to the database
c mydatabase;

2. SQLite Database Creation

# Create a new SQLite database
sqlite3 mydatabase.db

# Create a sample table
CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    username TEXT NOT NULL,
    email TEXT NOT NULL
);

# Insert sample data
INSERT INTO users (username, email) VALUES ('user1', 'user1@example.com');
INSERT INTO users (username, email) VALUES ('user2', 'user2@example.com');

3. PostgreSQL Table Creation and Data Insertion

-- Create a sample table
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(50) NOT NULL
);

-- Insert sample data
INSERT INTO users (username, email) VALUES ('user1', 'user1@example.com');
INSERT INTO users (username, email) VALUES ('user2', 'user2@example.com');

Verify Database Connections

1. PostgreSQL Verification

-- List databases
l

-- List tables
dt

-- Query sample data
SELECT * FROM users;

2. SQLite Verification

# List tables
.tables

# Query sample data
SELECT * FROM users;

Environment is now set up and ready for joining tables in upcoming units.

Practical Implementation of Different Types of Joins

Using PostgreSQL and SQLite

1. Inner Join

-- Select common data from Orders and Customers where there is a match in CustomerID
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

2. Left Join (or Left Outer Join)

-- Select all data from Orders and matching data from Customers
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
LEFT JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

3. Right Join (or Right Outer Join) – PostgreSQL Only

-- Select all data from Customers and matching data from Orders
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
RIGHT JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

4. Full Join (or Full Outer Join) – PostgreSQL Only

-- Select all data from Orders and Customers, showing NULL in non-matching rows
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
FULL OUTER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

5. Cross Join

-- Select a Cartesian product of Orders and Customers (every row of Orders combined with every row of Customers)
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
CROSS JOIN Customers;

6. Self Join

-- Example: Select related rows within the same table Customers based on ManagerID
SELECT C1.CustomerName AS Employee, C2.CustomerName AS Manager
FROM Customers C1, Customers C2
WHERE C1.ManagerID = C2.CustomerID;

7. Natural Join – PostgreSQL Only

-- Automatically join using columns with the same name in both tables
SELECT *
FROM Orders
NATURAL JOIN Customers;

Conclusion

These practical SQL queries using PostgreSQL and SQLite showcase different join operations that enhance database interactions. Each query is self-contained and ready to be executed in a real-life project where these types of operations are necessary.

Practical Implementation of Inner Joins in PostgreSQL and SQLite

PostgreSQL Implementation

-- PostgreSQL Inner Join Example

-- Select columns from TableA and TableB
SELECT 
    a.column1, 
    a.column2, 
    b.column3, 
    b.column4
FROM 
    TableA a
INNER JOIN 
    TableB b
ON 
    a.common_column = b.common_column;

SQLite Implementation

-- SQLite Inner Join Example

-- Select columns from TableX and TableY
SELECT 
    x.columnA, 
    x.columnB, 
    y.columnC, 
    y.columnD
FROM 
    TableX x
INNER JOIN 
    TableY y
ON 
    x.common_attribute = y.common_attribute;

Practical Example:

Assume we have two tables: students and enrollments.

  • students table: student_id, student_name
  • enrollments table: student_id, course_id

PostgreSQL Example:

-- PostgreSQL Example Join

SELECT 
    s.student_id, 
    s.student_name, 
    e.course_id
FROM 
    students s
INNER JOIN 
    enrollments e
ON 
    s.student_id = e.student_id;

SQLite Example:

-- SQLite Example Join

SELECT 
    s.student_id, 
    s.student_name, 
    e.course_id
FROM 
    students s
INNER JOIN 
    enrollments e
ON 
    s.student_id = e.student_id;

These queries will retrieve the students’ IDs, names, and their enrolled course IDs by linking records from both tables using the student_id column. This is a practical application for establishing relationships between tables in your database using inner joins.

Implementing Outer Joins

Using PostgreSQL

-- Creating tables
CREATE TABLE employees (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100)
);

CREATE TABLE departments (
    id SERIAL PRIMARY KEY,
    employee_id INT,
    department_name VARCHAR(100)
);

-- Inserting data
INSERT INTO employees (name) VALUES ('Alice'), ('Bob'), ('Charlie');
INSERT INTO departments (employee_id, department_name) VALUES (1, 'HR'), (2, 'Finance');

-- LEFT OUTER JOIN: retrieve all employees, including those without a department
SELECT e.id, e.name, d.department_name
FROM employees e
LEFT JOIN departments d
ON e.id = d.employee_id;

-- RIGHT OUTER JOIN: retrieve all departments, including those without employees
SELECT e.id, e.name, d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.id = d.employee_id;

-- FULL OUTER JOIN: retrieve all employees and departments, including unmatched ones
SELECT e.id, e.name, d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.id = d.employee_id;

Using SQLite

-- Creating tables
CREATE TABLE employees (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT
);

CREATE TABLE departments (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    employee_id INT,
    department_name TEXT
);

-- Inserting data
INSERT INTO employees (name) VALUES ('Alice'), ('Bob'), ('Charlie');
INSERT INTO departments (employee_id, department_name) VALUES (1, 'HR'), (2, 'Finance');

-- LEFT OUTER JOIN: SQLite does not support RIGHT JOIN or FULL OUTER JOIN directly
SELECT e.id, e.name, d.department_name
FROM employees e
LEFT JOIN departments d
ON e.id = d.employee_id;

-- Implementing FULL OUTER JOIN using UNION
SELECT e.id, e.name, d.department_name
FROM employees e
LEFT JOIN departments d
ON e.id = d.employee_id
UNION
SELECT e.id, e.name, d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.id = d.employee_id;

Ensure your tables and data are created properly before executing these queries to observe the outer joins’ results.

Joining More than Two Tables

When working with multiple tables in PostgreSQL or SQLite, you often need to perform joins that combine data from more than two tables. Below is a practical implementation example using SQL:

PostgreSQL and SQLite

-- Assuming we have three tables: employees, departments, and projects

-- employees table
-- +-------------+-----------+--------+
-- | employee_id | name      | dept_id|
-- +-------------+-----------+--------+
-- | 1           | Alice     | 101    |
-- | 2           | Bob       | 102    |
-- +-------------+-----------+--------+

-- departments table
-- +--------+-------------+
-- | dept_id| dept_name   |
-- +--------+-------------+
-- | 101    | HR          |
-- | 102    | Engineering |
-- +--------+-------------+

-- projects table
-- +-------------+-------------+-----------+
-- | project_id  | project_name| employee_id|
-- +-------------+-------------+-----------+
-- | 201         | Project A   | 1         |
-- | 202         | Project B   | 2         |
-- +-------------+-------------+-----------+

-- SQL query to join all three tables

SELECT employees.name AS employee_name,
       departments.dept_name AS department_name,
       projects.project_name AS project_name
FROM employees
JOIN departments ON employees.dept_id = departments.dept_id
JOIN projects ON employees.employee_id = projects.employee_id;

Explanation:

  • The query selects columns from all three tables.
  • It joins employees with departments on the dept_id.
  • Then it joins the result with projects on the employee_id.

This query will fetch a result set that links employees to their respective departments and projects.


This implementation can be directly applied to your PostgreSQL or SQLite database environment.

Related Posts