Practical SQL

Book description

"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."

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. About the Author
  5. About the Technical Reviewer
  6. Brief Contents
  7. Contents in Detail
  8. Foreword by Sarah Frostenson
  9. Acknowledgments
  10. Introduction
    1. What Is SQL?
    2. Why Use SQL?
    3. About This Book
    4. Using the Book’s Code Examples
    5. Using PostgreSQL
    6. Wrapping Up
  11. 1. Creating Your First Database and Table
    1. Creating a Database
    2. Creating a Table
    3. Inserting Rows into a Table
    4. When Code Goes Bad
    5. Formatting SQL for Readability
    6. Wrapping Up
    7. Try It Yourself
  12. 2. Beginning Data Exploration with SELECT
    1. Basic SELECT Syntax
    2. Sorting Data with ORDER BY
    3. Filtering Rows with WHERE
    4. Putting It All Together
    5. Wrapping Up
    6. Try It Yourself
  13. 3. Understanding Data Types
    1. Characters
    2. Numbers
    3. Dates and Times
    4. Using the interval Data Type in Calculations
    5. Miscellaneous Types
    6. Transforming Values from One Type to Another with CAST
    7. CAST Shortcut Notation
    8. Wrapping Up
    9. Try It Yourself
  14. 4. Importing and Exporting Data
    1. Working with Delimited Text Files
    2. Using COPY to Import Data
    3. Importing Census Data Describing Counties
    4. Importing a Subset of Columns with COPY
    5. Adding a Default Value to a Column During Import
    6. Using COPY to Export Data
    7. Importing and Exporting Through pgAdmin
    8. Wrapping Up
    9. Try It Yourself
  15. 5. Basic Math and Stats With SQL
    1. Math Operators
    2. Doing Math Across Census Table Columns
    3. Aggregate Functions for Averages and Sums
    4. Finding the Median
    5. Finding the Mode
    6. Wrapping Up
    7. Try It Yourself
  16. 6. Joining Tables in a Relational Database
    1. Linking Tables Using JOIN
    2. Relating Tables with Key Columns
    3. Querying Multiple Tables Using JOIN
    4. JOIN Types
    5. Using NULL to Find Rows with Missing Values
    6. Three Types of Table Relationships
    7. Selecting Specific Columns in a Join
    8. Simplifying JOIN Syntax with Table Aliases
    9. Joining Multiple Tables
    10. Performing Math on Joined Table Columns
    11. Wrapping Up
    12. Try It Yourself
  17. 7. Table Design That Works for You
    1. Naming Tables, Columns, and Other Identifiers
    2. Controlling Column Values with Constraints
    3. Speeding Up Queries with Indexes
    4. Wrapping Up
    5. Try It Yourself
  18. 8. Extracting Information by Grouping and Summarizing
    1. Creating the Library Survey Tables
    2. Exploring the Library Data Using Aggregate Functions
    3. Wrapping Up
    4. Try It Yourself
  19. 9. Inspecting and Modifying Data
    1. Importing Data on Meat, Poultry, and Egg Producers
    2. Interviewing the Data Set
    3. Modifying Tables, Columns, and Data
    4. Deleting Unnecessary Data
    5. Using Transaction Blocks to Save or Revert Changes
    6. Improving Performance When Updating Large Tables
    7. Wrapping Up
    8. Try It Yourself
  20. 10. Statistical Functions in SQL
    1. Creating a Census Stats Table
    2. Creating Rankings with SQL
    3. Calculating Rates for Meaningful Comparisons
    4. Wrapping Up
    5. Try It Yourself
  21. 11. Working with Dates and Times
    1. Data Types and Functions for Dates and Times
    2. Manipulating Dates and Times
    3. Working with Time Zones
    4. Calculations with Dates and Times
    5. Wrapping Up
    6. Try It Yourself
  22. 12. Advanced Query Techniques
    1. Using Subqueries
    2. Common Table Expressions
    3. Cross Tabulations
    4. Reclassifying Values with CASE
    5. Using CASE in a Common Table Expression
    6. Wrapping Up
    7. Try It Yourself
  23. 13. Mining Text to Find Meaningful Data
    1. Formatting Text Using String Functions
    2. Matching Text Patterns with Regular Expressions
    3. Full Text Search in PostgreSQL
    4. Wrapping Up
    5. Try It Yourself
  24. 14. Analyzing Spatial Data with PostGIS
    1. Installing PostGIS and Creating a Spatial Database
    2. The Building Blocks of Spatial Data
    3. Two-Dimensional Geometries
    4. PostGIS Data Types
    5. Creating Spatial Objects with PostGIS Functions
    6. Analyzing Farmers’ Markets Data
    7. Working with Census Shapefiles
    8. Performing Spatial Joins
    9. Wrapping Up
    10. Try It Yourself
  25. 15. Saving Time with Views, Functions, and Triggers
    1. Using Views to Simplify Queries
    2. Programming Your Own Functions
    3. Automating Database Actions with Triggers
    4. Wrapping Up
    5. Try It Yourself
  26. 16. Using PostgreSQL from the Command Line
    1. Setting Up the Command Line for psql
    2. Working with psql
    3. Additional Command Line Utilities to Expedite Tasks
    4. Wrapping Up
    5. Try It Yourself
  27. 17. Maintaining Your Database
    1. Recovering Unused Space with VACUUM
    2. Changing Server Settings
    3. Backing Up and Restoring Your Database
    4. Wrapping Up
    5. Try It Yourself
  28. 18. Identifying and Telling the Story Behind Your Data
    1. Start with a Question
    2. Document Your Process
    3. Gather Your Data
    4. No Data? Build Your Own Database
    5. Assess the Data’s Origins
    6. Interview the Data with Queries
    7. Consult the Data’s Owner
    8. Identify Key Indicators and Trends over Time
    9. Ask Why
    10. Communicate Your Findings
    11. Wrapping Up
    12. Try It Yourself
  29. Appendix: Additional PostgreSQL Resources
    1. PostgreSQL Development Environments
    2. PostgreSQL Utilities, Tools, and Extensions
    3. PostgreSQL News
    4. Documentation
  30. Index

Product information

  • Title: Practical SQL
  • Author(s): Anthony DeBarros
  • Release date: May 2018
  • Publisher(s): No Starch Press
  • ISBN: 9781593278274