100% found this document useful (1 vote)
30 views

Download Complete Leveling Up with SQL: Advanced Techniques for Transforming Data into Insights Mark Simon PDF for All Chapters

into

Uploaded by

behrandjaka16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
30 views

Download Complete Leveling Up with SQL: Advanced Techniques for Transforming Data into Insights Mark Simon PDF for All Chapters

into

Uploaded by

behrandjaka16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Download the full version of the ebook at

https://ebookmass.com

Leveling Up with SQL: Advanced Techniques for


Transforming Data into Insights Mark Simon

https://ebookmass.com/product/leveling-up-with-
sql-advanced-techniques-for-transforming-data-
into-insights-mark-simon/

Explore and download more ebook at https://ebookmass.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Getting Started with SQL and Databases: Managing and


Manipulating Data with SQL Mark Simon

https://ebookmass.com/product/getting-started-with-sql-and-databases-
managing-and-manipulating-data-with-sql-mark-simon/

testbankdeal.com

Getting Started with SQL and Databases: Managing and


Manipulating Data with SQL 1st Edition Mark Simon

https://ebookmass.com/product/getting-started-with-sql-and-databases-
managing-and-manipulating-data-with-sql-1st-edition-mark-simon/

testbankdeal.com

Practical Graph Structures in SQL Server and Azure SQL:


Enabling Deeper Insights Using Highly Connected Data 1st
Edition Louis Davidson
https://ebookmass.com/product/practical-graph-structures-in-sql-
server-and-azure-sql-enabling-deeper-insights-using-highly-connected-
data-1st-edition-louis-davidson-2/
testbankdeal.com

Practical Graph Structures in SQL Server and Azure SQL:


Enabling Deeper Insights Using Highly Connected Data 1st
Edition Louis Davidson
https://ebookmass.com/product/practical-graph-structures-in-sql-
server-and-azure-sql-enabling-deeper-insights-using-highly-connected-
data-1st-edition-louis-davidson/
testbankdeal.com
Magical Midlife Awakening (Leveling Up Book 10) K.F.
Breene

https://ebookmass.com/product/magical-midlife-awakening-leveling-up-
book-10-k-f-breene/

testbankdeal.com

Data Universe: Organizational Insights with Python:


Embracing Data Driven Decision Making Van Der Post

https://ebookmass.com/product/data-universe-organizational-insights-
with-python-embracing-data-driven-decision-making-van-der-post/

testbankdeal.com

Data Engineering with dbt: A practical guide to building a


cloud-based, pragmatic, and dependable data platform with
SQL Zagni
https://ebookmass.com/product/data-engineering-with-dbt-a-practical-
guide-to-building-a-cloud-based-pragmatic-and-dependable-data-
platform-with-sql-zagni/
testbankdeal.com

Insights from Data with R: An Introduction for the Life


and Environmental Sciences Owen L. Petchey

https://ebookmass.com/product/insights-from-data-with-r-an-
introduction-for-the-life-and-environmental-sciences-owen-l-petchey/

testbankdeal.com

Advanced Data Analytics Using Python : With Architectural


Patterns, Text and Image Classification, and Optimization
Techniques 2nd Edition Sayan Mukhopadhyay
https://ebookmass.com/product/advanced-data-analytics-using-python-
with-architectural-patterns-text-and-image-classification-and-
optimization-techniques-2nd-edition-sayan-mukhopadhyay/
testbankdeal.com
Leveling Up
with SQL
Advanced Techniques for Transforming
Data into Insights

Mark Simon
Leveling Up with SQL
Advanced Techniques
for Transforming Data into Insights

Mark Simon
Leveling Up with SQL: Advanced Techniques for Transforming Data into Insights
Mark Simon
Ivanhoe VIC, VIC, Australia

ISBN-13 (pbk): 978-1-4842-9684-4 ISBN-13 (electronic): 978-1-4842-9685-1


https://doi.org/10.1007/978-1-4842-9685-1
Copyright © 2023 by Mark Simon
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Smriti Srivastava
Development Editor: Laura Berendson
Editorial Assistant: Mark Powers
Cover designed by eStudioCalamar
Cover image by Jigar Panchal on Unsplash (www.unsplash.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza,
Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@
springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole
member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc
is a Delaware corporation.
For information on translations, please e-mail booktranslations@springernature.com; for reprint,
paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub (github.com/apress). For more detailed information, please visit https://www.apress.
com/gp/services/source-code.
Paper in this product is recyclable
To Brian. You’re part of what I am today.
Table of Contents
About the Author��������������������������������������������������������������������������������������������������� xiii

About the Technical Reviewer���������������������������������������������������������������������������������xv


Acknowledgments�������������������������������������������������������������������������������������������������xvii

Introduction������������������������������������������������������������������������������������������������������������xix

Chapter 1: Getting Ready������������������������������������������������������������������������������������������ 1


About the Sample Database���������������������������������������������������������������������������������������������������������� 1
Setting Up������������������������������������������������������������������������������������������������������������������������������������� 3
Database Management Software�������������������������������������������������������������������������������������������� 3
Database Client����������������������������������������������������������������������������������������������������������������������� 4
The Sample Database�������������������������������������������������������������������������������������������������������������� 4
What You Probably Know Already������������������������������������������������������������������������������������������������� 5
Some Philosophical Concepts������������������������������������������������������������������������������������������������� 6
Writing SQL���������������������������������������������������������������������������������������������������������������������������� 10
Basic SQL������������������������������������������������������������������������������������������������������������������������������ 10
Data Types����������������������������������������������������������������������������������������������������������������������������� 11
SQL Clauses��������������������������������������������������������������������������������������������������������������������������� 12
Calculating Columns�������������������������������������������������������������������������������������������������������������� 15
Joins�������������������������������������������������������������������������������������������������������������������������������������� 17
Aggregates���������������������������������������������������������������������������������������������������������������������������� 18
Working with Tables�������������������������������������������������������������������������������������������������������������� 20
Manipulating Data����������������������������������������������������������������������������������������������������������������� 21
Set Operations����������������������������������������������������������������������������������������������������������������������� 21
Coming Up����������������������������������������������������������������������������������������������������������������������������������� 22

v
Table of Contents

Chapter 2: Working with Table Design�������������������������������������������������������������������� 25


Understanding Normalized Tables���������������������������������������������������������������������������������������������� 25
Columns Should Be Independent������������������������������������������������������������������������������������������������ 27
Adding the Towns Table��������������������������������������������������������������������������������������������������������� 28
Adding a Foreign Key to the Town����������������������������������������������������������������������������������������� 29
Update the Customers Table�������������������������������������������������������������������������������������������������� 30
Remove the Old Address Columns����������������������������������������������������������������������������������������� 32
Changing the Town���������������������������������������������������������������������������������������������������������������� 35
Adding the Country���������������������������������������������������������������������������������������������������������������� 36
Additional Comments������������������������������������������������������������������������������������������������������������ 38
Improving Database Integrity������������������������������������������������������������������������������������������������������ 38
Fixing Issues with a Nullable Column������������������������������������������������������������������������������������ 40
Other Adjustments����������������������������������������������������������������������������������������������������������������� 45
Adding Indexes��������������������������������������������������������������������������������������������������������������������������� 49
Adding an Index to the Books and Authors Tables����������������������������������������������������������������� 50
Creating a Unique Index�������������������������������������������������������������������������������������������������������� 52
Review���������������������������������������������������������������������������������������������������������������������������������������� 54
Normal Form������������������������������������������������������������������������������������������������������������������������� 54
Multiple Values���������������������������������������������������������������������������������������������������������������������� 55
Altering Tables����������������������������������������������������������������������������������������������������������������������� 55
Views������������������������������������������������������������������������������������������������������������������������������������� 55
Indexes���������������������������������������������������������������������������������������������������������������������������������� 56
The Final Product������������������������������������������������������������������������������������������������������������������ 56
Summary������������������������������������������������������������������������������������������������������������������������������������ 57
Coming Up����������������������������������������������������������������������������������������������������������������������������������� 58

Chapter 3: Table Relationships and Joins�������������������������������������������������������������� 59


An Overview of Relationships����������������������������������������������������������������������������������������������������� 60
One-to-Many Relationship���������������������������������������������������������������������������������������������������������� 61
Counting One-to-Many Joins������������������������������������������������������������������������������������������������� 64
The NOT IN Quirk������������������������������������������������������������������������������������������������������������������� 69
Creating a Books and Authors View��������������������������������������������������������������������������������������� 70

vi
Table of Contents

One-to-One Relationships����������������������������������������������������������������������������������������������������������� 72
One-to-Maybe Relationships������������������������������������������������������������������������������������������������� 72
Multiple Values���������������������������������������������������������������������������������������������������������������������������� 76
Many-to-Many Relationships������������������������������������������������������������������������������������������������ 77
Joining Many-to-Many Tables����������������������������������������������������������������������������������������������� 82
Summarizing Multiple Values������������������������������������������������������������������������������������������������ 84
Combining the Joins�������������������������������������������������������������������������������������������������������������� 86
Many-to-Many Relationships Happen All the Time���������������������������������������������������������������� 90
Another Many-to-Many Example������������������������������������������������������������������������������������������������ 90
Inserting into Related Tables������������������������������������������������������������������������������������������������������� 93
Adding a Book and an Author������������������������������������������������������������������������������������������������ 94
Adding a New Sale���������������������������������������������������������������������������������������������������������������� 98
Review�������������������������������������������������������������������������������������������������������������������������������������� 102
Types of Relationships��������������������������������������������������������������������������������������������������������� 103
Joining Tables���������������������������������������������������������������������������������������������������������������������� 103
Views����������������������������������������������������������������������������������������������������������������������������������� 104
Inserting into Related Tables����������������������������������������������������������������������������������������������� 104
Summary���������������������������������������������������������������������������������������������������������������������������������� 104
Coming Up��������������������������������������������������������������������������������������������������������������������������������� 104

Chapter 4: Working with Calculated Data������������������������������������������������������������� 105


Calculation Basics��������������������������������������������������������������������������������������������������������������������� 106
Using Aliases����������������������������������������������������������������������������������������������������������������������� 108
Dealing with NULLs������������������������������������������������������������������������������������������������������������� 113
Using Calculations in Other Clauses������������������������������������������������������������������������������������ 118
More Details on Calculations���������������������������������������������������������������������������������������������������� 122
Casting�������������������������������������������������������������������������������������������������������������������������������� 122
Numeric Calculations���������������������������������������������������������������������������������������������������������� 127
String Calculations�������������������������������������������������������������������������������������������������������������� 132
Date Operations������������������������������������������������������������������������������������������������������������������� 139
The CASE Expression���������������������������������������������������������������������������������������������������������������� 151
Various Uses of CASE���������������������������������������������������������������������������������������������������������� 152

vii
Table of Contents

Coalesce Is like a Special Case of CASE������������������������������������������������������������������������������ 154


Nested CASE Expression����������������������������������������������������������������������������������������������������� 155
Summary���������������������������������������������������������������������������������������������������������������������������������� 158
Aliases��������������������������������������������������������������������������������������������������������������������������������� 159
NULLs���������������������������������������������������������������������������������������������������������������������������������� 159
Casting Types���������������������������������������������������������������������������������������������������������������������� 159
Calculating with Numbers��������������������������������������������������������������������������������������������������� 159
Calculating with Strings������������������������������������������������������������������������������������������������������ 160
Calculating with Dates��������������������������������������������������������������������������������������������������������� 160
The CASE Expression����������������������������������������������������������������������������������������������������������� 160
Coming Up��������������������������������������������������������������������������������������������������������������������������������� 161

Chapter 5: Aggregating Data�������������������������������������������������������������������������������� 163


The Basic Aggregate Functions������������������������������������������������������������������������������������������������ 163
NULL������������������������������������������������������������������������������������������������������������������������������������ 166
Understanding Aggregates�������������������������������������������������������������������������������������������������������� 166
Aggregating Some of the Values����������������������������������������������������������������������������������������������� 170
Distinct Values��������������������������������������������������������������������������������������������������������������������� 170
Aggregate Filter������������������������������������������������������������������������������������������������������������������� 171
Grouping by Calculated Values�������������������������������������������������������������������������������������������������� 173
Grouping with CASE Statements����������������������������������������������������������������������������������������� 177
Revisiting the Delivery Status���������������������������������������������������������������������������������������������� 179
Ordering by Arbitrary Strings����������������������������������������������������������������������������������������������� 181
Group Concatenation����������������������������������������������������������������������������������������������������������������� 183
Summarizing the Summary with Grouping Sets����������������������������������������������������������������������� 185
Preparing Data for Summarizing����������������������������������������������������������������������������������������� 186
Combining Summaries with the UNION Clause������������������������������������������������������������������� 189
Using GROUPING SETS, CUBE, and ROLLUP������������������������������������������������������������������������� 195
Histograms, Mean, Mode, and Median�������������������������������������������������������������������������������������� 201
Calculating the Mean����������������������������������������������������������������������������������������������������������� 203
Generating a Frequency Table��������������������������������������������������������������������������������������������� 203
Calculating the Mode����������������������������������������������������������������������������������������������������������� 205

viii
Table of Contents

Calculating the Median�������������������������������������������������������������������������������������������������������� 207


The Standard Deviation������������������������������������������������������������������������������������������������������� 208
Summary���������������������������������������������������������������������������������������������������������������������������������� 209
Basic Aggregate Functions�������������������������������������������������������������������������������������������������� 209
NULLs���������������������������������������������������������������������������������������������������������������������������������� 210
The Aggregating Process����������������������������������������������������������������������������������������������������� 210
Aggregate Filters����������������������������������������������������������������������������������������������������������������� 210
GROUP BY���������������������������������������������������������������������������������������������������������������������������� 211
Mixing Subtotals������������������������������������������������������������������������������������������������������������������ 211
Statistics������������������������������������������������������������������������������������������������������������������������������ 212
Coming Up��������������������������������������������������������������������������������������������������������������������������������� 212

Chapter 6: Using Views and Friends��������������������������������������������������������������������� 213


Working with Views������������������������������������������������������������������������������������������������������������������� 214
Creating a View�������������������������������������������������������������������������������������������������������������������� 216
Using ORDER BY in MSSQL�������������������������������������������������������������������������������������������������� 219
Tips for Working with View�������������������������������������������������������������������������������������������������� 219
Table-Valued Functions������������������������������������������������������������������������������������������������������� 221
What Can You Do with a View?�������������������������������������������������������������������������������������������� 225
Caching Data and Temporary Tables����������������������������������������������������������������������������������������� 227
Computed Columns������������������������������������������������������������������������������������������������������������������� 231
Summary���������������������������������������������������������������������������������������������������������������������������������� 233
Views����������������������������������������������������������������������������������������������������������������������������������� 233
Table Valued Functions�������������������������������������������������������������������������������������������������������� 233
Temporary Tables���������������������������������������������������������������������������������������������������������������� 234
Coming Up��������������������������������������������������������������������������������������������������������������������������������� 234

Chapter 7: Working with Subqueries and Common Table Expressions���������������� 235


Correlated and Non-correlated Subqueries������������������������������������������������������������������������������ 239
Subqueries in the SELECT Clause��������������������������������������������������������������������������������������������� 243
Subqueries in the WHERE Clause���������������������������������������������������������������������������������������������� 246
Subqueries with Simple Aggregates����������������������������������������������������������������������������������� 246

ix
Visit https://ebookmass.com
now to explore a rich
collection of eBooks and enjoy
exciting offers!
Table of Contents

Big Spenders����������������������������������������������������������������������������������������������������������������������� 246


Last Orders, Please�������������������������������������������������������������������������������������������������������������� 249
Duplicated Customers��������������������������������������������������������������������������������������������������������� 251
Subqueries in the FROM Clause������������������������������������������������������������������������������������������������ 252
Nested Subqueries�������������������������������������������������������������������������������������������������������������� 255
Using WHERE EXISTS (Subquery)���������������������������������������������������������������������������������������������� 258
WHERE EXISTS with Non-correlated Subqueries���������������������������������������������������������������� 259
WHERE EXISTS with Correlated Subqueries������������������������������������������������������������������������ 259
WHERE EXISTS vs. the IN() Expression�������������������������������������������������������������������������������� 260
LATERAL JOINS (a.k.a. CROSS APPLY) and Friends������������������������������������������������������������������� 261
Adding Columns������������������������������������������������������������������������������������������������������������������ 263
Multiple Columns����������������������������������������������������������������������������������������������������������������� 265
Working with Common Table Expressions�������������������������������������������������������������������������������� 267
Syntax���������������������������������������������������������������������������������������������������������������������������������� 268
Using a CTE to Prepare Calculations����������������������������������������������������������������������������������� 269
Summary���������������������������������������������������������������������������������������������������������������������������������� 272
Correlated and Non-correlated Subqueries������������������������������������������������������������������������� 272
The WHERE EXISTS Expression������������������������������������������������������������������������������������������� 273
LATERAL JOINS (a.k.a. CROSS APPLY)��������������������������������������������������������������������������������� 273
Common Table Expressions������������������������������������������������������������������������������������������������� 273
Coming Up��������������������������������������������������������������������������������������������������������������������������������� 273

Chapter 8: Window Functions������������������������������������������������������������������������������� 275


Writing Window Functions�������������������������������������������������������������������������������������������������������� 276
Simple Aggregate Windows������������������������������������������������������������������������������������������������� 277
Aggregate Functions����������������������������������������������������������������������������������������������������������������� 279
Aggregate Window Functions and ORDER BY��������������������������������������������������������������������������� 284
The Framing Clause������������������������������������������������������������������������������������������������������������� 285
Creating a Daily Sales View������������������������������������������������������������������������������������������������� 287
A Sliding Window����������������������������������������������������������������������������������������������������������������� 288
Window Function Subtotals������������������������������������������������������������������������������������������������������ 290
PARTITION BY Multiple Columns������������������������������������������������������������������������������������������ 294

x
Table of Contents

Ranking Functions�������������������������������������������������������������������������������������������������������������������� 296


Basic Ranking Functions����������������������������������������������������������������������������������������������������� 297
Ranking with PARTITION BY������������������������������������������������������������������������������������������������� 300
Paging Results��������������������������������������������������������������������������������������������������������������������� 302
Working with ntile��������������������������������������������������������������������������������������������������������������������� 305
A Workaround for ntile��������������������������������������������������������������������������������������������������������� 307
Working with Previous and Next Rows������������������������������������������������������������������������������������� 309
Summary���������������������������������������������������������������������������������������������������������������������������������� 311
Window Clauses������������������������������������������������������������������������������������������������������������������ 311
Coming Up��������������������������������������������������������������������������������������������������������������������������������� 312

Chapter 9: More on Common Table Expressions�������������������������������������������������� 313


CTEs As Variables���������������������������������������������������������������������������������������������������������������������� 313
Setting Hard-Coded Constants�������������������������������������������������������������������������������������������� 314
Deriving Constants�������������������������������������������������������������������������������������������������������������� 316
Using Aggregates in the CTE����������������������������������������������������������������������������������������������������� 317
Finding the Most Recent Sales per Customer��������������������������������������������������������������������� 317
Finding Customers with Duplicate Names�������������������������������������������������������������������������� 319
CTE Parameter Names�������������������������������������������������������������������������������������������������������������� 320
Using Multiple Common Table Expressions������������������������������������������������������������������������������ 321
Summarizing Duplicate Names with Multiple CTEs������������������������������������������������������������ 322
Recursive CTEs������������������������������������������������������������������������������������������������������������������������� 325
Generating a Sequence������������������������������������������������������������������������������������������������������� 328
Joining a Sequence CTE to Get Missing Values������������������������������������������������������������������� 331
Daily Comparison Including Missing Days��������������������������������������������������������������������������� 333
Traversing a Hierarchy��������������������������������������������������������������������������������������������������������� 336
Working with Table Literals������������������������������������������������������������������������������������������������������� 342
Using a Table Literal for Testing������������������������������������������������������������������������������������������� 344
Using a Table Literal for Sorting������������������������������������������������������������������������������������������ 348
Using a Table Literal As a Lookup���������������������������������������������������������������������������������������� 351
Splitting a String������������������������������������������������������������������������������������������������������������������ 353

xi
Table of Contents

Summary���������������������������������������������������������������������������������������������������������������������������������� 364
Simple CTEs������������������������������������������������������������������������������������������������������������������������� 364
Parameter Names���������������������������������������������������������������������������������������������������������������� 365
Multiple CTEs����������������������������������������������������������������������������������������������������������������������� 365
Recursive CTEs�������������������������������������������������������������������������������������������������������������������� 365
Coming Up��������������������������������������������������������������������������������������������������������������������������������� 365

Chapter 10: More Techniques: Triggers, Pivot Tables, and Variables������������������� 367
Understanding Triggers������������������������������������������������������������������������������������������������������������� 368
Some Trigger Basics������������������������������������������������������������������������������������������������������������ 369
Preparing the Data to Be Archived�������������������������������������������������������������������������������������� 370
Creating the Trigger������������������������������������������������������������������������������������������������������������� 372
Pros and Cons of Triggers���������������������������������������������������������������������������������������������������� 380
Pivoting Data����������������������������������������������������������������������������������������������������������������������������� 381
Pivoting the Data����������������������������������������������������������������������������������������������������������������� 382
Manually Pivoting Data�������������������������������������������������������������������������������������������������������� 384
Using the Pivot Feature (MSSQL, Oracle)����������������������������������������������������������������������������� 389
Working with SQL Variables������������������������������������������������������������������������������������������������������ 394
Code Blocks������������������������������������������������������������������������������������������������������������������������� 395
Updated Code to Add a Sale������������������������������������������������������������������������������������������������ 396
Review�������������������������������������������������������������������������������������������������������������������������������������� 404
Triggers�������������������������������������������������������������������������������������������������������������������������������� 404
Pivot Tables�������������������������������������������������������������������������������������������������������������������������� 405
SQL Variables����������������������������������������������������������������������������������������������������������������������� 405
Summary���������������������������������������������������������������������������������������������������������������������������������� 406

Appendix A: Cultural Notes����������������������������������������������������������������������������������� 407

Appendix B: DBMS Differences����������������������������������������������������������������������������� 411

Appendix C: Using SQL with Python��������������������������������������������������������������������� 421

Index��������������������������������������������������������������������������������������������������������������������� 443

xii
About the Author
Mark Simon has been involved in training and education
since the beginning of his career. He started as a teacher
of mathematics, but quickly pivoted into IT consultancy
and training because computers are much easier to work
with than high school students. He has worked with and
trained in several programming and coding languages and
currently focuses mainly on web development and database
languages. When not involved in work, you will generally
find him listening to or playing music, reading, or just
wandering about.

xiii
About the Technical Reviewer
Aaditya Pokkunuri is an experienced senior cloud database
engineer with a demonstrated history of working in the
information technology and services industry with 13 years
of experience.
He is skilled in performance tuning, MS SQL Database
Server Administration, SSIS, SSRS, PowerBI, and SQL
development.
He possesses in-depth knowledge of replication,
clustering, SQL Server high availability options, and ITIL
processes.
His expertise lies in Windows administration tasks, Active Directory, and Microsoft
Azure technologies.
He also has extensive knowledge of MySQL, MariaDB, and MySQL Aurora database
engines.
He has expertise in AWS Cloud and is an AWS Solution Architect Associate and AWS
Database Specialty.
Aaditya is a strong information technology professional with a Bachelor of Technology
in Computer Science and Engineering from Sastra University, Tamil Nadu.

xv
Acknowledgments
The sample data includes information about books and authors from Goodreads
(www.goodreads.com/), particularly from their lists of classical literature over the past
centuries. Additional author information was obtained, of course, from Wikipedia
(www.wikipedia.org/).
The author makes no guarantees about whether the information was correct or even
copied correctly. Certainly, the list of books should not in any way be interpreted as an
endorsement or even an indication of personal taste. After all, it’s just sample data.

xvii
Introduction
In the early 1970s, a new design for managing databases was being developed based on
the original work of E. F. Codd. The underlying model was known as the relational model
and described a way of collecting data and accessing and manipulating data using
mathematical principles.
Over the decade, the SQL language was developed, and, though it doesn’t follow the
relational model completely, it attempts to make the database accessible using a simple
language.
The SQL language has been improved, enhanced, and further developed over the
years, and in the late 1980s, the language was developed into a standard of both ANSI
(the American National Standards Institute) and ISO (the International Organization for
Standardization, and, that’s right, it doesn’t spell ISO).
The takeaways from this very brief history are

• SQL has been around for some time.

• SQL is based on some solid mathematical principles.

• There is an official standard, even if nobody quite sticks to it.

• SQL is a developing language, and there are new features and new
techniques being added all the time.

The second half of the third point is worth stressing. Nobody quite sticks to the SQL
standards. There are many reasons for this, some good, some bad. But you’ll probably
find that the various dialects of SQL are about 80–90% compatible, and the rest we’ll fill
you in on as we go.
In this book, you’ll learn about using SQL to a level which goes beyond the basics.
Some things you’ll learn about are newer features in SQL; some are older features that
you may not have known about. We’ll look at a few non-standard features, and we’ll also
look at using features that you already know about, but in more powerful ways.
This book is not for the raw beginner—we assume you have some knowledge and
experience in SQL. If you are a raw beginner, then you will get more from my previous

xix
Introduction

book, Getting Started with SQL and Databases;1 you can then return to this book full of
confidence and enthusiasm with a good solid grounding in SQL.
If you have the knowledge and experience, the first chapter will give you a quick
overview of the sort of knowledge you should have.

The Sample Database


To work through the exercises, you’ll need the following:

• A database server and a suitable database client.

• Permissions to do anything you like on the database. If you’ve installed


the software locally, you probably have all the permissions you need,
but if you’re doing this on somebody else’s system, you need to check.

• The script which produces the sample database.

The first chapter will go into the details of getting your DBMS software and sample
database ready. It will also give you an overview of the story behind the sample database.

Notes
While you’re writing SQL to work with the data, there’s a piece of software at the other
end responding to the SQL. That software is referred to generically as a database server,
and, more specifically, as a DataBase Management System, or DBMS to its friends. We’ll
be using that term throughout the book.
The DBMSs we’ll be covering are PostgreSQL, MariaDB, MySQL, Microsoft SQL
Server, SQLite, and Oracle. We’ll assume that you’re working with reasonably current
versions of the DBMSs.
Chapter 1 will go into more details on setting up your DBMS, as well as downloading
and installing the sample database.

Source Code
All source code used in this book can be downloaded from ­github.com/apress/
leveling-up-sql.

1
https://link.springer.com/book/978148429494.

xx
CHAPTER 1

Getting Ready
If you’re reading this book, you’ll already know some SQL, either through previous study
or through bitter experience, or, more likely, a little of both. In the process, there may be
a few bits that you’ve missed, or forgotten, or couldn’t see the point.
We’ll assume that you’re comfortable enough with SQL to get the basic things
done, which mostly involves fetching data from one or more tables. You may even have
manipulated some of that data or even the tables themselves.
We won’t assume that you consider yourself an expert in all of this. Have a look in
the section “What You Probably Know Already” to check the sort of experience we think
you already have. If there are some areas you’re not completely sure about, don’t panic.
Each chapter will include some of the background concepts which should take you to
the next level.
If all of this is a bit new to you, perhaps we can recommend an introductory book. It’s
called Getting Started with SQL and Databases by Mark Simon, and you can learn more
about it at https://link.springer.com/book/10.1007/978-1-4842-9493-2.

About the Sample Database


For the sample database, we’re going to suppose that we’re running an online bookshop:
BookWorks. In this scenario

• Customers visit the website.

• At some point, customers will have registered with their details.

• They then add one or more copies of one or more books to a


shopping cart.

• Hopefully, they then check out and pay.

• BookWorks will then procure the books and ship them to customers
at some point.

1
© Mark Simon 2023
M. Simon, Leveling Up with SQL, https://doi.org/10.1007/978-1-4842-9685-1_1
Visit https://ebookmass.com
now to explore a rich
collection of eBooks and enjoy
exciting offers!
Chapter 1 Getting Ready

To manage all of this, the database tables look something like Figure 1-1.

Figure 1-1. The BookWorks Schema

In real life, there’s more to the story. For example, we haven’t included payment or
shipping methods, and we haven’t included login credentials. There’s no stock either,
although we’ll presume that the books are ordered on demand.
But there’s enough in this database for us to work with as we develop and improve
our SQL skills.

2
Chapter 1 Getting Ready

Setting Up
You can sit in a comfortable chair with a glass of your favorite refreshment and a box of
nice chocolates and read this book from cover to cover. However, you’ll get more from
this book if you join in on the samples.

Database Management Software


First, you’ll need access to Database Management Software (DBMS). The five and a half
DBMSs we work with in the book are

• PostgreSQL

• MariaDB/MySQL

• Microsoft SQL Server

• SQLite

• Oracle

PostgreSQL, MariaDB/MySQL, and SQLite are all free. Microsoft SQL Server and
Oracle are paid products, but have free versions.
MariaDB is a spin-off of MySQL, which is why they’re treated together. They are
almost identical in features, but you’ll find a few places where they’re not identical.

If you’re using MariaDB/MySQL, we’re going to assume that you’re running it in


ANSI mode. It’s easily done if you start the session with
SET SESSION sql_mode = 'ANSI';
You’ll probably see this message a few times throughout the book. The Appendix
will tell you why.

3
Random documents with unrelated
content Scribd suggests to you:
of which I had never entertained the slightest hope, my joyful
emotions at the bare idea, could only be equalled by my surprise at
such an intimation. It immediately struck me that the latter was the
result of preconcertion between Governor King and Mr. Marsden,
and that it was at the bottom, his Excellency’s wish to render me this
essential service. The sequel proved that my conjectures were not
unfounded, for it afterwards appeared that Governor King having an
incredible number of public documents lying in a state of great
confusion, and most of which he wanted copied prior to delivering
them up, it was indispensably necessary that he should have an
expeditious and experienced clerk, to transcribe and arrange such
papers, during his voyage home; and it seems he knew of no person
more eligible for such an employment than myself. I replied to Mr.
Marsden, with a warmth and earnestness which convinced him how
happy such an arrangement would make me, and assured him of my
everlasting gratitude towards himself and the late Governor for their
consideration; earnestly pressing Mr. Marsden to a speedy
“consummation” of what I so “devoutly wished.” The anxiety of mind
produced in me by this conversation may be conceived by the
reader, if he reflects upon the situation in which I was then placed;
for, though it was my chief and ardent wish to return to my native
land as soon as I became free, yet as I had no apparent prospect of
realizing money enough to pay my passage, and the hardships
incidental to working for it, would expose me to the most trying
severities, such an opportunity as the one I now contemplated, could
not be too highly appreciated, or too eagerly embraced.
CHAPTER XVII.
The Buffalo being ready for Sea, I receive an Intimation from Mr. Marsden, that the
late Governor has obtained a Remission of my unexpired Time, and consents
to take me home as his Clerk.—I wait on his Excellency accordingly, and
receive orders to go on board.—We set sail.

The process of fitting out and victualling the Buffalo, with other
causes, delayed the Governor’s departure for some weeks after the
appointed time. At length I received, about the first of February, in a
note from Mr. Marsden, (who with his family, had removed to Sydney,
in readiness for embarkation,) the agreeable tidings that Governor
King had consented to take me on board the Buffalo, in the capacity
of his private clerk, and had obtained the sanction of Governor Bligh
for my departure from the colony. Mr. Marsden concluded with
advising me to wait on Governor King without delay, for the purpose
of receiving his commands. I immediately hastened to Sydney, and
waited on his Excellency; who after some conversation, told me that
the Buffalo’s complement being already full, it was not in his power to
put me on the ship’s books[45], but that he would give me a passage,
and himself supply me with provisions. He then inquired if I was
incumbered with any debts; and, on my answering that I owed to
individuals about twenty pounds, and to Government, for various
articles from the stores, about fifteen pounds, his Excellency advised
me to take measures for settling these accompts, and, having so
done, to repair on board with the least possible delay. I now returned
to Parramatta, (having first given the usual notice of my departure for
publication in the Sydney Gazette,) and applied with diligence to the
settlement of my affairs. I was not displeased at the Governor’s
intimation respecting not putting me on the Buffalo’s books, because,
had he so done, it being war time, I might have found a difficulty in
getting clear of her on our arrival in England: on the other hand, by
going home as a passenger, although I should forego the advantage
of pay, for the time occupied in our voyage, yet I should be at liberty
to quit the ship the moment we anchored. I was not altogether so
easy with respect to my debts, as I feared my utmost means were
inadequate to their liquidation. ’Tis true, I had a watch, some good
clothes, and other effects, which altogether might suffice to
discharge my private debts, but then I should much distress myself
by the deprival of these useful articles. As to my Government debt, I
had a secret hope that the generosity of Governor King (which was
often conspicuous when his privy purse was not encroached upon,)
would induce him to assist me in surmounting that difficulty.
I lost not a moment in settling my business at Parramatta, and
returning to Sydney with all my worldly goods, I waited on my
principal creditor, with whom I managed to square the yards on
tolerably easy terms. I was obliged, however, to dispose of nearly all
my personal effects, before I could satisfy the private demands upon
me, retaining only a very limited stock of indispensable articles for
my voyage, and one genteel suit of clothes for the purpose of attiring
myself on quitting the ship, together with an English half-guinea to
bear my expenses from Portsmouth to London. Having taken these
measures, I embarked with my chest and bedding on the 5th of
February, Governor King and Mr. Marsden with their respective
families, being already on board. His Excellency hearing of my
arrival, sent for me to his cabin, and questioned me on the subject of
my debts. I informed him, that I had discharged every claim upon
me, except the sum I owed to the Crown, and confessed my inability
to liquidate that account, without some extension of his Excellency’s
kind assistance. The Governor after some moments’ reflection, wrote
a short memorandum, which he ordered me to take to Mr.
Commissary Palmer, to whom it was addressed. I accordingly went
on shore, and proceeding to the Commissary’s office, delivered the
note, which Mr. Palmer having read, handed to one of his clerks,
directing him to comply with the tenor of it, which was to the following
effect:—“James Vaux (the bearer) having behaved well in the
different situations of store-keeper’s, secretary’s, and magistrate’s
clerk, I think the small sum he stands indebted to Government ought
to be remitted him.” (Signed) “P. G. K.” The clerk having turned to my
account in the ledger, gave me credit therein, to the amount of my
debt, by an item worded as follows:—“By remuneration for services
as clerk, &c.” This point being adjusted, I now found myself perfectly
disembarrassed, and free to depart the country. With a joyful heart, I
took leave of my old acquaintances and fellow-clerks, with some of
whom I spent a convivial evening, and the next day I finally
embarked, all things being now in readiness, and the ship only
waiting the auspicious breeze. On the 10th of February, 1807, was
the happy day. At an early hour the ship was crowded with visiters,
comprising most of the principal officers of the colony, with their
ladies and friends, as well as many respectable inhabitants, all
anxious to testify their heartfelt respect towards a departing
Governor, whose general conduct had gained him universal esteem,
and who had, during a government of six years, united the
tenderness of a parent with the authority of a chief. The vessel was
surrounded with boats, some of which brought various friends of the
ship’s company, passengers, &c., who came to take a last farewell;
others contained spectators of both sexes, attracted by curiosity to
witness the interesting scene, as well as to shew their loyalty and
good wishes by joining in the parting cheers, with which the air
resounded as we cleared the heads of the harbour, to which distance
most of the boats accompanied us. About three o’clock P. M. the
breeze freshening, we quickly gained a good offing, and by half past
four a sudden squall, attended with rain and mist, obscured from us
all view of the land, which we beheld no more.
CHAPTER XVIII.
Receive a free Pardon from the hands of Captain (late Governor) King.—Account
of our Passage Home.—Suffer a great deal from the want of Provisions.—
Leaky State of the Ship.—Double Cape Horn.—Fall in with an English Frigate,
bound to the River Plate.—Arrive at Rio de Janeiro.

The morning after our sailing, Captain King, (as I shall in future
style him,) called me into his cabin, and presented me with a free
pardon, which, as he rightly observed, was merely a matter of form,
as it was probable we should not arrive in England before the
expiration of my time. This instrument, though only made out a few
days before, was ante-dated the 12th of August, 1806, being the day
before Governor Bligh assumed the command, and was
consequently the last official document, to which Governor King had
affixed his signature, and the last public act of his government.
In about a week, we made the island of New Zealand, at which it
was captain King’s intention to have touched, having some live pigs,
brought from Port Jackson, for the purpose of presenting to the
native chiefs, two of whom had lately visited, and resided for some
months in New South Wales; but this kind idea was frustrated by a
squall of wind, which took us aback while in sight of the island, and
night coming on, it was not considered prudent to approach the land;
we therefore hauled our wind, and shaped our course to the
southward.
Captain King had given orders to the ship’s steward, that I should
receive his (Captain King’s) own ration of provisions, on which I was
to subsist as well as I could. This quota (according to the usual
allowance of the navy,) would have been amply sufficient, but by
some unaccountable want of management, the Buffalo,
notwithstanding the length of time occupied in preparation, was at
last sent to sea with a very limited supply of provisions; nor was what
she really had of the kind or quality invariably allowed in the King’s
service; the consequence was, that on the very day of our sailing, all
hands were put upon half-allowance of bread, (that is, half a pound
per day,) and the other half of rice: this latter grain was also
substituted on banyan days for pease and for oatmeal: of cheese
and butter we were also destitute; so that we had not a prospect of
much good living before us. Of the rice, the crew soon became so
much tired, that they ceased to draw any from the purser, leaving it
in his hands, to be compensated for at the end of our voyage. The
salt-meat was also barely adequate to the computed length of our
passage to Rio de Janeiro, the first port at which we could hope for a
further supply. The reader will allow that half a pound of bread was
little enough for a man’s daily ration, (being just two biscuits) but ere
we had been six weeks at sea, it was found necessary to reduce the
allowance to one-third of a pound; and, in a few weeks more, it was
lowered to one quarter, that is, a biscuit a day! The necessity of
these reductions arose from a discovery that the rats, cockroaches,
and other vermin, with which the ship was infested, had made a
most destructive devastation in the bread-room, besides which, a
leak in the side had admitted so much salt-water, that a great many
bags of biscuit were totally spoiled, thereby adding to our distress,
which was before sufficiently great. Even the small portion of bread
we did receive, was scarcely eatable, most of it being very old
stores, and full of maggots, and what was baked in the colony being
of a very coarse quality, and every biscuit more or less excavated by
the vermin before-mentioned. The Buffalo, as I have before shewn,
was detained several months, for the purpose of being properly
victualled, and after all, the purser had been obliged to take a
quantity of wheat in casks, for consumption in some shape or other,
as it was found inexpedient to wait longer for a further supply of
biscuit. This wheat, when the crew were tired of rice, was boiled in
lieu of pease, and served out at dinner-time, but it was so
unpalatable a mess, that I, among many others, could never
stomach it, being thrown into the coppers, dust and all, boiled in
stinking water, and when taken up, it was nearly as hard as when
first put in, and much inferior to the food of pigs in England. The
ship’s company at length were convinced of their folly, in suffering
such a mode of cookery, or rather waste, for very few could eat it;
and, there being luckily a steel-mill on board, it was ordered to be put
up in the after gun-room, and every mess in the ship, in its turn,
ground the next day’s portion of wheat over night, and sifting the
meal, produced a bag of tolerable flour, of which the cook composed
a certain food for breakfast, known among sailors by the name of
skilligolee, being, in plain English, paste, similar to that used by the
sons of Crispin, only not quite of so thick a consistence: however, it
was much preferable to the hog-wash before-mentioned, and, with a
little sugar, served as an apology for a meal.
I have before observed, that I drew Captain King’s ration of
provisions, but he withheld the daily allowance of spirits, forming part
of the said ration, which he himself received, and deposited in his
private liquor-case. It is, however, probable that his lady, who was a
rigid economist, was the suggester of this practice. By way of
composition, this good lady gave directions that I should receive a
dram every day from her own steward. This trifling indulgence by the
by, was not ordered me, until the severity of the climate compelled
me to give a hint on the subject, and after about three week’s
continuance it was put a stop to, on pretence of the stock becoming
low. Upon the whole, I suffered a great deal from the scantiness, as
well as the quality of my allowance, and the want of certain comforts
to which I had been accustomed, particularly during the severe
weather which we encountered in our voyage round Cape Horn. It
soon appeared that I was not to eat the bread of idleness; I was
employed from morning till night in copying and arranging Captain
King’s papers, of which he had several large trunks full. I had also
the tuition of his daughter Miss King, who was about twelve years of
age, and performed the same duty towards Mr. Marsden’s two
children, as well as a native boy about fourteen, who had been
brought up in the family of Mr. Marsden. This youth had received the
rudiments of a good education, and discovered a most susceptible
genius. He could read and write tolerably well, was perfectly docile,
and well-behaved, and would doubtless have been an object of
general admiration, and reflected the highest credit on his humane
benefactors had he ever reached England, but unhappily during our
stay at Rio Janeiro, he absconded from the house of his protectors,
in consequence of some chastisement for misconduct, and,
notwithstanding every exertion was used, he could not be recovered
before the ship’s departure.
To add to our sufferings from the dearth of provisions, we had not
been a month at sea, before the ship was discovered to be in a very
leaky state, and these leaks increased so rapidly, that she at length
made above five feet water in the well, every four hours.
As we approached the higher latitudes, the climate became every
day more intensely cold, and we had continual tempestuous gales
for several weeks, but fortunately they were from the right quarter, so
that we were in general before the wind, and, considering that our
ship was a very heavy sailer, made a pretty rapid progress.
On the 10th of March, having reached a certain degree of
longitude, I witnessed the remarkable circumstance of gaining a day,
the consequence of circumnavigating the globe. The effect was, that
the following day, as well as this, was called in the ship’s log, the
10th of March, so that we had actually two Tuesdays in one week!
This was a favourable circumstance in one respect to myself and the
ship’s company, for as Tuesday is a sumptuous day in point of
allowance in the navy, beef and pudding being the prescribed fare
for dinner, we by this accident feasted two days together; whereas
had it occurred on a Monday, Wednesday, or Friday, two successive
banyan (or starvation) days would have been our dismal portion. On
our arrival in England our account of time after this alteration,
corresponded exactly with the almanack for the year.
On the 15th of March, we rounded Cape Horn, passing within five
miles of that inhospitable shore. This point of the American
continent, is situated in fifty-six degrees of south latitude, and had we
passed it in the winter season, instead of the autumn, the cold would
have been hardly bearable; as it was, it had the effect of destroying
almost every natural production of New South Wales, with some very
fine specimens of which our ship was at first literally crowded, so as
to resemble Noah’s Ark. There were kangaroos, black swans, a
noble emu, and cockatoos, parrots, and smaller birds without
number; all of which, except one cockatoo, which was carefully
nursed by its mistress, and half a dozen swans, fell victims to the
severity of the weather. The latter birds, indeed, being natives of Van
Diemen’s Land, which is a colder climate than Port Jackson, were of
a hardy nature, and survived our long and tedious voyage. On their
arrival in England, they were sent by Captain King as a present to
the Royal Menagerie in Kew-gardens.
In our passage round Cape Horn, we had frequent storms of
snow; a native of New Zealand, who had been for some time on
board the Buffalo, and was a very active intelligent fellow, expressed
the utmost surprise on observing a phenomenon, which in the happy
clime of his nativity is never experienced, and in the first emotions of
his admiration, exclaimed, “Look, look, white rain, white rain!” This
man would have been an object of curiosity in England, for he had a
very handsome person, and was punctured or tattooed in a most
fanciful and extraordinary manner from head to foot, including his
face, which was covered with ornamental devices; but unhappily he
contracted the small-pox, which, notwithstanding all possible
attention, carried him off before we arrived at the Brazils.
I must not omit a remarkable and awful phenomenon, which
occurred during this part of our passage. In the middle of a
tempestuous night, a sudden concussion was heard, resembling the
report of a cannon close to us; it alarmed three parts of the people
who were below asleep, as well as the watch on deck; and the
general opinion was at first, that a ship was close aboard us. Every
one hurried upon deck, when the cause was found to have been,
what is called a Fire-ball, which had been probably attracted by the
spindle at the mast-head, for it made a rapid descent down the main-
mast, exploded at the break of the quarter-deck; the concussion had
knocked down a quarter-master who stood near the binnacle, and
two men who were at the wheel. The former was for some minutes
insensible, but happily they were no further injured than by the fright.
Captain King was instantly on deck with his usual promptness, and
fearing the effects of the fire, gave orders to cover the magazine-
scuttle with wet-swabs; then to sound the pump-well, for some were
in dread of the ball having gone through the ship’s bottom: however,
it appeared the ship made only the usual quantity of water, and that
no real injury was sustained. Another night, as we were scudding
before a heavy gale of wind, and a tremendous sea rolling after us,
we had the misfortune to be pooped, as the phrase is, by a wave or
sea striking our stern, which stove in the cabin-windows, and rushing
impetuously through the cabin, and along the main-deck, bore down
all before it. Luckily no farther damage was done than breaking the
windows, which rendered it necessary to put up the dead-lights
during the remainder of the passage.
When within a day or two’s sail of the River Plate, as we were
running down the coast of Brazil, we fell in with a frigate, which, on
hailing, we found to be His Majesty’s ship Thisbe, bound to Buenos
Ayres, having on board, General Whitelocke and his staff. This
officer was going to conduct the attack upon the city above
mentioned, as well as upon Monte Video, both belonging to the
Spaniards. A formidable expedition had been fitted out in England for
the service, and a numerous army was already collected in the River
Plate, waiting the arrival of General Whitelocke. Captain King
complimented this officer with a visit on board the Thisbe, which the
latter returned. On arriving abreast of the River Plate, the Thisbe
parted from us, steering for the entrance of that river, and we
continued our course along-shore. In a few days we came in sight of
Rio de Janeiro, but were becalmed for two days in sight of the port.
At length, on the 22d of May, we entered that beautiful harbour, after
a most hazardous and blowing passage of fifteen weeks. It was
remarkable that on the day before we got in, the last biscuit was
issued to the ship’s company, and the last cask of salt-meat we
possessed, was broached; so that had our passage been longer
protracted, we must have endured still greater privations.
CHAPTER XIX.
Account of my Adventures at San Sebastian.—Form acquaintance with a
Portuguese Family.—Their Affection for me.—Overtures made to induce my
Stay in South America;—The Ship being repaired and victualled, we re-
embark and sail for Europe.

On coming to an anchor in the usual situation, we were boarded


by several persons of distinction, accompanied by an intelligent
gentleman as interpreter; after a conference between whom and
Captain King, it was determined that the latter should without delay
address an official letter to the Viceroy, stating the causes of his
putting into that port, the nature of his wants, and other particulars.
Captain King accordingly sketched a draught, which I was employed
to transcribe. This letter explained to his Excellency (the Viceroy) the
leaky and disabled state of His Majesty’s ship; her want of provisions
and water, &c. &c.; and concluded with expressing Captain King’s
willingness to compliment the Portuguese flag, by firing a salute of
fifteen guns, provided the Viceroy would pledge himself to return an
equal number. It is to be observed, that among the Portuguese,
every thing is conducted with the utmost formality; and as they are
highly jealous of foreigners, Captain King (whose experience was
very great in such matters) was particularly cautious not to give any
cause of offence, or to fail in due respect; at the same time that he
was determined to support the honour of his own nation, by
stipulating for an equal interchange of ceremony, conformable to the
etiquette of the service. No foreign vessels are suffered to proceed
beyond a certain distance within the harbour, without special
permission. One object of request in the letter was therefore, that the
Buffalo might be allowed to anchor above the Island of Cobres, (or
Snake Island) where she could have the benefit of smooth water to
undergo the necessary repairs. Other objects were, to be furnished
with shipwrights, caulkers, &c., to have a new rudder (our own being
sprung) prepared in the dock-yard, as also a new boltsprit from the
same cause, permission to land and house the rigging and other
stores in convenient receptacles; a daily supply of fresh meat,
vegetables, &c., and several other similar demands. To this letter a
most gracious answer was returned, assuring Captain King of the
fullest compliance with his requisitions, and expressing every
sentiment of friendship for the English nation, and of respect for his
person. His Excellency’s letter concluded with the usual compliment
of the Portuguese “God preserve your Excellency many years.” At
the same time was transmitted a long table of rules and ceremonies,
to be observed by our ship’s company, &c. These rules were
promulgated by Captain King, who ordered the strictest attention to
be paid thereto. They related to the times and places of landing from
boats; the behaviour of the English when on shore; respect to be
paid to the Catholic religion and its ministers, &c. &c. Among other
injunctions it was expected that every Englishman on passing a
church should move his hat, and pay the same compliment to the
Image of the Virgin Mary, which you encounter at the corner of every
principal street, enclosed in a sort of cupboard with a glass-door, and
silk-curtains drawn open on each side. These cupboards are affixed
to the wall, on a level with the first-floor windows of the house, and at
night a lamp is kept burning over them, which has a very pretty
effect. It was also required that every person, on meeting the Host
(which is a grand procession of priests, friars, &c., followed by an
immense rabble) should bend on one knee, and so remain till the
procession was past. The priests, on this occasion, carry gold or
silver images of our Saviour and the saints, accompanied with
musical instruments, flags, numerous lighted flambeaus, and every
other device to render the scene at once grand, solemn, and
impressive. In fact, I witnessed various processions of a religious
nature during my stay here, scarcely a night passing without one or
more; but it is foreign to my purpose, to enter into more minute
details. It being the first Catholic country I was ever in, I was struck
with admiration at the grandeur of the churches, the nightly
exhibitions of fire-works, illuminations, ringing of bells, and other
demonstrations of religious respect, or what we should call
superstition. But I am of opinion there is much policy in carrying
these rites to such an extravagant pitch, for the population of this city
being composed of negroes, in the proportion, I take it, of at least
nineteen to one white, and the majority of the former being slaves,
the splendour and enchanting effects of music, paintings, fire-works,
processions, and other devices, together with the implicit devotion in
which they are brought up to the Catholic religion, keep their minds
in a continual state of resigned subjection, amuse their senses, and
leave neither time, nor inducement to attempt, by acts of rebellion, a
release from their habitual bondage.
To resume my narrative: all preliminaries being settled to the
satisfaction of both parties, Captain King, a few days after our arrival,
hired a commodious and handsome house, ready-furnished, in a
pleasant part of the town, to which he with his whole family removed.
I, of course, accompanied them, highly delighted at the prospect of
viewing this beautiful city, and observing the manners and customs
of its inhabitants, from which I anticipated the greatest gratification.
Mr. Marsden and his family, also occupied a genteel house, and in
fact, all the passengers (of whom we had a good number) quitted the
ship, which was intended to undergo a thorough repair.
During our residence ashore, I had much leisure time, which I
employed in walking throughout the city and suburbs, viewing the
churches, convents, and other objects deserving of notice; and I
endeavoured to acquire, by every means opportunity offered, some
little knowledge of the Portuguese language. As the latter bears a
great affinity to the Latin and French, I succeeded beyond my hopes
in this object, by virtue of which I was enabled to extend my
inquiries, and to interchange personal civilities with many of the
inhabitants.
In the street in which we resided, I had frequently observed an old
lady, together with three young ones, sitting at the door of a genteel-
looking house, employed, according to their custom, in weaving lace,
with a number of bobbins, and a cushion placed before them. This
employment serves both as an amusement, and a source of profit to
the younger branches of families.—They were seated cross-legged
on the floor of the parlour, the door of which, (composed of lattice-
work) was open for the benefit of air. In the evenings I generally saw
them seated in the same manner, in conversation, the old lady
turning over her beads, of which a string, or rosary, with a cross
suspended to the end of it, was hung upon her wrist, and appeared
to receive her devout attention. Having repeatedly passed this
interesting group, and fancying that they noticed me with some
emotion, I ventured to compliment them en passant, which courtesy
they returned with evident pleasure; and every succeeding salutation
produced a longer dialogue than the preceding; till at length the old
lady one evening invited me to enter, and sit down; but not on the
floor, for they had chairs, as well as other furniture, of an elegant but
simple kind. These good ladies put several questions to me
respecting the English Governor (as they styled him) my situation in
his family, name, &c. I informed them, as fully as I could find words
for; and telling them my name in Portuguese, they ever afterwards
called me Senor Jacobe, it not being their custom to use surnames. I
had the pleasure to find that we understood each other tolerably
well, and gathered from them, that the old lady was called Senora
Donna Joaquima Roza de Lacè; (as the Vicar of Wakefield says, I
love to give the name at full length;) she was the widow of a military
officer in the Portuguese service, and enjoyed a pension from the
government, upon which, and the earnings of her accomplished
daughters, she lived in a private, but genteel manner: the latter were
named Donna Anna Precioza, Donna Joaquima, and Donna
Joanina. The mother was a most agreeable woman, courteous and
affable to a degree, but seemingly a strong devotee to her religion.
The daughters were beautiful girls, evidently of a susceptible
disposition; but, like the Portugueze in general, very reserved, and
tenacious of their sex’s dignity. They pressed me to partake of some
coffee, which was served in with much ceremony by a female negro;
and after two or three hours most agreeably spent, I took leave, with
an earnest injunction to repeat my visit.
From this time hardly a day passed without my calling on this
worthy family, and at every visit we parted with a stronger regard for
each other. I felt a growing sentiment in favour of the eldest daughter
(Anna,) and I ingratiated myself with the mother, by warmly praising
the Catholic religion, shewing my respect by kissing the cross
attached to her beads, and every other means in my power. In a few
days I became, as it were, one of the family, visiting them at all
hours, and experiencing the kindest welcome. The ladies took a
pleasure in improving my knowledge in their language, and in return
requested me to teach them my own. At this period there were very
few English persons in the Brazils; but the inhabitants, in general,
evinced the greatest fondness for our nation; so that not the meanest
of our ship’s company could walk the streets without being gratified
with hearing the exclamation of “Sta bon Inglise,” (very good
Englishman,) from the mouths of passengers, both men, women,
and children. Myself and other persons, whose appearance was at
all respectable, were also very courteously saluted by the females,
who stand for hours peeping through the lattice-windows of the
houses, which are half opened, so as to give them a view, and
although most of these diffident ladies close the lattice and retire with
precipitation, on the approach of a male stranger, yet many of them
suffered me to hold a conversation for a few minutes; and I
sometimes prevailed so far as to obtain one of their fair hands to
kiss, a mark of great favour and condescension, even to their
common acquaintances.
Captain King received every possible mark of respect both public
and private, during his residence in this city. Whenever he appeared
abroad in an open chaise, (which he hired by the month,) or on
horseback, the military-guard at every public post which he passed,
were turned out to pay him the same honours as are usually
rendered to the viceroy himself. The latter officer also shewed him
every attention, frequently exchanging visits with him, lending him
his own horses for a ride, &c. Most of the principal inhabitants paid
their respects to Mrs. King, and invited her to various public and
private entertainments. The Portuguese excel in music, particularly
the violin and bass-viol; and there were frequently large parties of
amateurs at our house, who formed a most delightful evening
concert. This is, in fact, the custom of all people of fashion there,
who, instead of meeting for the purpose of gaming as in London,
adopt this much more laudable and rational mode of amusement.
I observed, as I traversed the city, that a number of bonfires were
lighted in various streets, as soon as it grew dusk, almost every
evening, which were surrounded by children, negroes, and others,
rejoicing until a late hour. As these fires were never twice in the
same spot, I was at a loss to understand their meaning, until I at
length ascertained that they were in celebration of the birth-days of
children. The custom of observing such days differs, however, from
our own country; for instead of taking place on the anniversary of the
child’s nativity, the observation is confined to the festival of that
particular Saint after whom the child is named; for instance, on St.
Anna’s day, all persons who have a daughter called Anna, celebrate
the same by regaling their friends, and having a bonfire before their
door. As almost every day in the year is dedicated to a male or
female saint, whose name is borne by some hundreds of children,
these bonfires are, of course, frequent and numerous. The effect of
them is both lively and gratifying, especially to those who know their
cause; but many of the streets being extremely narrow, and some of
the fires very large, I have been frequently much embarrassed to
effect a passage, in which attempt I was sometimes in danger of
being scorched by the intense heat reflected from the stone walls of
which the houses are composed.
Another object of my attention was the slave-market, which is held
on the arrival of a Guinea ship, in the suburbs of the city. These
unfortunate beings are chained together, and driven to the scene of
this disgraceful traffic, where they are sold like cattle or sheep in
Smithfield. A healthy youth of about fifteen will produce from about
thirty to fifty pounds, this being considered the most advantageous
age at which a slave can be purchased. The condition of this
unhappy class of persons is, however, much more tolerable in this
city than a stranger would suppose. Most of the inhabitants possess
a male or female, (some several,) exclusive of those employed in
household affairs. These superfluous slaves are sent out in a
morning to ply for hire on their owner’s account, and obtain as much
as they can by their labour; the majority of the women confine
themselves to carrying water from the fountains, which they cry as
the venders of milk do in London; and on being hailed, they stop and
empty their vessel, for which they receive a vintain, (about three
halfpence;) they then return to the nearest fountain to replenish. I
could not help being surprised at the large size of the vessels in
which they carry this water on their heads; they are a kind of wooden
casks, some of them holding at least ten gallons.
Having mentioned the public fountains, (with which this city
abounds,) it may not be amiss to describe them. There being but few
springs in the town, water is conveyed by means of a noble brick
aqueduct (communicating with others,) from a distance of several
miles through the interior, over the summits of many high hills, and
being distributed throughout the city, it supplies a fountain in most of
the principal streets. These fountains are handsomely constructed of
stone, having a large cistern into which the purest water is constantly
running from the beaks of several ducks, geese, or other birds,
which are of cast metal, and well executed. As the trade of water-
carrying furnishes employment for many hundreds of negro women,
and their continual resort to the fountains would occasion much
confusion and quarrelling, a soldier is placed in attendance to
preserve order, and the women seat themselves in a row on a long
bench fixed on purpose, where they are obliged to wait for their turn
to fill. It is a curious and interesting sight to observe these harmless
and industrious creatures conversing with each other, and smoking
their pipes, while they patiently await the sentinel’s intimation to
approach. The incessant prattle of their tongues may be heard by a
passenger long before he reaches the spot, and adds to the novelty
of the scene.
The negroes I saw here, the majority of whom are natives, born of
those imported from Africa, or of their descendants, are upon the
whole the most comely race of blacks I ever met with. The men wear
coarse jackets and trowsers, the women a sort of gown of blue cloth;
but their linen is always remarkably white, of a fine texture, and
beautifully wrought with ornamental work about the sleeves and
bosom. They have also bracelets both on the wrists and ancles, and
ear-rings, all of the purest gold. Both sexes are very cleanly in their
persons, and of a most docile and gentle disposition.
By the kindness of my worthy friends, the ladies, I obtained
frequent admission to the Opera, (as it is called, though the pieces I
saw appeared to be principally comedies and farces, with scarcely
any vocal music.) As I could not understand the plot, or dialogue, my
chief satisfaction was in contemplating the coup d’œil, which was
very striking. The house is about the size of our little theatre in the
Haymarket; the pit is solely occupied by the male sex, but the boxes
are filled with beautiful women, dressed in the most elegant and
bewitching manner, and adorned with brilliant ear-rings, bracelets,
&c. The viceroy has an elegant box in the centre of the front tiers, so
as to be immediately facing the stage, and to be seen by the whole
audience. The performance is never suffered to commence till his
arrival, which is sometimes protracted; and on his Excellency’s
entrance, the whole of the spectators rise to testify their respect for
his person. The admission to the pit is only a petack (two shillings.)
The boxes are engaged by parties, at the expense of about twelve
shillings each, whether the party is large or small; and there is no
gallery. The music is excellent and the band numerous.
Were I to attempt a minute description of the magnificent
churches, convents, and other buildings, the numerous processions,
public rejoicings, &c.; the beautiful gardens, romantic walks, and
other objects worthy of the most scrupulous attention, which I met
with in this rich and flourishing city, as well as the various customs
and manners peculiar to its inhabitants, I should swell this work
much beyond the limits I have prescribed; but as I am inadequate to
the task, I shall leave such a subject to some abler pen; and as I only
profess to write an account of my own adventures, shall resume the
thread of my narrative.
I was so much delighted with all I saw, and found the climate so
congenial to my constitution, that I could have been content to have
spent the rest of my life in South America; and hinting as much to my
kind friends, the old lady seriously pressed me to that measure,
assuring me of her protection and interest, together with that of her
friends, who were numerous and respectable. She even pledged
herself to procure me a salary of one doubloon (about £3. 12s.) per
month, at the first onset, as clerk in a mercantile house, which would
be increased as I acquired the Portugueze language. But that
longing desire to re-visit my native country, which is predominant in
the breast of every Englishman, together with a wish to see once
more my beloved relations to whom I owed so much, prevailed over
every other consideration; and I declined, but with a proper sense of
gratitude, these flattering offers of favour and service.
Since my departure from Portsmouth, in 1801, I had not heard a
syllable of or from any of my friends in England, and though it was
probable that my dear benefactors in S⸺ had ere this period paid
the debt of nature, I felt a secret hope that they might still survive,
and that from the favourable situation in which I stood with Captain
King I might, at no very distant period, find myself enabled to
contribute some little assistance towards rendering the evening of
their lives happy. It was my firm determination never again to swerve
from a course of rectitude; and I had the fullest confidence that the
late governor, my patron, would procure me the means of adhering
to so laudable a resolution. How fatally I was mistaken, how
uncertain are the favours of the great, will be seen in a future
chapter.
The Buffalo having been as effectually repaired as circumstances
would admit, and a tolerable supply of provisions received on board,
every preparation was made for prosecuting our voyage, and a day
appointed for sailing. As the latter period approached, I redoubled
my respectful attentions to the worthy family in the Strada de los
Mongares, (or Monkey-street,) and they every day gave stronger
proofs of their concern at my approaching departure. The day at
length arrived, and I waited on them to take a final leave. On this
occasion many tears were shed on their part, and I confess my
sensibility was such that I involuntarily caught the soft infection. I
obtained the favour of a warm embrace from them all, and the young
ladies condescended to deviate from their usual strict decorum by
suffering me to imprint some ardent kisses on their charming lips.
The mother appeared a little disconcerted at this indulgence, but her
affection for me prevented any expressions of displeasure; she
herself presented me her cheek with a very good grace. I must here
observe, that the Portugueze ladies do not allow even their lovers,
before marriage, a greater license than kissing the hand; this
intimation was given me on my first acquaintance with Donna
Joaquima, by that worthy matron herself, with a very serious air, as
she discovered, I suppose, that I evinced too much warmth in my
attention to her lovely daughters. After numerous repetitions of
“Adieu” on my side, and of “Dios guarda, mio Amigo,” (God preserve
you, my friend,) on that of these amiable females, I was obliged to
tear myself abruptly from them; and with a heart overcharged with
grief and tenderness, I hastened to the boat, which conveyed myself
and the few other persons remaining on shore, to the ship, which
was then under weigh, and, the breeze freshening, we with difficulty
got alongside her, when abreast of Fort Santa Cruz, (near the
entrance of the Rio de Janeiro,) and just as the Buffalo was saluting
that battery. In about an hour afterwards, we cleared the Heads; the
same evening (August the 12th 1807,) we lost sight of the coast of
Brazil, and proceeded on our course for Europe.

You might also like