-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson10.sql
More file actions
144 lines (109 loc) · 2.89 KB
/
Copy pathlesson10.sql
File metadata and controls
144 lines (109 loc) · 2.89 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
/*
Following along lesson 10
*/
use ConsumerComplaints;
DESCRIBE Complaint;
SELECT DateReceived, Product, Company, State
FROM Complaint;
-- Listing 10.4: Using WHERE
-- Two hyphens is a SQL comment. This line is ignored.
-- If your query has many columns, you may want to stack them for readability.
-- Whitespace is ignored.
SELECT
Product,
Issue,
SubmissionMethod
FROM Complaint
WHERE State = 'TX';
-- Listing 10.5: A query to fetch records from the ConsumerComplaints database
USE consumercomplaints;
SELECT *
FROM complaint
WHERE state = 'LA'
AND (Product = 'Mortagage' OR Product = 'Debt collection');
-- Listing 10.6: Dropping the parentheses
USE consumercomplaints;
SELECT *
FROM complaint
WHERE state = 'LA'
AND Product = 'Mortagage' OR Product = 'Debt collection';
-- Listing 10.7: Using Math Comparisons
USE ConsumerComplaints;
SELECT Product, Issue, Company, ResponseToConsumer
FROM complaint
WHERE ConsumerDisputed = 1
AND ConsumerConsent = 1
AND Product NOT IN ('Mortgage', 'Debt collection');
-- Listing 10.8: Invalid WHERE Statements
USE ConsumerComplaints;
-- This query does not return any records at all.
SELECT *
FROM complaint
WHERE SubProduct = NULL;
-- But neither does this!
SELECT *
FROM complaint
WHERE SubProduct != NULL;
-- Still empty
SELECT *
FROM complaiint
WHERE ComplaintId BETWEEN 15000 AND NULL;
-- No NULLS included in results.
SELECT *
FROM complaint
WHERE SubProduct IN ('Other mortgage', NULL);
-- Listing 10.9: Valid WHERE Statements Using IS NULL or IS NOT NULL
USE ConsumerComplaints;
-- Return 278 rows
SELECT *
FROM Complaint
WHERE SubProduct IS NULL;
-- Returns 722 rows
SELECT *
FROM complaint
WHERE SubProduct IS NOT NULL;
-- Returns 991 rows
SELECT *
FROM complaint
WHERE CompaintId > 15000 OR ComplaintId IS NULL;
-- Return 391 rows
SELECT *
FROM Complaint
WHERE SubProduct = 'Other mortgage'
OR SubProduct IS NULL;
-- All complaints with a value for ComplaintNarrative.
-- Exclude null values.
SELECT *
FROM Complaint
WHERE ComplaintNarrative IS NOT NULL;
-- Listing 10.10: Calculating the Number of Days Between Two Dates
USE ConsumerComplaints;
SELECT
ComplaintId,
DateReceived,
DateSentToCompany,
(DateSentToCompany - DateReceived) AS DateDifference
FROM Complaint;
-- Listing 10.11: Using a Calculated Value in a WHERE Clause
USE ConsumerComplaints;
SELECT
ComplaintId,
DateReceived,
DateSentToCompany,
(DateSentToCompany - DateReceived) AS DateDifference
FROM Complaint
WHERE (DateSentToCompany - DateReceived) > 365;
-- Listing 10.12: Using a Calculated Field in a WHERE Clause
USE ConsumerComplaints;
SELECT
Newtable.ComplaintId,
Newtable.DateReceived,
Newtable.DateSentToCompany,
Newtable.DateDifference
FROM (SELECT
ComplaintId,
DateReceived,
DateSentToCompany,
(DateSentToCompany - DateReceived) AS DateDifference
FROM Complaint) AS Newtable
WHERE Newtable.DateDifference > 365;