-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_queries2.sql
More file actions
217 lines (147 loc) · 3.14 KB
/
Copy pathSQL_queries2.sql
File metadata and controls
217 lines (147 loc) · 3.14 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
-- Unions
-- USE sql_store;
/* SELECT
order_id,
order_date,
'Active' AS status
FROM orders
WHERE order_date >= '2019-01-01'
UNION
SELECT
order_id,
order_date,
'Archived' AS status
FROM orders
WHERE order_date < '2019-01-01' */
/* SELECT first_name
FROM customers
UNION
SELECT name
FROM shippers */
-- Exercise
/* SELECT
customer_id,
first_name,
points,
'Bronze' AS type
FROM customers
WHERE points < 2000
UNION
SELECT
customer_id,
first_name,
points,
'Silver' AS type
FROM customers
WHERE points BETWEEN 2000 AND 3000
UNION
SELECT
customer_id,
first_name,
points,
'Gold' AS type
FROM customers
WHERE points > 3000
ORDER BY first_name */
-- Column Attributes
-- Inserting a single Row
/* INSERT INTO customers (
first_name,
last_name,
birth_date,
address,
city,
state)
VALUES (
'ASHISH',
'PATEL',
'2000-08-02',
'address',
'city',
'UP') */
-- Inserting Multiple rows
/* INSERT INTO shippers (name)
VALUES ('shipper1'),
('shipper2'),
('shipper3'),
('shipper4') */
-- Insert three rows in the products table
/* INSERT INTO products (name, quantity_in_stock, unit_price)
VALUES ('Product1', 10, 1.95),
('Product2', 11, 1.95),
('Product3', 12, 1.95) */
-- Inserting Hierarchical Rows
/* INSERT INTO orders (customer_id, order_date, status)
VALUES (1, '2019-01-02', 1);
INSERT INTO order_items
VALUES
(LAST_INSERT_ID(), 1, 1, 2.95),
(LAST_INSERT_ID(), 2, 1, 3.95) */
-- Creating a copy of a table
/* CREATE TABLE orders_archived AS
SELECT * FROM orders */
/* INSERT INTO orders_archived
SELECT *
FROM orders
WHERE order_date < '2019-01-01' */
-- Exercise
/* USE sql_invoicing;
CREATE TABLE invoices_archived AS
SELECT
i.invoice_id,
i.number,
c.name AS client,
i.invoice_total,
i.payment_total,
i.invoice_date,
i.payment_date,
i.due_date
FROM invoices i
JOIN clients c
USING (client_id)
WHERE payment_date IS NOT NULL */
-- Updating a single Row
/* UPDATE invoices
SET
payment_total = invoice_total*0.5,
payment_date = due_date
WHERE invoice_id = 3 */
-- Updating Multiple Rows
/* UPDATE invoices
SET
payment_total = invoice_total*0.5,
payment_date = due_date
WHERE client_id = 3 */
-- Write a SQL statement to
-- give any customers born before 1990
-- 50 extra points
/* USE sql_store;
UPDATE customers
SET points = points + 50
WHERE birth_date < '1990-01-01' */
/* USE sql_invoicing;
UPDATE invoices
SET
payment_total = invoice_total*0.5,
payment_date = due_date
WHERE client_id IN
(SELECT client_id
FROM clients
WHERE state IN ('CA', 'NY')) */
-- Exercise
/* USE sql_store;
UPDATE orders
SET comments = 'Gold customer'
WHERE customer_id IN
(SELECT customer_id
FROM customers
WHERE points > 3000 ) */
-- Deleting Rows
-- USE sql_invoicing;
/* DELETE FROM invoices
WHERE client_id = (
SELECT *
FROM clients
WHERE name = 'Myworks'
) */
-- Restoring the databases