Retrieve a list of customers that have purchased MariaDB-related products by joining Customers, Orders, OrderItems, and Products.
Teams often migrate from MariaDB or sell MariaDB-based services. Knowing which customers interact with MariaDB products helps target migrations, upsells, or support.
Join Customers → Orders → OrderItems → Products and filter on product names that contain ‘MariaDB’. Use DISTINCT to avoid duplicates.
SELECT id FROM Products WHERE name ILIKE '%mariadb%';
SELECT DISTINCT c.* FROM Customers c
JOIN Orders o ON o.customer_id = c.id
JOIN OrderItems oi ON oi.order_id = o.id
JOIN Products p ON p.id = oi.product_id
WHERE p.name ILIKE '%mariadb%';
Create B-tree indexes on Products.name, Orders.customer_id, and OrderItems.order_id to speed up the joins and LIKE filter.
1) Use ILIKE for case-insensitive matches.
2) Qualify columns to avoid ambiguity.
3) SELECT only needed columns in production.
Duplicates: Forgetting DISTINCT returns the same customer many times. Add DISTINCT
or aggregate.
Slow scans: Missing indexes causes sequential scans. Index frequently filtered columns.
Yes. CREATE OR REPLACE VIEW maria_db_customers AS (/* core query */);
lets analysts query SELECT * FROM maria_db_customers;
.
Schedule the query in Galaxy or a cron-driven psql script. Store results in a reporting table if historical tracking is required.
Yes. Replace the ILIKE
filter with p.sku = 'MDB-PRO-01'
for exact, index-friendly matching.
Add the schema prefix, e.g., public.Customers
, or set search_path
appropriately.
Use LIKE
instead of ILIKE
; both sides must match case exactly.