-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRunSQLQuery.sql
More file actions
278 lines (216 loc) · 7.4 KB
/
RunSQLQuery.sql
File metadata and controls
278 lines (216 loc) · 7.4 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
Use W3SchoolsDemoDatabaseNorthwindLT;
--SELECT * Example
select * from Customers;
--SELECT Column Example
select CustomerName, City from Customers;
--SELECT DISTINCT Examples
select Country from Customers;
select distinct Country from Customers;
select count(distinct Country) from Customers;
--START: WHERE Clause Example
select * from Customers
where Country='Mexico';
--Text Fields vs. Numeric Fields
select * from Customers
where CustomerID=1;
--AND Example
select * from Customers
where Country='Germany' and City='Berlin';
--OR Example
select * from Customers
where City='Berlin' or City='Aachen';
--NOT Example
select * from Customers
where Country<>'Germany';
--Combining AND, OR and NOT
select * from Customers
where Country='Germany' and (City='Berlin' or City='Aachen');
--using <> operator "Germany" and <> "USA"
select * from Customers
where Country<>'Germany' and Country<>'USA';
--NOT "Germany" and NOT "USA":
select * from Customers
where Not Country='Germany' and not Country='USA';
--END: WHERE Clause Example
--START: ORDER BY Example
select * from Customers
order by Country;
--ORDER BY DESC Example
select * from CUstomers
order by Country desc;
--ORDER BY Several Columns Example
select * from Customers
order by Country , CustomerName;
--ORDER BY Several Columns Example 2
select * from Customers
order by country asc, CustomerName desc;
--END: ORDER BY Example
--INSERT INTO Example
insert into Customers(CustomerName, ContactName, Address, City, PostalCode, Country)
values('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
--Check inserted row in Customers table
select * from Customers
where CustomerName='Cardinal';
--Insert Data Only in Specified Columns
insert into Customers(CustomerName, City, Country)
values ('tarak', 'kolkata', 'India');
--Check inserted row in Customers table
select * from Customers
where CustomerName='tarak';
--The IS NULL Operator
select * from Customers
where ContactName is Null;
--The IS NOT NULL Operator
select * from Customers
where ContactName is not null;
--START: UPDATE Table
-- Test Customer record Before Update
select * from Customers
where CustomerID=1;
--Update for CustomerID = 1
update Customers
set ContactName='Alfred Schmidt', City='Frankfurt'
where CustomerID=1;
-- Test Customer record After Update
select * from Customers
where CustomerID=1;
--UPDATE Multiple Records
-- Test Customer record Before Update
select * from Customers
where Country='Mexico';
--Update for Country='Mexico'
update Customers
set ContactName='Juan'
where Country='Mexico';
-- Test Customer record After Update
select * from Customers
where Country='Mexico';
--Update back to original state of Customers table
update Customers
set ContactName='Ana Trujillo'
where CustomerID=2;
update Customers
set ContactName='Antonio Moreno'
where CustomerID=3;
update Customers
set ContactName='Francisco Chang'
where CustomerID=13;
update Customers
set ContactName='Guillermo Fernández'
where CustomerID=58;
update Customers
set ContactName='Miguel Angel Paolino'
where CustomerID=80;
--END: UPDATE Table
--SQL DELETE Example
delete from Customers
where CustomerName='tarak';
--Test the deletion from Customers table
select * from Customers
where CustomerName='tarak';
--Delete the CustomerName='Cardinal'
delete from Customers
where CustomerName='Cardinal';
--Test the deletion from Customers table
select * from Customers
where CustomerName='Cardinal';
--SQL TOP
select top 3 * from Customers;
--SQL TOP PERCENT Example
select top 50 percent * from Customers;
--Top with ADD a WHERE CLAUSE
select top 3 * from Customers
where Country='Germany';
--MIN() Example
select MIN(Price) As SmallestPrice
from Products;
--MAX() Example
select MAX(Price) As LargestPrice
from Products;
--COUNT() Example
select COUNT(ProductID) As TotalNumOfProducts
from Products;
--AVG() Example
select AVG(Price) As AvgPrice
from Products;
--SUM() Example
select SUM(Quantity) As TotalQuantityOfOrderDetails
from OrderDetails;
--START: SQL LIKE Examples
--CustomerName starting with "a":
select * from Customers
where CustomerName like 'a%';
--CustomerName ending with "a":
select * from Customers
where CustomerName like '%a';
--CustomerName that have "or" in any position:
select * from Customers
where CustomerName like '%or%';
--CustomerName that have "r" in the second position:
select * from Customers
where CustomerName like '_r%';
--CustomerName that starts with "a" and are at least 3 characters in length:
select * from Customers
where CustomerName like 'a_%_%';
--ContactName that starts with "a" and ends with "o":
select * from Customers
where ContactName like 'a%o';
--CustomerName that does NOT start with "a":
select * from Customers
where CustomerName not like 'a%';
--Using the [charlist] Wildcard
select * from Customers
where City like '[bsp]%';
--END: SQL LIKE Examples
--START: IN Operator Examples
select * from Customers
where Country in ('Germany', 'France', 'UK');
--NOT located in "Germany", "France" or "UK":
select * from Customers
where Country not in ('Germany', 'France', 'UK');
--selects all customers that are from the same countries as the suppliers:
select * from Customers
where Country in (select Country from Suppliers);
--END: IN Operator Examples
--BETWEEN Example
select * from Products
where Price between 10 and 20;
--NOT BETWEEN Example
select * from Products
where Price not between 10 and 20;
--selects all products with a price BETWEEN 10 and 20. In addition; do not show products with a CategoryID of 1,2, or 3:
select * from Products
where (Price between 10 and 20) and CategoryID not in (1, 2, 3);
--selects all products with a ProductName BETWEEN 'Carnarvon Tigers' and 'Mozzarella di Giovanni':
select * from Products
where ProductName between 'Carnarvon Tigers' and 'Mozzarella di Giovanni'
order by ProductName;
--selects all products with a ProductName NOT BETWEEN 'Carnarvon Tigers' and 'Mozzarella di Giovanni':
select * from Products
where ProductName not between 'Carnarvon Tigers' and 'Mozzarella di Giovanni'
order by ProductName;
--selects all orders with an OrderDate BETWEEN '04-July-1996' and '09-July-1996':
select * from Orders
where OrderDate between '07/04/1996' and '07/09/1996';
--Alias for Columns Examples
select CustomerID as ID, CustomerName as Customer
from Customers;
--It requires double quotation marks or square brackets if the alias name contains spaces:
select CustomerName as Customer, ContactName as [Contact Person]
from Customers;
--creates an alias named "Address" that combine four columns (Address, PostalCode, City and Country):
select CustomerName, Address+', '+PostalCode+', '+City+', '+Country as Address
from Customers;
--Alias for Tables Example
select o.OrderID, o.OrderDate, c.CustomerName
from Customers as c, Orders as o
where c.CustomerName='Around the Horn' and c.CustomerID=o.CustomerID;
--START: SQL JOIN
select Orders.OrderID, Customers.CustomerName, Orders.OrderDate
from Orders inner join Customers
on Orders.CustomerID=Customers.CustomerID;
--END: SQL JOIN
--JOIN Three Tables
select Orders.OrderID, Customers.CustomerName, Shippers.ShipperName
from (Orders inner join Customers on Orders.CustomerID=Customers.CustomerID) inner join Shippers
on Orders.ShipperID=Shippers.ShipperID;