Skip to content

Instantly share code, notes, and snippets.

@aleksandar-b
Created May 8, 2023 16:43
Show Gist options
  • Save aleksandar-b/14519d4cc11dc339c5362433e960ac89 to your computer and use it in GitHub Desktop.
Save aleksandar-b/14519d4cc11dc339c5362433e960ac89 to your computer and use it in GitHub Desktop.
SQL Query: Electronics
SELECT
u.name AS user_name,
u.email AS user_email,
SUM(o.quantity * p.price) AS total_spent
FROM
users u
JOIN
orders o ON u.id = o.user_id
JOIN
products p ON o.product_id = p.id
WHERE
p.category = 'Electronics'
GROUP BY
u.id, u.name, u.email
HAVING
COUNT(o.id) >= 3 AND SUM(o.quantity * p.price) > 1000
ORDER BY
total_spent DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment