-
Notifications
You must be signed in to change notification settings - Fork 0
/
Customer Table Data.sql
126 lines (100 loc) · 3.23 KB
/
Customer Table Data.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
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
create table Customer
( id int Primary Key identity (1,1),
FirstName varchar(50),
LastName varchar(50),
Age int,
City varchar(50)
)
select *
from Customer;
insert into Customer
(FirstName,LastName,Age,City)
values ('Joey','Blue',40,'Goddard');
insert into Customer
(FirstName,LastName,Age,City)
values ('Barry','Bonds',50,'San Francisco');
insert into Customer
(FirstName,LastName,Age,City)
values ('Mike','Schmidt',60,'KC');
create table Products
( id int Primary Key identity (1,1),
ProductName varchar(50)
)
alter table Products
add Price float;
Select * from Products
insert into Products (ProductName,Price) values ('Baseball',5.95);
insert into Products (ProductName,Price) values ('Bat',195.99);
create table Orders
( OrderId int Primary key identity(1,1),
OrderDate Datetime,
CustomerID int,
ProductID int
)
select * from Orders;
select * from Products;
select * from Customer;
insert into Orders(OrderDate,CustomerID,ProductID)
values (GETDATE(),2,2);
insert into Orders(OrderDate,CustomerID,ProductID)
values (GETDATE(),2,1);
insert into Orders(OrderDate,CustomerID,ProductID)
values (GETDATE(),1,1);
insert into Orders(OrderDate,CustomerID,ProductID)
values (GETDATE(),3,1);
insert into Orders(OrderDate,CustomerID,ProductID)
values (GETDATE(),3,2);
insert into Orders(OrderDate,CustomerID,ProductID)
values (GETDATE(),1,2);
alter table Orders
add foreign key (CustomerId) references Customer(Id);
alter table Orders
add foreign key (ProductId) references Products(Id);
select * from Orders
inner join Products on Orders.ProductID=Products.id
select * from Orders as o
inner join Products as p on o.ProductID=p.id
select * from Orders o
inner join Products p on o.ProductID=p.id
select o.*,p.*,c.* from Orders o
inner join Products p on o.ProductID=p.id
inner join Customer c on o.CustomerID=c.id
select o.OrderDate,p.ProductName,p.Price,c.* from Orders o
inner join Products p on o.ProductID=p.id
inner join Customer c on o.CustomerID=c.id
update Customer
Set City='Washington DC'
where FirstName='Barry'and LastName='Bonds'
select sum (p.Price)
from Orders o
inner join Products p on o.ProductID=p.id
inner join Customer c on o.CustomerID=c.id
select sum (p.Price) as Total
from Orders o
inner join Products p on o.ProductID=p.id
inner join Customer c on o.CustomerID=c.id
select c.LastName, sum (p.Price) Total
from Orders o
inner join Products p on o.ProductID=p.id
inner join Customer c on o.CustomerID=c.id
group by c.LastName
select c.LastName, p.ProductName, sum (p.Price) Total
from Orders o
inner join Products p on o.ProductID=p.id
inner join Customer c on o.CustomerID=c.id
group by c.LastName, p.ProductName
select c.LastName, p.ProductName, sum (p.Price) Total
from Orders o
inner join Products p on o.ProductID=p.id
inner join Customer c on o.CustomerID=c.id
group by p.ProductName, c.LastName
select c.City, sum(p.Price),AVG(p.price) Total
from Orders o
inner join Products p on o.ProductID=p.id
inner join Customer c on o.CustomerID=c.id
group by c.City
select c.City, sum(p.Price) Total,AVG(p.price) Average
from Orders o
inner join Products p on o.ProductID=p.id
inner join Customer c on o.CustomerID=c.id
group by c.City