Practical SQL

Practical SQL

A Beginner’s Guide to Storytelling with Data
by Anthony DeBarros
May 2018, 392 pp.

Practical SQL is an approachable and fast-paced guide to SQL (Structured Query Language), the standard programming language for defining, organizing, and exploring data in relational databases. The book focuses on using SQL to find the story your data tells, with the popular open-source database PostgreSQL and the pgAdmin interface as its primary tools.

You’ll first cover the fundamentals of databases and the SQL language, then build skills by analyzing data from the U.S. Census and other federal and state government agencies. With exercises and real-world examples in each chapter, this book will teach even those who have never programmed before all the tools necessary to build powerful databases and access information quickly and efficiently.

You’ll learn how to:

  • Create databases and related tables using your own data
  • Define the right data types for your information
  • Aggregate, sort, and filter data to find patterns
  • Use basic math and advanced statistical functions
  • Identify errors in data and clean them up
  • Import and export data using delimited text files
  • Write queries for geographic information systems (GIS)
  • Create advanced queries and automate tasks

Learning SQL doesn’t have to be dry and complicated. Practical SQL delivers clear examples with an easy-to-follow approach to teach you the tools you need to build and manage your own databases.

This book uses PostgreSQL, but the SQL syntax is applicable to many database applications, including Microsoft SQL Server and MySQL.

Author Bio 

Anthony DeBarros is an award-winning journalist who's combined avid interests in data analysis, coding, software product development, and storytelling for much of his career. He's currently a data news editor for the Wall Street Journal, focusing on topics including the economy, trade, the U.S. Census, and politics. He previously held positions with USA TODAY, the Poughkeepsie Journal, and the DocumentCloud project for Investigative Reporters and Editors.

Table of contents 

Chapter 1: Creating Your First Database and Table
Chapter 2: Beginning Data Exploration with SELECT
Chapter 3: Understanding Data Types
Chapter 4: Importing and Exporting Data
Chapter 5: Basic Math and Stats with SQL
Chapter 6: Table Joins in a Relational Database
Chapter 7: Table Design that Works for You
Chapter 8: Extracting Information by Grouping and Summarizing
Chapter 9: Inspecting and Modifying Data
Chapter 10: Statistical Functions in SQL
Chapter 11: Working with Dates and Times
Chapter 12: Advanced Query Techniques
Chapter 13: Extracting Data from Text
Chapter 14: Analyzing Spatial Data with PostGIS
Chapter 15: Saving Time with Views, Functions, and Triggers
Chapter 16: Using PostgreSQL from the Command Line
Chapter 17: Maintaining Your Database
Chapter 18: Identifying and Telling the Story Behind Your Data
Appendix: Additional PostgreSQL Resources

View the Detailed Table of Contents
View the Index


“One of the best-organized travelogues we've seen into the SQL realm.”
Newstips Bulletin


Figure 1 on page xxxiii of the Introduction shows the pgAdmin app's opening screen. Beginning with version 3.0 of pgAdmin, released in April 2018, the application loads as a tab within your default web browser. Previous versions of pgAdmin run as a standalone desktop application. All other functionality for pgAdmin described in the book remains the same.

Page xxix: In step 8, the text that reads:
Select the box and click Finish.

Should read:
Make sure the box is checked and click Finish.

Page xxx: In step 13, the sentence that reads:
Answer Yes when asked to register GDAL.

Should read:
Answer Yes when asked to register the GDAL_DATA environment variable.

Page xxxii: The sentence that reads:
In the object browser, expand the plus sign (+) to the left of the Servers node to show the default server.

Should read:
In the object browser, click the arrow (>) to the left of the Servers node to show the default server.

Page 4:
In step 6, change "In the SQL Editor pane ..." to "In the Query Editor pane ..."
In step 7, the sentence that read:
Click the lightning bolt icon to execute the statement.

Should read:
Click the Execute/Refresh icon (shaped like a right arrow) to execute the statement.

Page 182: The URL in the first full paragraph should be updated to:

Page 195:
The results shown for Listing 12-3 are correct. However, the first sentence after the results has an incorrect number. It should read, "The difference between the median and average, 72,376, is nearly three times the size of the median."

Page 218:
Change "When you hover your cursor over any cell ..." to "When you double-click on any cell ..."

Page 230:
The final line in Listing 13-13 should have a space instead of a comma between the quote marks in the second argument. It should read:
SELECT regexp_split_to_array('Phil Mike Tony Steve', ' ');

Page 238:
The final line of Listing 13-25 is missing a semi-colon. It should read:

Page 269: The sentence that read:
When you right-click the view and choose Properties, you should see the query under the Definition tab in the dialog that opens.

Should read:
When you right-click the view and choose Properties, you should see the query under the Code tab in the dialog that opens.