πŸ“˜

Pages

466

Published

2022

SQL ✨ New

Practical SQL, 2nd Edition

A Beginner's Guide to Storytelling with Data Using PostgreSQL

Learn SQL by working with real datasets so you can query, analyze, and report on data with confidence from day one.

Practical SQL teaches you relational database concepts and SQL syntax through hands-on projects built around real-world data. Using PostgreSQL, you work with genuine datasets β€” U.S. Census figures, crime statistics, and more β€” to write queries, clean messy data, perform statistical analysis, and build reports. Each chapter adds a concrete skill, so by the end you can design tables, import data, and answer real analytical questions with SQL.

About this book

Most SQL books teach you syntax. Practical SQL teaches you how to think like an analyst. Anthony DeBarros walks you through each concept using real datasets β€” U.S. Census records, crime reports, taxi trip data β€” so every query you write answers a genuine question about the world, not a contrived classroom exercise.

The second edition is built around PostgreSQL, one of the most capable and widely deployed open-source databases available. Every chapter is grounded in tasks you will actually perform on the job: importing CSV files, cleaning inconsistent data, joining tables across sources, calculating statistical aggregates, and exporting results for reporting. The progression is deliberate β€” early chapters build the vocabulary, later chapters put it to work on problems that span multiple skills at once.

Beyond basic SELECT statements, you will learn how to use window functions for running totals and rankings, full-text search for document retrieval, and PostGIS for spatial queries that can answer questions like "which neighborhoods have the highest incident rates?" You will also learn how to maintain a database responsibly: indexing for performance, setting up transactions, and backing up your work.

  • Query real datasets including U.S. Census and crime statistics
  • Import and export data with COPY and external CSV files
  • Clean and standardize inconsistent source data
  • Join multiple tables and work with subqueries and CTEs
  • Use window functions for rankings and running aggregates
  • Write full-text search queries and spatial queries with PostGIS
  • Maintain indexes, transactions, and backups in PostgreSQL

At 466 pages, the book moves at a pace that respects your time. You can read it cover-to-cover or jump to the chapter that solves your current problem. Either way, you leave with working SQL skills tied to real analytical outcomes, not just memorized syntax.

🎯 What you'll learn

  • Design and create relational tables that enforce data integrity through constraints and proper data types.
  • Import real-world CSV datasets into PostgreSQL and handle encoding, formatting, and missing-value problems.
  • Write multi-table joins and correlated subqueries to answer questions that span more than one data source.
  • Apply aggregate functions, GROUP BY, and window functions to produce rankings, running totals, and statistical summaries.
  • Use CTEs and views to organize complex queries into readable, maintainable logic.
  • Query geographic data with PostGIS to perform distance calculations and spatial filtering.
  • Maintain database performance and reliability through indexing, transactions, and routine backup strategies.

πŸ‘€ Who is this book for?

  • Analysts and journalists who work with data files and spreadsheets but want to graduate to a real database for larger, more complex datasets.
  • Developers new to databases who need a practical foundation in SQL before integrating a PostgreSQL backend into an application.
  • Data professionals who learned just enough SQL to get by and want to fill the gaps β€” joins, aggregates, window functions β€” with structured practice.
  • Students in data science or statistics programs who need a hands-on SQL companion that goes beyond toy examples.
  • Business intelligence practitioners setting up PostgreSQL for the first time who want a reference they can also work through from the start.

Table of contents

  1. 01

    Setting Up Your Environment

    Install PostgreSQL and pgAdmin, create your first database, and run a simple query to confirm everything is working before any real data work begins.

  2. 02

    Beginning Data Exploration with SELECT

    Learn the core SELECT statement, filtering with WHERE, and sorting with ORDER BY by querying a provided teachers dataset to find meaningful patterns.

  3. 03

    Understanding Data Types

    Explore PostgreSQL's type system β€” integers, decimals, text, dates, and Booleans β€” and learn how choosing the right type protects your data and speeds up queries.

  4. 04

    Importing and Exporting Data

    Use PostgreSQL's COPY command to load CSV files into tables and export query results, working with a real U.S. Census dataset as the source material.

  5. 05

    Basic Math and Statistics in SQL

    Perform arithmetic operations inside queries and apply aggregate functions including SUM, AVG, and percentile calculations to summarize a dataset statistically.

  6. 06

    Joining Tables

    Connect related tables with INNER, LEFT, RIGHT, and FULL OUTER joins, then practice on school and government datasets to see how join type affects results.

  7. 07

    Table Design and Performance

    Apply naming conventions, set primary and foreign keys, create indexes, and learn how PostgreSQL's query planner uses indexes to speed up data retrieval.

  8. 08

    Extracting Information with Text Functions

    Clean and transform string data using pattern matching, regular expressions, and PostgreSQL's full-text search engine to find records by meaning rather than exact match.

Frequently asked questions

Do I need any prior SQL or database experience to read this book?

No prior SQL knowledge is assumed. The book starts from scratch with installation and basic queries. Comfort with general computer use and working in a terminal is helpful but not required.

Which database does the book use, and do I need a license?

All examples use PostgreSQL, which is free and open-source. The book walks you through installing it on Windows, macOS, or Linux in the first chapter.

Are the datasets used in the examples available to download?

Yes. The author provides the sample data files and SQL scripts referenced throughout the book so you can follow every exercise hands-on. Check the publisher's page for the download link.

Is this book suitable for someone who already knows basic SQL and wants to go further?

It works well as a structured refresher and extension. Chapters covering window functions, PostGIS, full-text search, and database maintenance cover material well beyond introductory SQL.

Does the second edition differ significantly from the first?

The second edition adds new chapters and updates all content for PostgreSQL 14, including expanded coverage of window functions, CTEs, and additional real-world datasets not present in the original.

You might also like

πŸ“¬ Weekly Newsletter

Stay ahead of the curve

Get the best programming tutorials, data analytics tips, and tool reviews delivered to your inbox every week.

No spam. Unsubscribe anytime.