Advanced SQL Queries & Joins
Master complex SQL queries, including various types of joins, subqueries, and window functions to retrieve sophisticated datasets.
Beyond Basic Queries
Welcome to Advanced SQL Queries! So far, you've learned to select, filter, and sort data. But real-world applications often need to combine data from multiple sources or perform complex calculations.
This lesson will equip you with powerful techniques to retrieve sophisticated datasets, making your Supabase applications even smarter.
Joins Recap: Inner Join
Let's quickly refresh our memory on joins. A JOIN combines rows from two or more tables based on a related column between them.
An INNER JOIN returns only the rows where there is a match in both tables. If a row in one table doesn't have a match in the other, it's excluded.
Try running this example to set up our tables and see an INNER JOIN:
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(50) NOT NULL
);
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
department_id INT REFERENCES departments(department_id)
);
INSERT INTO departments (department_id, department_name) VALUES
(1, 'Sales'),
(2, 'Marketing'),
(3, 'Engineering'),
(4, 'HR');
INSERT INTO employees (employee_id, first_name, last_name, department_id) VALUES
(101, 'Alice', 'Smith', 1),
(102, 'Bob', 'Johnson', 2),
(103, 'Charlie', 'Brown', 1),
(104, 'Diana', 'Prince', 3),
(105, 'Eve', 'Adams', NULL);
SELECT
e.first_name,
e.last_name,
d.department_name
FROM
employees e
INNER JOIN
departments d ON e.department_id = d.department_id;All lessons in this course
- Advanced SQL Queries & Joins
- Database Indexing for Performance
- Database Functions and Triggers