-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql-week-challenge.sql
More file actions
58 lines (49 loc) · 1.32 KB
/
sql-week-challenge.sql
File metadata and controls
58 lines (49 loc) · 1.32 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
create table CustomerX
(
ID INT IDENTITY(1, 1) NOT NULL,
FirstName NVARCHAR(200) NOT NULL,
LastName NVARCHAR(200) NOT NULL,
Cardnumber INT NOT NULL
Primary key(ID)
);
create table ProductX
(
ID INT IDENTITY(1,1) NOT NULL,
Name NVARCHAR(200) NOT NULL,
Price DECIMAL(10,2) NOT NULL
Primary key(ID)
);
CREATE TABLE OrdersX
(
ID INT IDENTITY(1, 1) NOT NULL,
ProductID INT FOREIGN KEY REFERENCES ProductX(ID) ON DELETE CASCADE ON UPDATE CASCADE,
CustomerID INT FOREIGN KEY REFERENCES CustomerX(ID) ON DELETE CASCADE ON UPDATE CASCADE
Primary key(ID)
);
Insert into CustomerX (FirstName, LastName, CardNumber) values
('Fred', 'Wilcox', 624735),
('Billy', 'Rand', 702854),
('Claire', 'Green', 624735);
insert into ProductX (Name, Price) values
('PC Monitor', 300.00),
('Neon Keyboard', 85.00),
('Samsung Earbuds', 25.00);
insert into OrdersX (CustomerID, ProductID) values
(4, 3),
(4, 1),
(2, 3);
insert into ProductX (Name, Price) values
('iPhone', 200.00);
insert into CustomerX (FirstName, LastName, CardNumber) values
('Tina', 'Smith', 624624);
insert into OrdersX (CustomerID, ProductID) values
(5, 4),
(3, 4);
select * from OrdersX
where CustomerID = 5;
select sum(p.Price) as Revenue from ProductX as p
left join OrdersX on OrdersX.ProductID = p.ID
where p.ID = 4;
update ProductX
set Price = 250.00
where Name = 'iPhone';