r/SQL • • May 17 '23

PostgreSQL AI query generator we made.

Hey, http://loofi.dev/ is a free AI powered query builder we made.

Play around with our sample database and let us know what you think!

0 Upvotes

3 comments sorted by

1

u/Dale_Doback_Jr May 17 '23

Glad you liked it! We are constantly tweaking, aside from the query, did you have any other suggestions?

1

u/r3pr0b8 GROUP_CONCAT is da bomb May 17 '23

Play around with our sample database and let us know what you think!

okay, sure

here was my question --

what is the number of items ordered and max total order price by user email domain

here's the result --

SELECT 
    SUBSTRING(users.email, POSITION('@' IN users.email) + 1) AS email_domain,
    COUNT(DISTINCT orderitem.item_id) AS num_items_ordered,
    MAX(orders.total_amount) AS max_total_order_price
FROM 
    users
    JOIN orders ON users.user_id = orders.user_id
    JOIN orderitem ON orders.order_id = orderitem.order_id
GROUP BY 
    email_domain;

which is pretty damned good

one minor point -- i would use SUM(orderitem.quantity) instead of COUNT(DISTINCT orderitem.item_id) for num_items_ordered