-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.sql
More file actions
286 lines (193 loc) · 5.21 KB
/
examples.sql
File metadata and controls
286 lines (193 loc) · 5.21 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
279
280
281
282
283
284
285
286
-- List table columns according to conditions
SELECT TableName = tbl.TABLE_SCHEMA + '.' + tbl.TABLE_NAME,
ColumnName = col.COLUMN_NAME,
ColumnDataType = col.DATA_TYPE
FROM INFORMATION_SCHEMA.TABLES tbl
INNER JOIN
INFORMATION_SCHEMA.COLUMNS col
ON col.TABLE_NAME = tbl.TABLE_NAME
AND col.TABLE_SCHEMA = tbl.TABLE_SCHEMA
WHERE tbl.TABLE_TYPE = 'BASE TABLE' AND tbl.TABLE_NAME LIKE 'Customer'
-- Select all columns
SELECT *
FROM SalesLT.Customer
-- Select subset of columns
SELECT CustomerID, Title
FROM SalesLT.Customer
-- Add filtering with WHERE clause
SELECT *
FROM SalesLT.Product
WHERE StandardCost > 500
-- Add operators to WHERE clause
SELECT *
FROM SalesLT.Product
WHERE Color = 'Black'
OR Color = 'Red'
SELECT Name, Color
FROM SalesLT.Product
WHERE Color IN ('Red', 'Black')
SELECT Name, Color, StandardCost
FROM SalesLT.Product
WHERE StandardCost between 50 and 100
-- Example aggregate
SELECT count(ProductID), Color
FROM SalesLT.Product
GROUP BY Color
-- Examples using HAVING to filter when working with aggregates.
SELECT count(ProductID), Color
FROM SalesLT.Product
GROUP BY Color
HAVING count(ProductID) > 10
-- Note we can still use WHERE in the query, just not on the column used in the aggregate functio
SELECT count(ProductID) as CountID, Color
FROM SalesLT.Product
WHERE ListPrice > 500
GROUP BY Color
HAVING count(ProductID) > 10
-- Sorting results
SELECT count(ProductID) AS CountID, Color
FROM SalesLT.Product
WHERE ListPrice > 500
GROUP BY Color
HAVING count(ProductID) > 10
ORDER BY CountID ASC
-- Note NULL is not zero
SELECT *
FROM SalesLT.Product
WHERE Color IS NULL
SELECT *
FROM SalesLT.Product
WHERE Color IS NOT NULL
-- Selecting ranges
SELECT TOP 10 *
FROM SalesLT.Product
SELECT TOP 10 StandardCost
FROM SalesLT.Product
ORDER BY StandardCost DESC
SELECT TOP 10 PERCENT StandardCost
FROM SalesLT.Product
ORDER BY StandardCost DESC
SELECT Name, MAX(StandardCost)
FROM SalesLT.Product
GROUP BY Name
SELECT Name, MIN(StandardCost)
FROM SalesLT.Product
GROUP BY Name
SELECT Name, AVG(StandardCost)
FROM SalesLT.Product
GROUP BY Name
SELECT COUNT(*)
FROM SalesLT.Product
SELECT COUNT(*)
FROM SalesLT.Product
WHERE Color = 'Black'
-- Fuzzy lookups with LIKE. Note wildcard
SELECT Name
FROM SalesLT.Product
WHERE Name LIKE '%Mountain%'
-- Alias for column and table
SELECT Name AS 'Product Name'
FROM SalesLT.Product
WHERE Name LIKE '%Mountain%'
SELECT t.name as 'Table Name', s.name as 'Schema Name'
FROM sys.tables t
LEFT JOIN sys.schemas s on t.schema_id = s.schema_id
-- Joins for taking columns from different tables and aligning them side by side in the result set
-- Inner - Rows that have matches from both sides
-- Left - All from the left, matches from the right
-- Right - All from the right, matches from the left
-- Full outer - All rows that have a match on either side
SELECT c.FirstName, c.LastName, ca.AddressID
FROM SalesLT.Customer c
INNER JOIN SalesLT.CustomerAddress ca
ON c.CustomerID = ca.CustomerID
SELECT c.FirstName, c.LastName, ca.AddressID
FROM SalesLT.Customer c
LEFT JOIN SalesLT.CustomerAddress ca
ON c.CustomerID =
ca.CustomerID
SELECT c.FirstName, c.LastName, ca.AddressID
FROM SalesLT.Customer c
RIGHT JOIN SalesLT.CustomerAddress ca
ON c.CustomerID = ca.CustomerID
SELECT c.FirstName, c.LastName, ca.AddressID
FROM SalesLT.Customer c
FULL OUTER JOIN SalesLT.CustomerAddress ca
ON c.CustomerID = ca.CustomerID
SELECT p.ProductID, p.Name,
pm.Name AS "Product Model Name",
pmx.Culture
FROM SalesLT.Product p
INNER JOIN SalesLT.ProductModel pm
ON p.ProductModelID = pm.ProductModelID
INNER JOIN SalesLT.ProductModelProductDescription pmx
ON pm.ProductModelID = pmx.ProductModelID
-- Sub qeury
SELECT *
FROM SalesLT.Product
WHERE ProductModelID in
(SELECT ProductModelID from SalesLT.ProductModel
WHERE ProductModelID = 6)
-- Union for stacking results
SELECT City FROM SalesLT.Address
UNION
SELECT City FROM SalesLT.Address
ORDER BY City
-- Logic Conditions
SELECT Name, StandardCost, "Price Comment" =
CASE
WHEN StandardCost > 1000 THEN 'Expensive'
ELSE 'Affordable'
END
FROM SalesLT.Product
-- Insert specific columns
INSERT INTO SalesLT.Customer
(FirstName, LastName, PasswordHash, PasswordSalt)
VALUES ('JP', 'Boyd', 'L/Rlwxzp4w7RWmEgXX+/A7cXaePEPcp+KwQhl2fJL7w=', 'eiCRDs8=')
-- Insert another table
SELECT *
INTO SalesLT.Customer_Test
FROM SalesLT.Customer
SELECT *
FROM SalesLT.Customer_Test
WHERE LastName = 'Boyd'
-- Update
UPDATE SalesLT.Customer_Test
SET Suffix = 'Mr'
WHERE LastName = 'Boyd'
-- Delete
DELETE FROM SalesLT.Customer_Test
WHERE LastName = 'Boyd'
-- Create Database
CREATE DATABASE jpbtest
SELECT *
FROM sys.databases
-- Delete Database
DROP DATABASE jpbtest
-- Create Table
CREATE TABLE jpbtest
(
FirstName VARCHAR(20),
LastName VARCHAR(20)
)
-- Amend Table
ALTER TABLE jpbtest
ADD Title VARCHAR(20)
-- Create index with keys of interest to speed up query performance, to save doing a full table scan
CREATE INDEX Test_Index
ON SalesLT.Product
(Name, Color)
SELECT *
FROM sys.indexes
WHERE name = 'Test_Index'
DROP INDEX Test_Index
ON SalesLT.Product
--- Create a view
SELECT *
FROM SalesLT.Customer
CREATE VIEW SalesLT.CustomerMinimum
AS
SELECT CustomerID, Title
FROM SalesLT.Customer
SELECT *
FROM SalesLT.CustomerMinimum