Practical SQL
2nd Edition
A Beginners Guide to Storytelling with Data
by Anthony DeBarros
![PRACTICAL SQL 2ND EDITION Copyright 2022 by Anthony DeBarros All rights - photo 2](/uploads/posts/book/289332/Images/nsp_logo_black_rk.png)
PRACTICAL SQL, 2ND EDITION. Copyright 2022 by Anthony DeBarros.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
Printed in the United States of America
First printing
25 24 23 22 21 1 2 3 4 5 6 7 8 9
ISBN-13: 978-1-7185-0106-5 (print)
ISBN-13: 978-1-7185-0107-2 (ebook)
Publisher: William Pollock
Managing Editor: Jill Franklin
Production Manager: Rachel Monaghan
Production Editors: Jennifer Kepler and Paula Williamson
Developmental Editor: Liz Chadwick
Cover Illustrator: Josh Ellingson
Interior Design: Octopod Studios
Technical Reviewer: Stephen Frost
Copyeditor: Kim Wimpsett
Compositor: Maureen Forys, Happenstance Type-O-Rama
Proofreader: Liz Wheeler
For information on book distributors or translations, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900; info@nostarch.com
www.nostarch.com
The Library of Congress has catalogued the first edition as follows:
Names: DeBarros, Anthony, author.
Title: Practical SQL : a beginner's guide to storytelling with data / Anthony DeBarros.
Description: San Francisco : No Starch Press, 2018. | Includes index.
Identifiers: LCCN 2018000030 (print) | LCCN 2017043947 (ebook) | ISBN 9781593278458 (epub) | ISBN
1593278454 (epub) | ISBN 9781593278274 (paperback) | ISBN 1593278276 (paperback) | ISBN 9781593278458
(ebook)
Subjects: LCSH: SQL (Computer program language) | Database design. | BISAC: COMPUTERS / Programming
Languages / SQL. | COMPUTERS / Database Management / General. | COMPUTERS / Database Management
/ Data Mining.
Classification: LCC QA76.73.S67 (print) | LCC QA76.73.S67 D44 2018 (ebook) | DDC 005.75/6--dc23
LC record available at https://lccn.loc.gov/2018000030
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
About the Author
Anthony DeBarros is a longtime journalist and early adopter of data journalism, the use of spreadsheets, databases, and code to find news in data. Hes currently a data editor for the Wall Street Journal, where he covers topics including the economy, trade, demographics, and the Covid-19 pandemic. Previously, he worked for the Gannett company at USA Today and the Poughkeepsie Journal and held product development and content strategy roles for Questex and DocumentCloud.
About the Technical Reviewer
Stephen Frost is the chief technology officer at Crunchy Data. He has been working with PostgreSQL since 2003 and general database technology since before then. Stephen began contributing to PostgreSQL development in 2004 and has been involved in the development of the role system, column-level privileges, row-level security, GSSAPI encryption, and the predefined roles system. He has also served on the board of the United States PostgreSQL Association and Software in the Public Interest, regularly speaks at PostgreSQL Community conferences and events, and works as a member of various PostgreSQL community teams.
Preface to the Second Edition
Since the publication of the first edition of Practical SQL, Ive received kind notes about the book from readers around the world. One happy reader said it helped him ace SQL questions on a job interview. Another, a teacher, wrote to say that his students remarked favorably about having the book assigned for class. Others just wanted to say thanks because they found the book helpful and a good read, two pieces of feedback that will warm the heart of most any author.
I also sometimes heard from readers who hit a roadblock while working through an exercise or who had trouble with software or data files. I paid close attention to those emails, especially when the same question seemed to crop up more than once. Meanwhile, during my own journey of learning SQLI use it every day at workId often discover a technique and wish that Id included it in the book.
With all that in mind, I approached the team at No Starch Press with the idea of updating and expanding Practical SQL into a second edition. Im thankful they said yes. This new version of the book is more complete, offers stronger guidance for readers related to software and code, and clarifies information that wasnt as clear or presented as accurately as it could have been. The book has been thoroughly enjoyable to revisit, and Ive learned much along the way.
This second edition includes numerous updates, expansions, and clarifications in every chapter. Throughout, Ive been careful to note when code syntax adheres to the SQL standardmeaning you can generally use it across database systemsor when the syntax is specific to the database used in the book, PostgreSQL.
The following are among the most substantial changes:
- Two chapters are new. Chapter 1, Setting Up Your Coding Environment, details how to install PostgreSQL, pgAdmin, and additional PostgreSQL components on multiple operating systems. It also shows how to obtain the code listings and data from GitHub. In the first edition, this information was located in the introduction and occasionally missed by readers. Chapter 16, Working with JSON Data, covers PostgreSQLs support for the JavaScript Object Notation data format, using datasets about movies and earthquakes.
- In Chapter 4 on data types, Ive added a section on
IDENTITY
, the ANSI SQL standard implementation for auto-incrementing integer columns. Throughout the book, IDENTITY
replaces the PostgreSQL-specific serial
auto-incrementing integer type so that code examples more closely reflect the SQL standard. - Chapter 5 on importing and exporting data now includes a section about using the
WHERE
keyword with the COPY
command to filter which rows are imported from a source file to a table. - Ive removed the user-created
median()
function from Chapter 6 on basic math in favor of focusing exclusively on the SQL standard percentile_cont()
function for calculating medians. - In Chapter 7 on table joins, Ive added a section covering the set operators
UNION
, UNION ALL
, INTERSECT
, and EXCEPT
. Additionally, Ive added a section covering the USING
clause in joins to reduce redundant output and simplify query syntax.