-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_null_top_distinct_where.sql
239 lines (190 loc) · 6.16 KB
/
03_null_top_distinct_where.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
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
/*
Working with NULL values
Top and Distinct functions
WHERE Clause
*/
-- Working with NULL VALUES
-- This doesn't work. Anytime the MiddleName is NULL the entire expression becomes NULL
SELECT FirstName + ' ' + MiddleName + ' ' + LastName AS FullName,
FirstName, MiddleName, LastName
FROM Person.Person;
--Use the ISNULL function to replace the NULL value
SELECT FirstName + ' ' + ISNULL(MiddleName,'') + ' ' + LastName AS FullName,
FirstName, MiddleName, LastName
FROM Person.Person;
-- *** Top and Distinct Functions
--Returns 121,317 rows
SELECT * FROM Sales.SalesOrderDetail;
--Returns 10 rows
SELECT TOP(10) *
FROM Sales.SalesOrderDetail;
--Returns 12,132 rows
SELECT TOP(10) PERCENT *
FROM Sales.SalesOrderDetail;
--Use with ORDER BY
SELECT TOP(10) SalesOrderID, SalesOrderDetailID
FROM Sales.SalesOrderDetail
ORDER BY SalesOrderID;
--Use DISTINCT to get a unique set of rows
SELECT DISTINCT Color
FROM Production.Product;
-- Still returns all the rows because each row is unique
SELECT *
FROM Production.Product;
SELECT DISTINCT *
FROM Production.Product;
-- WHERE CLAUSE
SELECT CustomerID, SalesOrderID, OrderDate
FROM Sales.SalesOrderHeader
WHERE CustomerID = 29825;
SELECT CustomerID, SalesOrderID, OrderDate
FROM Sales.SalesOrderHeader
WHERE CustomerID = CustomerID;
SELECT CustomerID, SalesOrderID, OrderDate
FROM Sales.SalesOrderHeader
WHERE 1 = 2;
-- Using a function in the where clause
-- This is not always a good idea.
-- If there was an index on OrderDate, SQL would not be able to use it effectively
SELECT CustomerID, SalesOrderID, OrderDate
FROM Sales.SalesOrderHeader
WHERE YEAR(OrderDate) = 2013;
SELECT FirstName, LastName
FROM Person.Person
WHERE LEFT(LastName ,1) = 'S';
-- OPERATORS
SELECT CustomerID
FROM Sales.SalesOrderHeader
WHERE CustomerID = 11000;
SELECT CustomerID
FROM Sales.SalesOrderHeader
WHERE CustomerID <> 11000;
SELECT CustomerID
FROM Sales.SalesOrderHeader
WHERE CustomerID != 11000;
SELECT CustomerID
FROM Sales.SalesOrderHeader
WHERE CustomerID > 11000;
SELECT CustomerID
FROM Sales.SalesOrderHeader
WHERE CustomerID < 11005;
SELECT CustomerID
FROM Sales.SalesOrderHeader
WHERE CustomerID <= 11005;
SELECT CustomerID
FROM Sales.SalesOrderHeader
WHERE CustomerID BETWEEN 11000 AND 11005;
SELECT FirstName, LastName
FROM Person.Person
WHERE LastName BETWEEN 'A' and 'C';
-- LIKE operator
-- Use LIKE when you know at least the first letter
-- Return all the LastName values that start with S
SELECT LastName
FROM Person.Person
WHERE LastName LIKE 'S%';
--Can use % anywhere in the value
--Return all the LastName values that have S in them
SELECT LastName
FROM Person.Person
WHERE LastName LIKE '%s%';
--Use _ to replace one character
SELECT LastName
FROM Person.Person
WHERE LastName LIKE 'Anders_n';
--Can use a list of possible values to replace one character
SELECT LastName
FROM Person.Person
WHERE LastName LIKE 'Anders[eo]n';
-- IN OPERATOR
SELECT FirstName, LastName
FROM Person.Person
WHERE LastName IN ('Smith','Anderson');
SELECT OrderDate, SalesOrderID
FROM Sales.SalesOrderHeader
WHERE OrderDate IN ('2012-08-01','2013-08-01');
SELECT CustomerID, OrderDate, SalesOrderID
FROM Sales.SalesOrderHeader
WHERE CustomerID IN (11000,11001);
-- Multiple Predicates
-- Use AND when both predicates must be true
SELECT BusinessEntityID, FirstName, LastName
FROM Person.Person
WHERE FirstName = 'Hailey' AND LastName = 'Barnes';
-- USE OR When either can be true
SELECT BusinessEntityID, FirstName, LastName
FROM Person.Person
WHERE FirstName = 'Hailey' OR LastName = 'Barnes';
-- Can use more than two conditions
-- and any type of predicate
SELECT SalesOrderID, CustomerID, OrderDate
FROM Sales.SalesOrderHeader
WHERE CustomerID BETWEEN 11000 AND 12000
AND OrderDate >= '2012-01-01' AND OrderDate < '2013-01-01'
ORDER BY CustomerID
--Combine OR and AND
SELECT BusinessEntityID, FirstName, LastName
FROM Person.Person
WHERE FirstName = 'Hailey' OR FirstName = 'Haley';
--Find Hailey or Haley Barnes
--This one finds Haley Barnes plus any Hailey
SELECT BusinessEntityID, FirstName, LastName
FROM Person.Person
WHERE FirstName = 'Hailey' OR FirstName = 'Haley'
AND LastName = 'Barnes';
--To solve this, always include parentheses to enforce logic
--Here, the first name can be Hailey or Haley, and the last name must be Barnes
SELECT BusinessEntityID, FirstName, LastName
FROM Person.Person
WHERE (FirstName = 'Hailey' OR FirstName = 'Haley')
AND LastName = 'Barnes';
-- NOT Predicate
-- Use NOT to negate a predicate
SELECT FirstName, LastName
FROM Person.Person
WHERE NOT LastName = 'Smith'; -- same thing as LastName <> 'Smith'
--Retuns any records with Haley OR Barnes
SELECT BusinessEntityID, FirstName, LastName
FROM Person.Person
WHERE FirstName = 'Hailey' OR LastName = 'Barnes';
--Returns all the rest
SELECT BusinessEntityID, FirstName, LastName
FROM Person.Person
WHERE NOT(FirstName = 'Hailey' OR LastName = 'Barnes');
--All rows with 11000
SELECT CustomerID, SalesOrderID
FROM Sales.SalesOrderHeader
WHERE CustomerID IN(11000);
--all the other rows
SELECT CustomerID, SalesOrderID
FROM Sales.SalesOrderHeader
WHERE CustomerID NOT IN(11000);
--the rows that start with S
SELECT BusinessEntityID, FirstName, LastName
FROM Person.Person
WHERE LastName LIKE 'S%';
--the rows that do not start with S
SELECT BusinessEntityID, FirstName, LastName
FROM Person.Person
WHERE LastName NOT LIKE 'S%';
-- Working with NULL values
-- Total Rows
-- 19,972 rows
SELECT FirstName, MiddleName, LastName
FROM Person.Person;
--Find the rows with MiddleName = B
--291 rows
SELECT FirstName, MiddleName, LastName
FROM Person.Person
WHERE MiddleName = 'B';
--Find the rows where MiddleName <> B
-- 19972 - 291 = 19681?
-- No, only 11,182 rows returned
-- We do not know if the NULL rows are B or not!
SELECT FirstName, MiddleName, LastName
FROM Person.Person
WHERE MiddleName <> 'B';
--Use the IS NULL operator
SELECT FirstName, MiddleName, LastName
FROM Person.Person
WHERE MiddleName <> 'B' OR MiddleName IS NULL;