Preface to the Second Edition |
|
xvii | |
Acknowledgments |
|
xxi | |
Introduction |
|
xxiii | |
What Is SQL? |
|
xxiv | |
Why SQL? |
|
xxv | |
Who Is This Book For? |
|
xxvi | |
What You'll Learn |
|
xxvi | |
|
1 Setting up your Coding Environment |
|
|
1 | (16) |
|
|
2 | (1) |
|
Downloading Code and Data from GitHub |
|
|
3 | (1) |
|
Installing PostgreSQL and pgAdmin |
|
|
3 | (1) |
|
|
4 | (4) |
|
|
8 | (1) |
|
|
9 | (1) |
|
|
10 | (1) |
|
Launching pgAdmin and Setting a Master Password |
|
|
11 | (1) |
|
Connecting to the Default postgres Database |
|
|
12 | (2) |
|
|
14 | (1) |
|
|
15 | (1) |
|
|
15 | (1) |
|
|
16 | (1) |
|
2 Creating Your First Database and Table |
|
|
17 | (12) |
|
|
17 | (2) |
|
|
19 | (1) |
|
|
20 | (2) |
|
Connecting to the analysis Database |
|
|
22 | (1) |
|
|
22 | (1) |
|
Using the CREATE TABLE Statement |
|
|
22 | (1) |
|
Making the teachers Table |
|
|
23 | (2) |
|
Inserting Rows into a Table |
|
|
25 | (1) |
|
Using the INSERT Statement |
|
|
25 | (1) |
|
|
26 | (1) |
|
Getting Help When Code Goes Bad |
|
|
26 | (1) |
|
Formatting SQL for Readability |
|
|
27 | (1) |
|
|
27 | (2) |
|
3 Beginning Data Exploration with Select |
|
|
29 | (30) |
|
|
30 | (1) |
|
Querying a Subset of Columns |
|
|
31 | (1) |
|
Sorting Data with ORDER BY |
|
|
32 | (1) |
|
Using DISTINCT to Find Unique Values |
|
|
33 | (2) |
|
Filtering Rows with WHERE |
|
|
35 | (2) |
|
Using LIKE and ILIKE with WHERE |
|
|
37 | (1) |
|
Combining Operators with AND and OR |
|
|
38 | (1) |
|
|
39 | (1) |
|
|
39 | (2) |
|
|
41 | (1) |
|
|
42 | (2) |
|
|
44 | (1) |
|
|
45 | (1) |
|
Auto-Incrementing Integers |
|
|
46 | (1) |
|
|
47 | (3) |
|
Choosing Your Number Data Type |
|
|
50 | (1) |
|
Understanding Dates and Times |
|
|
51 | (2) |
|
Using the interval Data Type in Calculations |
|
|
53 | (1) |
|
Understanding JSON and JSONB |
|
|
54 | (1) |
|
Using Miscellaneous Types |
|
|
55 | (1) |
|
Transforming Values from One Type to Another with CAST |
|
|
55 | (1) |
|
Using CAST Shortcut Notation |
|
|
56 | (1) |
|
|
56 | (3) |
|
5 Importing and Exporting Data |
|
|
59 | (18) |
|
Working with Delimited Text Files |
|
|
60 | (1) |
|
|
61 | (1) |
|
Quoting Columns That Contain Delimiters |
|
|
61 | (1) |
|
Using COPY to Import Data |
|
|
62 | (1) |
|
Importing Census Data Describing Counties |
|
|
63 | (1) |
|
Creating the us_counties_pop_est_2019 Table |
|
|
64 | (1) |
|
Understanding Census Columns and Data Types |
|
|
65 | (2) |
|
Performing the Census Import with COPY |
|
|
67 | (1) |
|
|
67 | (1) |
|
Importing a Subset of Columns with COPY |
|
|
68 | (2) |
|
Importing a Subset of Rows with COPY |
|
|
70 | (1) |
|
Adding a Value to a Column During Import |
|
|
71 | (1) |
|
Using COPY to Export Data |
|
|
72 | (1) |
|
|
72 | (1) |
|
Exporting Particular Columns |
|
|
73 | (1) |
|
|
73 | (1) |
|
Importing and Exporting Through pgAdmin |
|
|
74 | (1) |
|
|
75 | (2) |
|
6 Basic Math and Stats With Sql |
|
|
77 | (16) |
|
Understanding Math Operators and Functions |
|
|
78 | (1) |
|
Understanding Math and Data Types |
|
|
78 | (1) |
|
Adding, Subtracting, and Multiplying |
|
|
79 | (1) |
|
Performing Division and Modulo |
|
|
79 | (1) |
|
Using Exponents, Roots, and Factorials |
|
|
80 | (1) |
|
Minding the Order of Operations |
|
|
81 | (1) |
|
Doing Math Across Census Table Columns |
|
|
82 | (1) |
|
Adding and Subtracting Columns |
|
|
82 | (2) |
|
Finding Percentages of the Whole |
|
|
84 | (1) |
|
|
85 | (1) |
|
Using Aggregate Functions for Averages and Sums |
|
|
86 | (1) |
|
|
87 | (1) |
|
Finding the Median with Percentile Functions |
|
|
88 | (1) |
|
Finding Median and Percentiles with Census Data |
|
|
89 | (1) |
|
Finding Other Quantiles with Percentile Functions |
|
|
90 | (1) |
|
|
91 | (1) |
|
|
92 | (1) |
|
7 Joining Tables in a Relational Database |
|
|
93 | (24) |
|
Linking Tables Using JOIN |
|
|
94 | (1) |
|
Relating Tables with Key Columns |
|
|
94 | (3) |
|
Querying Multiple Tables Using JOIN |
|
|
97 | (1) |
|
|
98 | (2) |
|
|
100 | (1) |
|
|
101 | (1) |
|
|
102 | (1) |
|
|
103 | (1) |
|
Using NULL to Find Rows with Missing Values |
|
|
104 | (1) |
|
Understanding the Three Types of Table Relationships |
|
|
105 | (1) |
|
|
105 | (1) |
|
|
105 | (1) |
|
Many-to-Many Relationship |
|
|
105 | (1) |
|
Selecting Specific Columns in a Join |
|
|
106 | (1) |
|
Simplifying JOIN Syntax with Table Aliases |
|
|
107 | (1) |
|
|
107 | (2) |
|
Combining Query Results with Set Operators |
|
|
109 | (1) |
|
|
109 | (2) |
|
|
111 | (1) |
|
Performing Math on Joined Table Columns |
|
|
112 | (2) |
|
|
114 | (3) |
|
8 Table Design that Works for You |
|
|
117 | (22) |
|
Following Naming Conventions |
|
|
118 | (1) |
|
Quoting Identifiers Enables Mixed Case |
|
|
118 | (1) |
|
Pitfalls with Quoting Identifiers |
|
|
119 | (1) |
|
Guidelines for Naming Identifiers |
|
|
120 | (1) |
|
Controlling Column Values with Constraints |
|
|
120 | (1) |
|
Primary Keys: Natural vs. Surrogate |
|
|
121 | (7) |
|
|
128 | (1) |
|
How to Automatically Delete Related Records with CASCADE |
|
|
129 | (1) |
|
|
130 | (1) |
|
|
131 | (1) |
|
|
132 | (1) |
|
How to Remove Constraints or Add Them Later |
|
|
132 | (1) |
|
Speeding Up Queries with Indexes |
|
|
133 | (1) |
|
B-Tree: PostgreSQL's Default Index |
|
|
134 | (3) |
|
Considerations When Using Indexes |
|
|
137 | (1) |
|
|
137 | (2) |
|
9 Extracting Information By Grouping and Summarizing |
|
|
139 | (18) |
|
Creating the Library Survey Tables |
|
|
140 | (1) |
|
Creating the 2018 Library Data Table |
|
|
140 | (1) |
|
Creating the 2017 and 2016 Library Data Tables |
|
|
141 | (1) |
|
Exploring the Library Data Using Aggregate Functions |
|
|
142 | (1) |
|
Counting Rows and Values Using count() |
|
|
143 | (2) |
|
Finding Maximum and Minimum Values Using max() and min() |
|
|
145 | (1) |
|
Aggregating Data Using GROUP BY |
|
|
146 | (8) |
|
|
154 | (3) |
|
10 Inspecting and Modifying Data |
|
|
157 | (26) |
|
Importing Data on Meat, Poultry, and Egg Producers |
|
|
158 | (1) |
|
|
159 | (1) |
|
Checking for Missing Values |
|
|
160 | (2) |
|
Checking for Inconsistent Data Values |
|
|
162 | (1) |
|
Checking for Malformed Values Using length() |
|
|
162 | (2) |
|
Modifying Tables, Columns, and Data |
|
|
164 | (1) |
|
Modifying Tables with ALTER TABLE |
|
|
165 | (1) |
|
Modifying Values with UPDATE |
|
|
166 | (1) |
|
Viewing Modified Data with RETURNING |
|
|
167 | (1) |
|
|
167 | (1) |
|
Restoring Missing Column Values |
|
|
168 | (2) |
|
Updating Values for Consistency |
|
|
170 | (2) |
|
Repairing ZIP Codes Using Concatenation |
|
|
172 | (1) |
|
Updating Values Across Tables |
|
|
173 | (2) |
|
|
175 | (1) |
|
Deleting Rows from a Table |
|
|
175 | (1) |
|
Deleting a Column from a Table |
|
|
176 | (1) |
|
Deleting a Table from a Database |
|
|
176 | (1) |
|
Using Transactions to Save or Revert Changes |
|
|
177 | (2) |
|
Improving Performance When Updating Large Tables |
|
|
179 | (1) |
|
|
180 | (3) |
|
11 Statistical Functions in Sql |
|
|
183 | (20) |
|
Creating a Census Stats Table |
|
|
184 | (1) |
|
Measuring Correlation with corr (Y, X) |
|
|
185 | (3) |
|
Checking Additional Correlations |
|
|
188 | (1) |
|
Predicting Values with Regression Analysis |
|
|
189 | (2) |
|
Finding the Effect of an Independent Variable with r-Squared |
|
|
191 | (1) |
|
Finding Variance and Standard Deviation |
|
|
192 | (1) |
|
Creating Rankings with SQL |
|
|
193 | (1) |
|
Ranking with rank() and dense_rank() |
|
|
193 | (2) |
|
Ranking Within Subgroups with PARTITION BY |
|
|
195 | (1) |
|
Calculating Rates for Meaningful Comparisons |
|
|
196 | (1) |
|
Finding Rates of Tourism-Related Businesses |
|
|
197 | (1) |
|
|
198 | (4) |
|
|
202 | (1) |
|
12 Working with Dates and Times |
|
|
203 | (20) |
|
Understanding Data Types and Functions for Dates and Times |
|
|
204 | (1) |
|
Manipulating Dates and Times |
|
|
204 | (1) |
|
Extracting the Components of a timestamp Value |
|
|
205 | (2) |
|
Creating Datetime Values from timestamp Components |
|
|
207 | (1) |
|
Retrieving the Current Date and Time |
|
|
208 | (1) |
|
|
209 | (1) |
|
Finding Your Time Zone Setting |
|
|
209 | (1) |
|
|
210 | (2) |
|
Performing Calculations with Dates and Times |
|
|
212 | (1) |
|
Finding Patterns in New York City Taxi Data |
|
|
213 | (5) |
|
Finding Patterns in Amtrak Data |
|
|
218 | (4) |
|
|
222 | (1) |
|
13 Advanced Query Techniques |
|
|
223 | (22) |
|
|
223 | (1) |
|
Filtering with Subqueries in a WHERE Clause |
|
|
224 | (1) |
|
Creating Derived Tables with Subqueries |
|
|
225 | (1) |
|
|
226 | (2) |
|
Generating Columns with Subqueries |
|
|
228 | (1) |
|
Understanding Subquery Expressions |
|
|
229 | (2) |
|
Using Subqueries with LATERAL |
|
|
231 | (3) |
|
Using Common Table Expressions |
|
|
234 | (2) |
|
Performing Cross Tabulations |
|
|
236 | (1) |
|
Installing the crosstab() Function |
|
|
237 | (1) |
|
Tabulating Survey Results |
|
|
237 | (2) |
|
Tabulating City Temperature Readings |
|
|
239 | (2) |
|
Reclassifying Values with CASE |
|
|
241 | (1) |
|
Using CASE in a Common Table Expression |
|
|
242 | (2) |
|
|
244 | (1) |
|
14 Mining Text to Find Meaningful Data |
|
|
245 | (30) |
|
Formatting Text Using String Functions |
|
|
246 | (1) |
|
|
246 | (1) |
|
|
246 | (1) |
|
|
247 | (1) |
|
Extracting and Replacing Characters |
|
|
247 | (1) |
|
Matching Text Patterns with Regular Expressions |
|
|
247 | (1) |
|
Regular Expression Notation |
|
|
248 | (2) |
|
Using Regular Expressions with WHERE |
|
|
250 | (1) |
|
Regular Expression Functions to Replace or Split Text |
|
|
251 | (2) |
|
Turning Text to Data with Regular Expression Functions |
|
|
253 | (12) |
|
Full-Text Search in PostgreSQL |
|
|
265 | (1) |
|
|
265 | (2) |
|
Creating a Table for Full-Text Search |
|
|
267 | (1) |
|
|
268 | (3) |
|
Ranking Query Matches by Relevance |
|
|
271 | (2) |
|
|
273 | (2) |
|
15 Analyzing Spatial Data with Postgis |
|
|
275 | (30) |
|
Enabling PostGIS and Creating a Spatial Database |
|
|
276 | (1) |
|
Understanding the Building Blocks of Spatial Data |
|
|
276 | (1) |
|
Understanding Two-Dimensional Geometries |
|
|
277 | (1) |
|
|
278 | (1) |
|
Projections and Coordinate Systems |
|
|
279 | (1) |
|
Spatial Reference System Identifier |
|
|
279 | (1) |
|
Understanding PostGIS Data Types |
|
|
280 | (1) |
|
Creating Spatial Objects with PostGIS Functions |
|
|
281 | (1) |
|
Creating a Geometry Type from Well-Known Text |
|
|
281 | (2) |
|
Creating a Geography Type from Well-Known Text |
|
|
283 | (1) |
|
|
283 | (1) |
|
Using LineString Functions |
|
|
284 | (1) |
|
|
284 | (1) |
|
Analyzing Farmers' Markets Data |
|
|
285 | (1) |
|
Creating and Filling a Geography Column |
|
|
285 | (1) |
|
|
286 | (1) |
|
Finding Geographies Within a Given Distance |
|
|
287 | (2) |
|
Finding the Distance Between Geographies |
|
|
289 | (1) |
|
Finding the Nearest Geographies |
|
|
290 | (1) |
|
Working with Census Shapefiles |
|
|
291 | (1) |
|
Understanding the Contents of a Shapefile |
|
|
292 | (1) |
|
|
292 | (3) |
|
Exploring the Census 2019 Counties Shapefile |
|
|
295 | (3) |
|
Examining Demographics Within a Distance |
|
|
298 | (2) |
|
|
300 | (1) |
|
Exploring Roads and Waterways Data |
|
|
300 | (1) |
|
Joining the Census Roads and Water Tables |
|
|
300 | (1) |
|
Finding the Location Where Objects Intersect |
|
|
301 | (1) |
|
|
302 | (3) |
|
16 Working with Json Data |
|
|
305 | (32) |
|
Understanding JSON Structure |
|
|
306 | (1) |
|
Considering When to Use JSON with SQL |
|
|
307 | (1) |
|
Using json and jsonb Data Types |
|
|
308 | (1) |
|
Importing and Indexing JSON Data |
|
|
309 | (1) |
|
Using json and jsonb Extraction Operators |
|
|
310 | (1) |
|
|
311 | (1) |
|
|
312 | (2) |
|
|
314 | (1) |
|
Containment and Existence |
|
|
315 | (3) |
|
Analyzing Earthquake Data |
|
|
318 | (1) |
|
Exploring and Loading the Earthquake Data |
|
|
318 | (1) |
|
Working with Earthquake Times |
|
|
319 | (2) |
|
Finding the Largest and Most-Reported Earthquakes |
|
|
321 | (2) |
|
Converting Earthquake JSON to Spatial Data |
|
|
323 | (4) |
|
Generating and Manipulating JSON |
|
|
327 | (1) |
|
Turning Query Results into JSON |
|
|
327 | (2) |
|
Adding, Updating, and Deleting Keys and Values |
|
|
329 | (2) |
|
Using JSON Processing Functions |
|
|
331 | (1) |
|
Finding the Length of an Array |
|
|
331 | (1) |
|
Returning Array Elements as Rows |
|
|
332 | (1) |
|
|
333 | (4) |
|
17 Saving Time With Views, Functions, and Triggers |
|
|
337 | (26) |
|
Using Views to Simplify Queries |
|
|
338 | (1) |
|
Creating and Querying Views |
|
|
338 | (3) |
|
Creating and Refreshing a Materialized View |
|
|
341 | (1) |
|
Inserting, Updating, and Deleting Data Using a View |
|
|
342 | (4) |
|
Creating Your Own Functions and Procedures |
|
|
346 | (1) |
|
Creating the percent_change() Function |
|
|
347 | (1) |
|
Using the percent_change() Function |
|
|
348 | (1) |
|
Updating Data with a Procedure |
|
|
349 | (3) |
|
Using the Python Language in a Function |
|
|
352 | (2) |
|
Automating Database Actions with Triggers |
|
|
354 | (1) |
|
Logging Grade Updates to a Table |
|
|
354 | (4) |
|
Automatically Classifying Temperatures |
|
|
358 | (2) |
|
|
360 | (3) |
|
18 Using Postgresql from the Command Line |
|
|
363 | (24) |
|
Setting Up the Command Line for psql |
|
|
364 | (1) |
|
|
364 | (4) |
|
|
368 | (2) |
|
|
370 | (1) |
|
|
370 | (1) |
|
Launching psql and Connecting to a Database |
|
|
370 | (4) |
|
Running SQL Queries on psql |
|
|
374 | (1) |
|
Navigating and Formatting Results |
|
|
375 | (4) |
|
Meta-Commands for Database Information |
|
|
379 | (1) |
|
Importing, Exporting, and Using Files |
|
|
380 | (3) |
|
Additional Command Line Utilities to Expedite Tasks |
|
|
383 | (1) |
|
Adding a Database with createdb |
|
|
383 | (1) |
|
Loading Shapefiles with shp2pgsql |
|
|
383 | (1) |
|
|
384 | (3) |
|
19 Maintaining Your Database |
|
|
387 | (12) |
|
Recovering Unused Space with VACUUM |
|
|
388 | (1) |
|
|
388 | (2) |
|
Monitoring the Autovacuum Process |
|
|
390 | (2) |
|
|
392 | (1) |
|
Reducing Table Size with VACUUM FULL |
|
|
392 | (1) |
|
|
392 | (1) |
|
Locating and Editing postgresql.conf |
|
|
393 | (2) |
|
Reloading Settings with pg_ctl |
|
|
395 | (1) |
|
Backing Up and Restoring Your Database |
|
|
395 | (1) |
|
Using pg_dump to Export a Database or Table |
|
|
395 | (1) |
|
Restoring a Database Export with pg_restore |
|
|
396 | (1) |
|
Exploring Additional Backup and Restore Options |
|
|
397 | (1) |
|
|
397 | (2) |
|
20 Telling Your Data's Story |
|
|
399 | (8) |
|
|
400 | (1) |
|
|
400 | (1) |
|
|
400 | (1) |
|
No Data? Build Your Own Database |
|
|
401 | (1) |
|
Assess the Data's Origins |
|
|
402 | (1) |
|
Interview the Data with Queries |
|
|
402 | (1) |
|
|
403 | (1) |
|
Identify Key Indicators and Trends over Time |
|
|
403 | (2) |
|
|
405 | (1) |
|
Communicate Your Findings |
|
|
405 | (1) |
|
|
406 | (1) |
|
APPENDIX: ADDITIONAL POSTGRESQL RESOURCES |
|
|
407 | (4) |
|
PostgreSQL Development Environments |
|
|
407 | (1) |
|
PostgreSQL Utilities, Tools, and Extensions |
|
|
408 | (1) |
|
PostgreSQL News and Community |
|
|
409 | (1) |
|
|
410 | (1) |
Index |
|
411 | |