Back to SQL Modules
Module 01

SQL Joins & Query Aggregates

Master relational data querying across multiple tables. Includes syntax, visual logic, grouping, and high-frequency placement interview questions.

Types of SQL Joins

1. INNER JOIN

Matching Rows Only

Returns records that have matching values in both tables. Unmatched rows from either table are completely excluded.

-- Syntax
SELECT e.employee_id, e.name, d.department_name
FROM Employees e
INNER JOIN Departments d
  ON e.department_id = d.department_id;

2. LEFT (OUTER) JOIN

All Left + Matching Right

Returns all records from the left table, and the matched records from the right table. If no match exists, NULLs are returned for right table columns.

-- Syntax
SELECT c.customer_id, c.name, o.order_id
FROM Customers c
LEFT JOIN Orders o
  ON c.customer_id = o.customer_id;

3. RIGHT (OUTER) JOIN

All Right + Matching Left

Returns all records from the right table, and the matched records from the left table. Populates NULLs where no matching record is found on the left.

-- Syntax
SELECT e.name, d.department_name
FROM Employees e
RIGHT JOIN Departments d
  ON e.department_id = d.department_id;

4. FULL OUTER JOIN

All Records From Both

Returns all records when there is a match in left OR right table. It combines the result set of both LEFT and RIGHT joins.

-- Syntax
SELECT e.name, d.department_name
FROM Employees e
FULL OUTER JOIN Departments d
  ON e.department_id = d.department_id;

Combining Joins with GROUP BY & HAVING

In placement assessments, interviewers frequently ask you to join two tables and perform aggregations using COUNT(), SUM(), or AVG() filtered by a HAVING clause.

-- Example: Find departments with more than 5 employees and their total salary spend
SELECT 
    d.department_name, 
    COUNT(e.employee_id) AS total_employees,
    SUM(e.salary) AS total_budget
FROM Departments d
JOIN Employees e ON d.department_id = e.department_id
GROUP BY d.department_name
HAVING COUNT(e.employee_id) > 5;

Top LeetCode SQL Practice Problems

Direct LeetCode Links