-
Notifications
You must be signed in to change notification settings - Fork 0
/
kyu5_SQL_Basics_simple_VIEW.sql
31 lines (29 loc) · 1.11 KB
/
kyu5_SQL_Basics_simple_VIEW.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- SQL Basics: Simple VIEW
--
-- https://www.codewars.com/kata/5811527d9d278b242f000006
--
-- For this challenge you need to create a VIEW. This VIEW is used by a sales store to give out vouches to members
-- who have spent over $1000 in departments that have brought in more than $10000 total ordered by the members id;
-- The VIEW must be called members_approved_for_voucher then you must create a SELECT query using the view;
--
-- resultant table schema
-- id
-- name
-- email
-- total_spending
CREATE VIEW members_approved_for_voucher AS
SELECT members.id, members.name, members.email, SUM(products.price) AS total_spending
FROM members
JOIN sales ON sales.member_id=members.id
JOIN products ON products.id=sales.product_id
WHERE sales.department_id IN (
SELECT sales.department_id
FROM sales
INNER JOIN products ON products.id = sales.product_id
GROUP BY sales.department_id
HAVING SUM(products.price) > 10000
)
GROUP BY members.id, members.name, members.email
HAVING SUM(products.price) > 1000
ORDER BY members.id;
SELECT * FROM members_approved_for_voucher;