-- Migrated from DuckDB test: test/sql/join/inner/ advanced tests -- Tests advanced inner join patterns CREATE TABLE customers(cust_id INTEGER, cust_name VARCHAR, city VARCHAR, ts TIMESTAMP TIME INDEX); Affected Rows: 0 CREATE TABLE orders(order_id INTEGER, cust_id INTEGER, order_date DATE, amount DOUBLE, ts TIMESTAMP TIME INDEX); Affected Rows: 0 CREATE TABLE order_items(item_id INTEGER, order_id INTEGER, product VARCHAR, quantity INTEGER, price DOUBLE, ts TIMESTAMP TIME INDEX); Affected Rows: 0 INSERT INTO customers VALUES (1, 'John', 'NYC', 1000), (2, 'Jane', 'LA', 2000), (3, 'Bob', 'Chicago', 3000), (4, 'Alice', 'NYC', 4000); Affected Rows: 4 INSERT INTO orders VALUES (101, 1, '2023-01-01', 250.00, 1000), (102, 2, '2023-01-02', 180.00, 2000), (103, 1, '2023-01-03', 420.00, 3000), (104, 3, '2023-01-04', 95.00, 4000), (105, 4, '2023-01-05', 310.00, 5000); Affected Rows: 5 INSERT INTO order_items VALUES (1, 101, 'Widget', 2, 125.00, 1000), (2, 101, 'Gadget', 1, 0.00, 2000), (3, 102, 'Tool', 3, 60.00, 3000), (4, 103, 'Device', 1, 420.00, 4000), (5, 104, 'Part', 5, 19.00, 5000), (6, 105, 'Component', 2, 155.00, 6000); Affected Rows: 6 -- Multi-table inner join SELECT c.cust_name, c.city, o.order_id, o.order_date, o.amount FROM customers c INNER JOIN orders o ON c.cust_id = o.cust_id ORDER BY o.order_date, c.cust_name; +-----------+---------+----------+------------+--------+ | cust_name | city | order_id | order_date | amount | +-----------+---------+----------+------------+--------+ | John | NYC | 101 | 2023-01-01 | 250.0 | | Jane | LA | 102 | 2023-01-02 | 180.0 | | John | NYC | 103 | 2023-01-03 | 420.0 | | Bob | Chicago | 104 | 2023-01-04 | 95.0 | | Alice | NYC | 105 | 2023-01-05 | 310.0 | +-----------+---------+----------+------------+--------+ -- Three-way inner join SELECT c.cust_name, o.order_id, oi.product, oi.quantity, oi.price FROM customers c INNER JOIN orders o ON c.cust_id = o.cust_id INNER JOIN order_items oi ON o.order_id = oi.order_id ORDER BY c.cust_name, o.order_id, oi.product; +-----------+----------+-----------+----------+-------+ | cust_name | order_id | product | quantity | price | +-----------+----------+-----------+----------+-------+ | Alice | 105 | Component | 2 | 155.0 | | Bob | 104 | Part | 5 | 19.0 | | Jane | 102 | Tool | 3 | 60.0 | | John | 101 | Gadget | 1 | 0.0 | | John | 101 | Widget | 2 | 125.0 | | John | 103 | Device | 1 | 420.0 | +-----------+----------+-----------+----------+-------+ -- Inner join with complex conditions SELECT c.cust_name, o.order_id, o.amount FROM customers c INNER JOIN orders o ON c.cust_id = o.cust_id AND o.amount > 200.00 ORDER BY o.amount DESC; +-----------+----------+--------+ | cust_name | order_id | amount | +-----------+----------+--------+ | John | 103 | 420.0 | | Alice | 105 | 310.0 | | John | 101 | 250.0 | +-----------+----------+--------+ -- Inner join with aggregation SELECT c.city, COUNT(o.order_id) as total_orders, SUM(o.amount) as total_amount, AVG(o.amount) as avg_order_amount FROM customers c INNER JOIN orders o ON c.cust_id = o.cust_id GROUP BY c.city ORDER BY total_amount DESC; +---------+--------------+--------------+-------------------+ | city | total_orders | total_amount | avg_order_amount | +---------+--------------+--------------+-------------------+ | NYC | 3 | 980.0 | 326.6666666666667 | | LA | 1 | 180.0 | 180.0 | | Chicago | 1 | 95.0 | 95.0 | +---------+--------------+--------------+-------------------+ -- Self join SELECT o1.order_id as order1, o2.order_id as order2, o1.amount, o2.amount FROM orders o1 INNER JOIN orders o2 ON o1.cust_id = o2.cust_id AND o1.order_id < o2.order_id ORDER BY o1.order_id, o2.order_id; +--------+--------+--------+--------+ | order1 | order2 | amount | amount | +--------+--------+--------+--------+ | 101 | 103 | 250.0 | 420.0 | +--------+--------+--------+--------+ -- Join with subquery SELECT c.cust_name, high_orders.total_amount FROM customers c INNER JOIN ( SELECT cust_id, SUM(amount) as total_amount FROM orders GROUP BY cust_id HAVING SUM(amount) > 300 ) high_orders ON c.cust_id = high_orders.cust_id ORDER BY high_orders.total_amount DESC; +-----------+--------------+ | cust_name | total_amount | +-----------+--------------+ | John | 670.0 | | Alice | 310.0 | +-----------+--------------+ -- Join with window functions SELECT c.cust_name, o.order_id, o.amount, ROW_NUMBER() OVER (PARTITION BY c.cust_id ORDER BY o.order_date) as order_sequence FROM customers c INNER JOIN orders o ON c.cust_id = o.cust_id ORDER BY c.cust_name, order_sequence; +-----------+----------+--------+----------------+ | cust_name | order_id | amount | order_sequence | +-----------+----------+--------+----------------+ | Alice | 105 | 310.0 | 1 | | Bob | 104 | 95.0 | 1 | | Jane | 102 | 180.0 | 1 | | John | 101 | 250.0 | 1 | | John | 103 | 420.0 | 2 | +-----------+----------+--------+----------------+ DROP TABLE customers; Affected Rows: 0 DROP TABLE orders; Affected Rows: 0 DROP TABLE order_items; Affected Rows: 0