Renumber chapters and listing numbers per new book order
This commit is contained in:
parent
b452769f96
commit
7189b2c405
@ -2,14 +2,14 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 1 Code Examples
|
-- Chapter 2 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Listing 1-1: Creating a database named analysis
|
-- Listing 2-1: Creating a database named analysis
|
||||||
|
|
||||||
CREATE DATABASE analysis;
|
CREATE DATABASE analysis;
|
||||||
|
|
||||||
-- Listing 1-2: Creating a table named teachers with six columns
|
-- Listing 2-2: Creating a table named teachers with six columns
|
||||||
|
|
||||||
CREATE TABLE teachers (
|
CREATE TABLE teachers (
|
||||||
id bigserial,
|
id bigserial,
|
||||||
@ -23,7 +23,7 @@ CREATE TABLE teachers (
|
|||||||
-- This command will remove (drop) the table.
|
-- This command will remove (drop) the table.
|
||||||
-- DROP TABLE teachers;
|
-- DROP TABLE teachers;
|
||||||
|
|
||||||
-- Listing 1-3 Inserting data into the teachers table
|
-- Listing 2-3 Inserting data into the teachers table
|
||||||
|
|
||||||
INSERT INTO teachers (first_name, last_name, school, hire_date, salary)
|
INSERT INTO teachers (first_name, last_name, school, hire_date, salary)
|
||||||
VALUES ('Janet', 'Smith', 'F.D. Roosevelt HS', '2011-10-30', 36200),
|
VALUES ('Janet', 'Smith', 'F.D. Roosevelt HS', '2011-10-30', 36200),
|
||||||
|
|||||||
@ -2,41 +2,41 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 2 Code Examples
|
-- Chapter 3 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Listing 2-1: Querying all rows and columns from the teachers table
|
-- Listing 3-1: Querying all rows and columns from the teachers table
|
||||||
|
|
||||||
SELECT * FROM teachers;
|
SELECT * FROM teachers;
|
||||||
|
|
||||||
-- Listing 2-2: Querying a subset of columns
|
-- Listing 3-2: Querying a subset of columns
|
||||||
|
|
||||||
SELECT last_name, first_name, salary FROM teachers;
|
SELECT last_name, first_name, salary FROM teachers;
|
||||||
|
|
||||||
-- Listing 2-3: Querying distinct values in the school column
|
-- Listing 3-3: Querying distinct values in the school column
|
||||||
|
|
||||||
SELECT DISTINCT school
|
SELECT DISTINCT school
|
||||||
FROM teachers;
|
FROM teachers;
|
||||||
|
|
||||||
-- Listing 2-4: Querying distinct pairs of values in the school and salary
|
-- Listing 3-4: Querying distinct pairs of values in the school and salary
|
||||||
-- columns
|
-- columns
|
||||||
|
|
||||||
SELECT DISTINCT school, salary
|
SELECT DISTINCT school, salary
|
||||||
FROM teachers;
|
FROM teachers;
|
||||||
|
|
||||||
-- Listing 2-5: Sorting a column with ORDER BY
|
-- Listing 3-5: Sorting a column with ORDER BY
|
||||||
|
|
||||||
SELECT first_name, last_name, salary
|
SELECT first_name, last_name, salary
|
||||||
FROM teachers
|
FROM teachers
|
||||||
ORDER BY salary DESC;
|
ORDER BY salary DESC;
|
||||||
|
|
||||||
-- Listing 2-6: Sorting multiple columns with ORDER BY
|
-- Listing 3-6: Sorting multiple columns with ORDER BY
|
||||||
|
|
||||||
SELECT last_name, school, hire_date
|
SELECT last_name, school, hire_date
|
||||||
FROM teachers
|
FROM teachers
|
||||||
ORDER BY school ASC, hire_date DESC;
|
ORDER BY school ASC, hire_date DESC;
|
||||||
|
|
||||||
-- Listing 2-7: Filtering rows using WHERE
|
-- Listing 3-7: Filtering rows using WHERE
|
||||||
|
|
||||||
SELECT last_name, school, hire_date
|
SELECT last_name, school, hire_date
|
||||||
FROM teachers
|
FROM teachers
|
||||||
@ -69,7 +69,7 @@ SELECT first_name, last_name, school, salary
|
|||||||
FROM teachers
|
FROM teachers
|
||||||
WHERE salary BETWEEN 40000 AND 65000;
|
WHERE salary BETWEEN 40000 AND 65000;
|
||||||
|
|
||||||
-- Listing 2-8: Filtering with LIKE AND ILIKE
|
-- Listing 3-8: Filtering with LIKE AND ILIKE
|
||||||
|
|
||||||
SELECT first_name
|
SELECT first_name
|
||||||
FROM teachers
|
FROM teachers
|
||||||
@ -79,7 +79,7 @@ SELECT first_name
|
|||||||
FROM teachers
|
FROM teachers
|
||||||
WHERE first_name ILIKE 'sam%';
|
WHERE first_name ILIKE 'sam%';
|
||||||
|
|
||||||
-- Listing 2-9: Combining operators using AND and OR
|
-- Listing 3-9: Combining operators using AND and OR
|
||||||
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM teachers
|
FROM teachers
|
||||||
@ -96,7 +96,7 @@ FROM teachers
|
|||||||
WHERE school = 'F.D. Roosevelt HS'
|
WHERE school = 'F.D. Roosevelt HS'
|
||||||
AND (salary < 38000 OR salary > 40000);
|
AND (salary < 38000 OR salary > 40000);
|
||||||
|
|
||||||
-- Listing 2-10: A SELECT statement including WHERE and ORDER BY
|
-- Listing 3-10: A SELECT statement including WHERE and ORDER BY
|
||||||
|
|
||||||
SELECT first_name, last_name, school, hire_date, salary
|
SELECT first_name, last_name, school, hire_date, salary
|
||||||
FROM teachers
|
FROM teachers
|
||||||
|
|||||||
@ -2,10 +2,10 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 3 Code Examples
|
-- Chapter 4 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Listing 3-1: Character data types in action
|
-- Listing 4-1: Character data types in action
|
||||||
|
|
||||||
CREATE TABLE char_data_types (
|
CREATE TABLE char_data_types (
|
||||||
varchar_column varchar(10),
|
varchar_column varchar(10),
|
||||||
@ -22,7 +22,7 @@ COPY char_data_types TO 'C:\YourDirectory\typetest.txt'
|
|||||||
WITH (FORMAT CSV, HEADER, DELIMITER '|');
|
WITH (FORMAT CSV, HEADER, DELIMITER '|');
|
||||||
|
|
||||||
|
|
||||||
-- Listing 3-2: Number data types in action
|
-- Listing 4-2: Number data types in action
|
||||||
|
|
||||||
CREATE TABLE number_data_types (
|
CREATE TABLE number_data_types (
|
||||||
numeric_column numeric(20,5),
|
numeric_column numeric(20,5),
|
||||||
@ -38,8 +38,8 @@ VALUES
|
|||||||
|
|
||||||
SELECT * FROM number_data_types;
|
SELECT * FROM number_data_types;
|
||||||
|
|
||||||
-- Listing 3-3: Rounding issues with float columns
|
-- Listing 4-3: Rounding issues with float columns
|
||||||
-- Assumes table created and loaded with Listing 3-2
|
-- Assumes table created and loaded with Listing 4-2
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
numeric_column * 10000000 AS "Fixed",
|
numeric_column * 10000000 AS "Fixed",
|
||||||
@ -47,7 +47,7 @@ SELECT
|
|||||||
FROM number_data_types
|
FROM number_data_types
|
||||||
WHERE numeric_column = .7;
|
WHERE numeric_column = .7;
|
||||||
|
|
||||||
-- Listing 3-4: Timestamp and interval types in action
|
-- Listing 4-4: Timestamp and interval types in action
|
||||||
|
|
||||||
CREATE TABLE date_time_types (
|
CREATE TABLE date_time_types (
|
||||||
timestamp_column timestamp with time zone,
|
timestamp_column timestamp with time zone,
|
||||||
@ -63,8 +63,8 @@ VALUES
|
|||||||
|
|
||||||
SELECT * FROM date_time_types;
|
SELECT * FROM date_time_types;
|
||||||
|
|
||||||
-- Listing 3-5: Using the interval data type
|
-- Listing 4-5: Using the interval data type
|
||||||
-- Assumes script 3-4 has been run
|
-- Assumes script 4-4 has been run
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
timestamp_column,
|
timestamp_column,
|
||||||
@ -72,7 +72,7 @@ SELECT
|
|||||||
timestamp_column - interval_column AS new_date
|
timestamp_column - interval_column AS new_date
|
||||||
FROM date_time_types;
|
FROM date_time_types;
|
||||||
|
|
||||||
-- Listing 3-6: Three CAST() examples
|
-- Listing 4-6: Three CAST() examples
|
||||||
|
|
||||||
SELECT timestamp_column, CAST(timestamp_column AS varchar(10))
|
SELECT timestamp_column, CAST(timestamp_column AS varchar(10))
|
||||||
FROM date_time_types;
|
FROM date_time_types;
|
||||||
|
|||||||
@ -2,10 +2,10 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 4 Code Examples
|
-- Chapter 5 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Listing 4-1: Using COPY for data import
|
-- Listing 5-1: Using COPY for data import
|
||||||
-- This is example syntax only; running it will produce an error
|
-- This is example syntax only; running it will produce an error
|
||||||
|
|
||||||
COPY table_name
|
COPY table_name
|
||||||
@ -13,7 +13,7 @@ FROM 'C:\YourDirectory\your_file.csv'
|
|||||||
WITH (FORMAT CSV, HEADER);
|
WITH (FORMAT CSV, HEADER);
|
||||||
|
|
||||||
|
|
||||||
-- Listing 4-2: A CREATE TABLE statement for Census county data
|
-- Listing 5-2: A CREATE TABLE statement for Census county data
|
||||||
-- Full data dictionary available at: http://www.census.gov/prod/cen2010/doc/pl94-171.pdf
|
-- Full data dictionary available at: http://www.census.gov/prod/cen2010/doc/pl94-171.pdf
|
||||||
-- Note: Some columns have been given more descriptive names
|
-- Note: Some columns have been given more descriptive names
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ CREATE TABLE us_counties_2010 (
|
|||||||
|
|
||||||
SELECT * FROM us_counties_2010;
|
SELECT * FROM us_counties_2010;
|
||||||
|
|
||||||
-- Listing 4-3: Importing Census data using COPY
|
-- Listing 5-3: Importing Census data using COPY
|
||||||
-- Note! If you run into an import error here, be sure you downloaded the code and
|
-- Note! If you run into an import error here, be sure you downloaded the code and
|
||||||
-- data for the book according to the steps listed on page xxvii in the Introduction.
|
-- data for the book according to the steps listed on page xxvii in the Introduction.
|
||||||
-- Windows users: Please check the Note on page xxvii as well.
|
-- Windows users: Please check the Note on page xxvii as well.
|
||||||
@ -148,7 +148,7 @@ ORDER BY internal_point_lon DESC
|
|||||||
LIMIT 5;
|
LIMIT 5;
|
||||||
|
|
||||||
|
|
||||||
-- Listing 4-4: Creating a table to track supervisor salaries
|
-- Listing 5-4: Creating a table to track supervisor salaries
|
||||||
|
|
||||||
CREATE TABLE supervisor_salaries (
|
CREATE TABLE supervisor_salaries (
|
||||||
town varchar(30),
|
town varchar(30),
|
||||||
@ -159,7 +159,7 @@ CREATE TABLE supervisor_salaries (
|
|||||||
benefits money
|
benefits money
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Listing 4-5: Importing salaries data from CSV to three table columns
|
-- Listing 5-5: Importing salaries data from CSV to three table columns
|
||||||
|
|
||||||
COPY supervisor_salaries (town, supervisor, salary)
|
COPY supervisor_salaries (town, supervisor, salary)
|
||||||
FROM 'C:\YourDirectory\supervisor_salaries.csv'
|
FROM 'C:\YourDirectory\supervisor_salaries.csv'
|
||||||
@ -168,7 +168,7 @@ WITH (FORMAT CSV, HEADER);
|
|||||||
-- Check the data
|
-- Check the data
|
||||||
SELECT * FROM supervisor_salaries LIMIT 2;
|
SELECT * FROM supervisor_salaries LIMIT 2;
|
||||||
|
|
||||||
-- Listing 4-6 Use a temporary table to add a default value to a column during
|
-- Listing 5-6 Use a temporary table to add a default value to a column during
|
||||||
-- import
|
-- import
|
||||||
|
|
||||||
DELETE FROM supervisor_salaries;
|
DELETE FROM supervisor_salaries;
|
||||||
@ -188,20 +188,20 @@ DROP TABLE supervisor_salaries_temp;
|
|||||||
-- Check the data
|
-- Check the data
|
||||||
SELECT * FROM supervisor_salaries LIMIT 2;
|
SELECT * FROM supervisor_salaries LIMIT 2;
|
||||||
|
|
||||||
-- Listing 4-7: Export an entire table with COPY
|
-- Listing 5-7: Export an entire table with COPY
|
||||||
|
|
||||||
COPY us_counties_2010
|
COPY us_counties_2010
|
||||||
TO 'C:\YourDirectory\us_counties_export.txt'
|
TO 'C:\YourDirectory\us_counties_export.txt'
|
||||||
WITH (FORMAT CSV, HEADER, DELIMITER '|');
|
WITH (FORMAT CSV, HEADER, DELIMITER '|');
|
||||||
|
|
||||||
|
|
||||||
-- Listing 4-8: Exporting selected columns from a table with COPY
|
-- Listing 5-8: Exporting selected columns from a table with COPY
|
||||||
|
|
||||||
COPY us_counties_2010 (geo_name, internal_point_lat, internal_point_lon)
|
COPY us_counties_2010 (geo_name, internal_point_lat, internal_point_lon)
|
||||||
TO 'C:\YourDirectory\us_counties_latlon_export.txt'
|
TO 'C:\YourDirectory\us_counties_latlon_export.txt'
|
||||||
WITH (FORMAT CSV, HEADER, DELIMITER '|');
|
WITH (FORMAT CSV, HEADER, DELIMITER '|');
|
||||||
|
|
||||||
-- Listing 4-9: Exporting query results with COPY
|
-- Listing 5-9: Exporting query results with COPY
|
||||||
|
|
||||||
COPY (
|
COPY (
|
||||||
SELECT geo_name, state_us_abbreviation
|
SELECT geo_name, state_us_abbreviation
|
||||||
|
|||||||
@ -2,23 +2,23 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 5 Code Examples
|
-- Chapter 6 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Listing 5-1: Basic addition, subtraction and multiplication with SQL
|
-- Listing 6-1: Basic addition, subtraction and multiplication with SQL
|
||||||
|
|
||||||
SELECT 2 + 2; -- addition
|
SELECT 2 + 2; -- addition
|
||||||
SELECT 9 - 1; -- subtraction
|
SELECT 9 - 1; -- subtraction
|
||||||
SELECT 3 * 4; -- multiplication
|
SELECT 3 * 4; -- multiplication
|
||||||
|
|
||||||
-- Listing 5-2: Integer and decimal division with SQL
|
-- Listing 6-2: Integer and decimal division with SQL
|
||||||
|
|
||||||
SELECT 11 / 6; -- integer division
|
SELECT 11 / 6; -- integer division
|
||||||
SELECT 11 % 6; -- modulo division
|
SELECT 11 % 6; -- modulo division
|
||||||
SELECT 11.0 / 6; -- decimal division
|
SELECT 11.0 / 6; -- decimal division
|
||||||
SELECT CAST(11 AS numeric(3,1)) / 6;
|
SELECT CAST(11 AS numeric(3,1)) / 6;
|
||||||
|
|
||||||
-- Listing 5-3: Exponents, roots and factorials with SQL
|
-- Listing 6-3: Exponents, roots and factorials with SQL
|
||||||
|
|
||||||
SELECT 3 ^ 4; -- exponentiation
|
SELECT 3 ^ 4; -- exponentiation
|
||||||
SELECT |/ 10; -- square root (operator)
|
SELECT |/ 10; -- square root (operator)
|
||||||
@ -34,7 +34,7 @@ SELECT (7 + 8) * 9; -- answer: 135
|
|||||||
SELECT 3 ^ 3 - 1; -- answer: 26
|
SELECT 3 ^ 3 - 1; -- answer: 26
|
||||||
SELECT 3 ^ (3 - 1); -- answer: 9
|
SELECT 3 ^ (3 - 1); -- answer: 9
|
||||||
|
|
||||||
-- Listing 5-4: Selecting Census population columns by race with aliases
|
-- Listing 6-4: Selecting Census population columns by race with aliases
|
||||||
|
|
||||||
SELECT geo_name,
|
SELECT geo_name,
|
||||||
state_us_abbreviation AS "st",
|
state_us_abbreviation AS "st",
|
||||||
@ -48,7 +48,7 @@ SELECT geo_name,
|
|||||||
p0010009 AS "Two or More Races"
|
p0010009 AS "Two or More Races"
|
||||||
FROM us_counties_2010;
|
FROM us_counties_2010;
|
||||||
|
|
||||||
-- Listing 5-5: Adding two columns in us_counties_2010
|
-- Listing 6-5: Adding two columns in us_counties_2010
|
||||||
|
|
||||||
SELECT geo_name,
|
SELECT geo_name,
|
||||||
state_us_abbreviation AS "st",
|
state_us_abbreviation AS "st",
|
||||||
@ -57,7 +57,7 @@ SELECT geo_name,
|
|||||||
p0010003 + p0010004 AS "Total White and Black"
|
p0010003 + p0010004 AS "Total White and Black"
|
||||||
FROM us_counties_2010;
|
FROM us_counties_2010;
|
||||||
|
|
||||||
-- Listing 5-6: Checking Census data totals
|
-- Listing 6-6: Checking Census data totals
|
||||||
|
|
||||||
SELECT geo_name,
|
SELECT geo_name,
|
||||||
state_us_abbreviation AS "st",
|
state_us_abbreviation AS "st",
|
||||||
@ -69,7 +69,7 @@ SELECT geo_name,
|
|||||||
FROM us_counties_2010
|
FROM us_counties_2010
|
||||||
ORDER BY "Difference" DESC;
|
ORDER BY "Difference" DESC;
|
||||||
|
|
||||||
-- Listing 5-7: Calculating the percent of the population that is
|
-- Listing 6-7: Calculating the percent of the population that is
|
||||||
-- Asian by county (percent of the whole)
|
-- Asian by county (percent of the whole)
|
||||||
|
|
||||||
SELECT geo_name,
|
SELECT geo_name,
|
||||||
@ -78,7 +78,7 @@ SELECT geo_name,
|
|||||||
FROM us_counties_2010
|
FROM us_counties_2010
|
||||||
ORDER BY "pct_asian" DESC;
|
ORDER BY "pct_asian" DESC;
|
||||||
|
|
||||||
-- Listing 5-8: Calculating percent change
|
-- Listing 6-8: Calculating percent change
|
||||||
|
|
||||||
CREATE TABLE percent_change (
|
CREATE TABLE percent_change (
|
||||||
department varchar(20),
|
department varchar(20),
|
||||||
@ -102,13 +102,13 @@ SELECT department,
|
|||||||
spend_2014 * 100, 1 ) AS "pct_change"
|
spend_2014 * 100, 1 ) AS "pct_change"
|
||||||
FROM percent_change;
|
FROM percent_change;
|
||||||
|
|
||||||
-- Listing 5-9: Using sum() and avg() aggregate functions
|
-- Listing 6-9: Using sum() and avg() aggregate functions
|
||||||
|
|
||||||
SELECT sum(p0010001) AS "County Sum",
|
SELECT sum(p0010001) AS "County Sum",
|
||||||
round(avg(p0010001), 0) AS "County Average"
|
round(avg(p0010001), 0) AS "County Average"
|
||||||
FROM us_counties_2010;
|
FROM us_counties_2010;
|
||||||
|
|
||||||
-- Listing 5-10: Testing SQL percentile functions
|
-- Listing 6-10: Testing SQL percentile functions
|
||||||
|
|
||||||
CREATE TABLE percentile_test (
|
CREATE TABLE percentile_test (
|
||||||
numbers integer
|
numbers integer
|
||||||
@ -124,7 +124,7 @@ SELECT
|
|||||||
WITHIN GROUP (ORDER BY numbers)
|
WITHIN GROUP (ORDER BY numbers)
|
||||||
FROM percentile_test;
|
FROM percentile_test;
|
||||||
|
|
||||||
-- Listing 5-11: Using sum(), avg(), and percentile_cont() aggregate functions
|
-- Listing 6-11: Using sum(), avg(), and percentile_cont() aggregate functions
|
||||||
|
|
||||||
SELECT sum(p0010001) AS "County Sum",
|
SELECT sum(p0010001) AS "County Sum",
|
||||||
round(avg(p0010001), 0) AS "County Average",
|
round(avg(p0010001), 0) AS "County Average",
|
||||||
@ -132,7 +132,7 @@ SELECT sum(p0010001) AS "County Sum",
|
|||||||
WITHIN GROUP (ORDER BY p0010001) AS "County Median"
|
WITHIN GROUP (ORDER BY p0010001) AS "County Median"
|
||||||
FROM us_counties_2010;
|
FROM us_counties_2010;
|
||||||
|
|
||||||
-- Listing 5-12: Passing an array of values to percentile_cont()
|
-- Listing 6-12: Passing an array of values to percentile_cont()
|
||||||
|
|
||||||
-- quartiles
|
-- quartiles
|
||||||
SELECT percentile_cont(array[.25,.5,.75])
|
SELECT percentile_cont(array[.25,.5,.75])
|
||||||
@ -150,7 +150,7 @@ SELECT percentile_cont(array[.1,.2,.3,.4,.5,.6,.7,.8,.9])
|
|||||||
WITHIN GROUP (ORDER BY p0010001) AS "deciles"
|
WITHIN GROUP (ORDER BY p0010001) AS "deciles"
|
||||||
FROM us_counties_2010;
|
FROM us_counties_2010;
|
||||||
|
|
||||||
-- Listing 5-13: Using unnest() to turn an array into rows
|
-- Listing 6-13: Using unnest() to turn an array into rows
|
||||||
|
|
||||||
SELECT unnest(
|
SELECT unnest(
|
||||||
percentile_cont(array[.25,.5,.75])
|
percentile_cont(array[.25,.5,.75])
|
||||||
@ -158,7 +158,7 @@ SELECT unnest(
|
|||||||
) AS "quartiles"
|
) AS "quartiles"
|
||||||
FROM us_counties_2010;
|
FROM us_counties_2010;
|
||||||
|
|
||||||
-- Listing 5-14: Creating a median() aggregate function in PostgreSQL
|
-- Listing 6-14: Creating a median() aggregate function in PostgreSQL
|
||||||
-- Source: https://wiki.postgresql.org/wiki/Aggregate_Median
|
-- Source: https://wiki.postgresql.org/wiki/Aggregate_Median
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION _final_median(anyarray)
|
CREATE OR REPLACE FUNCTION _final_median(anyarray)
|
||||||
@ -192,7 +192,7 @@ CREATE AGGREGATE median(anyelement) (
|
|||||||
INITCOND='{}'
|
INITCOND='{}'
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Listing 5-15: Using a median() aggregate function
|
-- Listing 6-15: Using a median() aggregate function
|
||||||
|
|
||||||
SELECT sum(p0010001) AS "County Sum",
|
SELECT sum(p0010001) AS "County Sum",
|
||||||
round(avg(p0010001), 0) AS "County Average",
|
round(avg(p0010001), 0) AS "County Average",
|
||||||
@ -201,7 +201,7 @@ SELECT sum(p0010001) AS "County Sum",
|
|||||||
WITHIN GROUP (ORDER BY P0010001) AS "50th Percentile"
|
WITHIN GROUP (ORDER BY P0010001) AS "50th Percentile"
|
||||||
FROM us_counties_2010;
|
FROM us_counties_2010;
|
||||||
|
|
||||||
-- Listing 5-16: Finding the most-frequent value with mode()
|
-- Listing 6-16: Finding the most-frequent value with mode()
|
||||||
|
|
||||||
SELECT mode() WITHIN GROUP (ORDER BY p0010001)
|
SELECT mode() WITHIN GROUP (ORDER BY p0010001)
|
||||||
FROM us_counties_2010;
|
FROM us_counties_2010;
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
-- Chapter 6 Code Examples
|
-- Chapter 6 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Listing 6-1: Creating the departments and employees tables
|
-- Listing 7-1: Creating the departments and employees tables
|
||||||
|
|
||||||
CREATE TABLE departments (
|
CREATE TABLE departments (
|
||||||
dept_id bigserial,
|
dept_id bigserial,
|
||||||
@ -37,13 +37,13 @@ VALUES
|
|||||||
('Soo', 'Nguyen', 83000, 2),
|
('Soo', 'Nguyen', 83000, 2),
|
||||||
('Janet', 'King', 95000, 2);
|
('Janet', 'King', 95000, 2);
|
||||||
|
|
||||||
-- Listing 6-2: Joining the employees and departments tables
|
-- Listing 7-2: Joining the employees and departments tables
|
||||||
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM employees JOIN departments
|
FROM employees JOIN departments
|
||||||
ON employees.dept_id = departments.dept_id;
|
ON employees.dept_id = departments.dept_id;
|
||||||
|
|
||||||
-- Listing 6-3: Creating two tables to explore JOIN types
|
-- Listing 7-3: Creating two tables to explore JOIN types
|
||||||
|
|
||||||
CREATE TABLE schools_left (
|
CREATE TABLE schools_left (
|
||||||
id integer CONSTRAINT left_id_key PRIMARY KEY,
|
id integer CONSTRAINT left_id_key PRIMARY KEY,
|
||||||
@ -68,7 +68,7 @@ INSERT INTO schools_right (id, right_school) VALUES
|
|||||||
(4, 'Chase Magnet Academy'),
|
(4, 'Chase Magnet Academy'),
|
||||||
(6, 'Jefferson High School');
|
(6, 'Jefferson High School');
|
||||||
|
|
||||||
-- Listing 6-4: Using JOIN
|
-- Listing 7-4: Using JOIN
|
||||||
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM schools_left JOIN schools_right
|
FROM schools_left JOIN schools_right
|
||||||
@ -80,51 +80,51 @@ SELECT *
|
|||||||
FROM schools_left INNER JOIN schools_right
|
FROM schools_left INNER JOIN schools_right
|
||||||
ON schools_left.id = schools_right.id;
|
ON schools_left.id = schools_right.id;
|
||||||
|
|
||||||
-- Listing 6-5: Using LEFT JOIN
|
-- Listing 7-5: Using LEFT JOIN
|
||||||
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM schools_left LEFT JOIN schools_right
|
FROM schools_left LEFT JOIN schools_right
|
||||||
ON schools_left.id = schools_right.id;
|
ON schools_left.id = schools_right.id;
|
||||||
|
|
||||||
-- Listing 6-6: Using RIGHT JOIN
|
-- Listing 7-6: Using RIGHT JOIN
|
||||||
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM schools_left RIGHT JOIN schools_right
|
FROM schools_left RIGHT JOIN schools_right
|
||||||
ON schools_left.id = schools_right.id;
|
ON schools_left.id = schools_right.id;
|
||||||
|
|
||||||
-- Listing 6-7: Using FULL OUTER JOIN
|
-- Listing 7-7: Using FULL OUTER JOIN
|
||||||
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM schools_left FULL OUTER JOIN schools_right
|
FROM schools_left FULL OUTER JOIN schools_right
|
||||||
ON schools_left.id = schools_right.id;
|
ON schools_left.id = schools_right.id;
|
||||||
|
|
||||||
-- Listing 6-8: Using CROSS JOIN
|
-- Listing 7-8: Using CROSS JOIN
|
||||||
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM schools_left CROSS JOIN schools_right;
|
FROM schools_left CROSS JOIN schools_right;
|
||||||
|
|
||||||
-- Listing 6-9: Filtering to show missing values with IS NULL
|
-- Listing 7-9: Filtering to show missing values with IS NULL
|
||||||
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM schools_left LEFT JOIN schools_right
|
FROM schools_left LEFT JOIN schools_right
|
||||||
ON schools_left.id = schools_right.id
|
ON schools_left.id = schools_right.id
|
||||||
WHERE schools_right.id IS NULL;
|
WHERE schools_right.id IS NULL;
|
||||||
|
|
||||||
-- Listing 6-10: Querying specific columns in a join
|
-- Listing 7-10: Querying specific columns in a join
|
||||||
SELECT schools_left.id,
|
SELECT schools_left.id,
|
||||||
schools_left.left_school,
|
schools_left.left_school,
|
||||||
schools_right.right_school
|
schools_right.right_school
|
||||||
FROM schools_left LEFT JOIN schools_right
|
FROM schools_left LEFT JOIN schools_right
|
||||||
ON schools_left.id = schools_right.id;
|
ON schools_left.id = schools_right.id;
|
||||||
|
|
||||||
-- Listing 6-11: Simplifying code with table aliases
|
-- Listing 7-11: Simplifying code with table aliases
|
||||||
SELECT lt.id,
|
SELECT lt.id,
|
||||||
lt.left_school,
|
lt.left_school,
|
||||||
rt.right_school
|
rt.right_school
|
||||||
FROM schools_left AS lt LEFT JOIN schools_right AS rt
|
FROM schools_left AS lt LEFT JOIN schools_right AS rt
|
||||||
ON lt.id = rt.id;
|
ON lt.id = rt.id;
|
||||||
|
|
||||||
-- Listing 6-12: Joining multiple tables
|
-- Listing 7-12: Joining multiple tables
|
||||||
CREATE TABLE schools_enrollment (
|
CREATE TABLE schools_enrollment (
|
||||||
id integer,
|
id integer,
|
||||||
enrollment integer
|
enrollment integer
|
||||||
@ -155,7 +155,7 @@ FROM schools_left AS lt LEFT JOIN schools_enrollment AS en
|
|||||||
LEFT JOIN schools_grades AS gr
|
LEFT JOIN schools_grades AS gr
|
||||||
ON lt.id = gr.id;
|
ON lt.id = gr.id;
|
||||||
|
|
||||||
-- Listing 6-13: Performing math on joined Census tables
|
-- Listing 7-13: Performing math on joined Census tables
|
||||||
-- Decennial Census 2000. Full data dictionary at https://www.census.gov/prod/cen2000/doc/pl94-171.pdf
|
-- Decennial Census 2000. Full data dictionary at https://www.census.gov/prod/cen2000/doc/pl94-171.pdf
|
||||||
-- Note: Some non-number columns have been given more descriptive names
|
-- Note: Some non-number columns have been given more descriptive names
|
||||||
|
|
||||||
|
|||||||
@ -2,10 +2,10 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 7 Code Examples
|
-- Chapter 8 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Listing 7-1: Declaring a single-column natural key as primary key
|
-- Listing 8-1: Declaring a single-column natural key as primary key
|
||||||
|
|
||||||
-- As a column constraint
|
-- As a column constraint
|
||||||
CREATE TABLE natural_key_example (
|
CREATE TABLE natural_key_example (
|
||||||
@ -25,14 +25,14 @@ CREATE TABLE natural_key_example (
|
|||||||
CONSTRAINT license_key PRIMARY KEY (license_id)
|
CONSTRAINT license_key PRIMARY KEY (license_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Listing 7-2: Example of a primary key violation
|
-- Listing 8-2: Example of a primary key violation
|
||||||
INSERT INTO natural_key_example (license_id, first_name, last_name)
|
INSERT INTO natural_key_example (license_id, first_name, last_name)
|
||||||
VALUES ('T229901', 'Lynn', 'Malero');
|
VALUES ('T229901', 'Lynn', 'Malero');
|
||||||
|
|
||||||
INSERT INTO natural_key_example (license_id, first_name, last_name)
|
INSERT INTO natural_key_example (license_id, first_name, last_name)
|
||||||
VALUES ('T229901', 'Sam', 'Tracy');
|
VALUES ('T229901', 'Sam', 'Tracy');
|
||||||
|
|
||||||
-- Listing 7-3: Declaring a composite primary key as a natural key
|
-- Listing 8-3: Declaring a composite primary key as a natural key
|
||||||
CREATE TABLE natural_key_composite_example (
|
CREATE TABLE natural_key_composite_example (
|
||||||
student_id varchar(10),
|
student_id varchar(10),
|
||||||
school_day date,
|
school_day date,
|
||||||
@ -40,7 +40,7 @@ CREATE TABLE natural_key_composite_example (
|
|||||||
CONSTRAINT student_key PRIMARY KEY (student_id, school_day)
|
CONSTRAINT student_key PRIMARY KEY (student_id, school_day)
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Listing 7-4: Example of a composite primary key violation
|
-- Listing 8-4: Example of a composite primary key violation
|
||||||
|
|
||||||
INSERT INTO natural_key_composite_example (student_id, school_day, present)
|
INSERT INTO natural_key_composite_example (student_id, school_day, present)
|
||||||
VALUES(775, '1/22/2017', 'Y');
|
VALUES(775, '1/22/2017', 'Y');
|
||||||
@ -51,7 +51,7 @@ VALUES(775, '1/23/2017', 'Y');
|
|||||||
INSERT INTO natural_key_composite_example (student_id, school_day, present)
|
INSERT INTO natural_key_composite_example (student_id, school_day, present)
|
||||||
VALUES(775, '1/23/2017', 'N');
|
VALUES(775, '1/23/2017', 'N');
|
||||||
|
|
||||||
-- Listing 7-5: Declaring a bigserial column as a surrogate key
|
-- Listing 8-5: Declaring a bigserial column as a surrogate key
|
||||||
|
|
||||||
CREATE TABLE surrogate_key_example (
|
CREATE TABLE surrogate_key_example (
|
||||||
order_number bigserial,
|
order_number bigserial,
|
||||||
@ -67,7 +67,7 @@ VALUES ('Beachball Polish', '2015-03-17'),
|
|||||||
|
|
||||||
SELECT * FROM surrogate_key_example;
|
SELECT * FROM surrogate_key_example;
|
||||||
|
|
||||||
-- Listing 7-6: A foreign key example
|
-- Listing 8-6: A foreign key example
|
||||||
|
|
||||||
CREATE TABLE licenses (
|
CREATE TABLE licenses (
|
||||||
license_id varchar(10),
|
license_id varchar(10),
|
||||||
@ -92,7 +92,7 @@ VALUES ('A203391', '3/17/2017', 'T229901');
|
|||||||
INSERT INTO registrations (registration_id, registration_date, license_id)
|
INSERT INTO registrations (registration_id, registration_date, license_id)
|
||||||
VALUES ('A75772', '3/17/2017', 'T000001');
|
VALUES ('A75772', '3/17/2017', 'T000001');
|
||||||
|
|
||||||
-- Listing 7-7: CHECK constraint examples
|
-- Listing 8-7: CHECK constraint examples
|
||||||
|
|
||||||
CREATE TABLE check_constraint_example (
|
CREATE TABLE check_constraint_example (
|
||||||
user_id bigserial,
|
user_id bigserial,
|
||||||
@ -110,7 +110,7 @@ VALUES ('admin');
|
|||||||
INSERT INTO check_constraint_example (salary)
|
INSERT INTO check_constraint_example (salary)
|
||||||
VALUES (0);
|
VALUES (0);
|
||||||
|
|
||||||
-- Listing 7-8: UNIQUE constraint example
|
-- Listing 8-8: UNIQUE constraint example
|
||||||
|
|
||||||
CREATE TABLE unique_constraint_example (
|
CREATE TABLE unique_constraint_example (
|
||||||
contact_id bigserial CONSTRAINT contact_id_key PRIMARY KEY,
|
contact_id bigserial CONSTRAINT contact_id_key PRIMARY KEY,
|
||||||
@ -129,7 +129,7 @@ VALUES ('Betty', 'Diaz', 'bdiaz@example.org');
|
|||||||
INSERT INTO unique_constraint_example (first_name, last_name, email)
|
INSERT INTO unique_constraint_example (first_name, last_name, email)
|
||||||
VALUES ('Sasha', 'Lee', 'slee@example.org');
|
VALUES ('Sasha', 'Lee', 'slee@example.org');
|
||||||
|
|
||||||
-- Listing 7-9: NOT NULL constraint example
|
-- Listing 8-9: NOT NULL constraint example
|
||||||
|
|
||||||
CREATE TABLE not_null_example (
|
CREATE TABLE not_null_example (
|
||||||
student_id bigserial,
|
student_id bigserial,
|
||||||
@ -138,7 +138,7 @@ CREATE TABLE not_null_example (
|
|||||||
CONSTRAINT student_id_key PRIMARY KEY (student_id)
|
CONSTRAINT student_id_key PRIMARY KEY (student_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Listing 7-10: Dropping and adding a primary key and a NOT NULL constraint
|
-- Listing 8-10: Dropping and adding a primary key and a NOT NULL constraint
|
||||||
|
|
||||||
-- Drop
|
-- Drop
|
||||||
ALTER TABLE not_null_example DROP CONSTRAINT student_id_key;
|
ALTER TABLE not_null_example DROP CONSTRAINT student_id_key;
|
||||||
@ -152,7 +152,7 @@ ALTER TABLE not_null_example ALTER COLUMN first_name DROP NOT NULL;
|
|||||||
-- Add
|
-- Add
|
||||||
ALTER TABLE not_null_example ALTER COLUMN first_name SET NOT NULL;
|
ALTER TABLE not_null_example ALTER COLUMN first_name SET NOT NULL;
|
||||||
|
|
||||||
-- Listing 7-11: Importing New York City address data
|
-- Listing 8-11: Importing New York City address data
|
||||||
|
|
||||||
CREATE TABLE new_york_addresses (
|
CREATE TABLE new_york_addresses (
|
||||||
longitude numeric(9,6),
|
longitude numeric(9,6),
|
||||||
@ -168,7 +168,7 @@ COPY new_york_addresses
|
|||||||
FROM 'C:\YourDirectory\city_of_new_york.csv'
|
FROM 'C:\YourDirectory\city_of_new_york.csv'
|
||||||
WITH (FORMAT CSV, HEADER);
|
WITH (FORMAT CSV, HEADER);
|
||||||
|
|
||||||
-- Listing 7-12: Benchmark queries for index performance
|
-- Listing 8-12: Benchmark queries for index performance
|
||||||
|
|
||||||
EXPLAIN ANALYZE SELECT * FROM new_york_addresses
|
EXPLAIN ANALYZE SELECT * FROM new_york_addresses
|
||||||
WHERE street = 'BROADWAY';
|
WHERE street = 'BROADWAY';
|
||||||
@ -179,6 +179,6 @@ WHERE street = '52 STREET';
|
|||||||
EXPLAIN ANALYZE SELECT * FROM new_york_addresses
|
EXPLAIN ANALYZE SELECT * FROM new_york_addresses
|
||||||
WHERE street = 'ZWICKY AVENUE';
|
WHERE street = 'ZWICKY AVENUE';
|
||||||
|
|
||||||
-- Listing 7-13: Creating a B-Tree index on the new_york_addresses table
|
-- Listing 8-13: Creating a B-Tree index on the new_york_addresses table
|
||||||
|
|
||||||
CREATE INDEX street_idx ON new_york_addresses (street);
|
CREATE INDEX street_idx ON new_york_addresses (street);
|
||||||
|
|||||||
@ -2,10 +2,10 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 8 Code Examples
|
-- Chapter 9 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Listing 8-1: Creating and filling the 2014 Public Libraries Survey table
|
-- Listing 9-1: Creating and filling the 2014 Public Libraries Survey table
|
||||||
|
|
||||||
CREATE TABLE pls_fy2014_pupld14a (
|
CREATE TABLE pls_fy2014_pupld14a (
|
||||||
stabr varchar(2) NOT NULL,
|
stabr varchar(2) NOT NULL,
|
||||||
@ -91,7 +91,7 @@ COPY pls_fy2014_pupld14a
|
|||||||
FROM 'C:\YourDirectory\pls_fy2014_pupld14a.csv'
|
FROM 'C:\YourDirectory\pls_fy2014_pupld14a.csv'
|
||||||
WITH (FORMAT CSV, HEADER);
|
WITH (FORMAT CSV, HEADER);
|
||||||
|
|
||||||
-- Listing 8-2: Creating and filling the 2009 Public Libraries Survey table
|
-- Listing 9-2: Creating and filling the 2009 Public Libraries Survey table
|
||||||
|
|
||||||
CREATE TABLE pls_fy2009_pupld09a (
|
CREATE TABLE pls_fy2009_pupld09a (
|
||||||
stabr varchar(2) NOT NULL,
|
stabr varchar(2) NOT NULL,
|
||||||
@ -173,7 +173,7 @@ COPY pls_fy2009_pupld09a
|
|||||||
FROM 'C:\YourDirectory\pls_fy2009_pupld09a.csv'
|
FROM 'C:\YourDirectory\pls_fy2009_pupld09a.csv'
|
||||||
WITH (FORMAT CSV, HEADER);
|
WITH (FORMAT CSV, HEADER);
|
||||||
|
|
||||||
-- Listing 8-3: Using count() for table row counts
|
-- Listing 9-3: Using count() for table row counts
|
||||||
|
|
||||||
SELECT count(*)
|
SELECT count(*)
|
||||||
FROM pls_fy2014_pupld14a;
|
FROM pls_fy2014_pupld14a;
|
||||||
@ -181,12 +181,12 @@ FROM pls_fy2014_pupld14a;
|
|||||||
SELECT count(*)
|
SELECT count(*)
|
||||||
FROM pls_fy2009_pupld09a;
|
FROM pls_fy2009_pupld09a;
|
||||||
|
|
||||||
-- Listing 8-4: Using count() for the number of values in a column
|
-- Listing 9-4: Using count() for the number of values in a column
|
||||||
|
|
||||||
SELECT count(salaries)
|
SELECT count(salaries)
|
||||||
FROM pls_fy2014_pupld14a;
|
FROM pls_fy2014_pupld14a;
|
||||||
|
|
||||||
-- Listing 8-5: Using count() for the number of distinct values in a column
|
-- Listing 9-5: Using count() for the number of distinct values in a column
|
||||||
|
|
||||||
SELECT count(libname)
|
SELECT count(libname)
|
||||||
FROM pls_fy2014_pupld14a;
|
FROM pls_fy2014_pupld14a;
|
||||||
@ -205,11 +205,11 @@ SELECT libname, city, stabr
|
|||||||
FROM pls_fy2014_pupld14a
|
FROM pls_fy2014_pupld14a
|
||||||
WHERE libname = 'OXFORD PUBLIC LIBRARY';
|
WHERE libname = 'OXFORD PUBLIC LIBRARY';
|
||||||
|
|
||||||
-- Listing 8-6: Finding the most and fewest visits using max() and min()
|
-- Listing 9-6: Finding the most and fewest visits using max() and min()
|
||||||
SELECT max(visits), min(visits)
|
SELECT max(visits), min(visits)
|
||||||
FROM pls_fy2014_pupld14a;
|
FROM pls_fy2014_pupld14a;
|
||||||
|
|
||||||
-- Listing 8-7: Using GROUP BY on the stabr column
|
-- Listing 9-7: Using GROUP BY on the stabr column
|
||||||
|
|
||||||
-- There are 56 in 2014.
|
-- There are 56 in 2014.
|
||||||
SELECT stabr
|
SELECT stabr
|
||||||
@ -223,7 +223,7 @@ FROM pls_fy2009_pupld09a
|
|||||||
GROUP BY stabr
|
GROUP BY stabr
|
||||||
ORDER BY stabr;
|
ORDER BY stabr;
|
||||||
|
|
||||||
-- Listing 8-8: Using GROUP BY on the city and stabr columns
|
-- Listing 9-8: Using GROUP BY on the city and stabr columns
|
||||||
|
|
||||||
SELECT city, stabr
|
SELECT city, stabr
|
||||||
FROM pls_fy2014_pupld14a
|
FROM pls_fy2014_pupld14a
|
||||||
@ -236,21 +236,21 @@ FROM pls_fy2014_pupld14a
|
|||||||
GROUP BY city, stabr
|
GROUP BY city, stabr
|
||||||
ORDER BY count(*) DESC;
|
ORDER BY count(*) DESC;
|
||||||
|
|
||||||
-- Listing 8-9: GROUP BY with count() on the stabr column
|
-- Listing 9-9: GROUP BY with count() on the stabr column
|
||||||
|
|
||||||
SELECT stabr, count(*)
|
SELECT stabr, count(*)
|
||||||
FROM pls_fy2014_pupld14a
|
FROM pls_fy2014_pupld14a
|
||||||
GROUP BY stabr
|
GROUP BY stabr
|
||||||
ORDER BY count(*) DESC;
|
ORDER BY count(*) DESC;
|
||||||
|
|
||||||
-- Listing 8-10: GROUP BY with count() on the stabr and stataddr columns
|
-- Listing 9-10: GROUP BY with count() on the stabr and stataddr columns
|
||||||
|
|
||||||
SELECT stabr, stataddr, count(*)
|
SELECT stabr, stataddr, count(*)
|
||||||
FROM pls_fy2014_pupld14a
|
FROM pls_fy2014_pupld14a
|
||||||
GROUP BY stabr, stataddr
|
GROUP BY stabr, stataddr
|
||||||
ORDER BY stabr ASC, count(*) DESC;
|
ORDER BY stabr ASC, count(*) DESC;
|
||||||
|
|
||||||
-- Listing 8-11: Using the sum() aggregate function to total visits to
|
-- Listing 9-11: Using the sum() aggregate function to total visits to
|
||||||
-- libraries in 2014 and 2009
|
-- libraries in 2014 and 2009
|
||||||
|
|
||||||
-- 2014
|
-- 2014
|
||||||
@ -263,7 +263,7 @@ SELECT sum(visits) AS visits_2009
|
|||||||
FROM pls_fy2009_pupld09a
|
FROM pls_fy2009_pupld09a
|
||||||
WHERE visits >= 0;
|
WHERE visits >= 0;
|
||||||
|
|
||||||
-- Listing 8-12: Using sum() to total visits on joined 2014 and 2009 library tables
|
-- Listing 9-12: Using sum() to total visits on joined 2014 and 2009 library tables
|
||||||
|
|
||||||
SELECT sum(pls14.visits) AS visits_2014,
|
SELECT sum(pls14.visits) AS visits_2014,
|
||||||
sum(pls09.visits) AS visits_2009
|
sum(pls09.visits) AS visits_2009
|
||||||
@ -271,7 +271,7 @@ FROM pls_fy2014_pupld14a pls14 JOIN pls_fy2009_pupld09a pls09
|
|||||||
ON pls14.fscskey = pls09.fscskey
|
ON pls14.fscskey = pls09.fscskey
|
||||||
WHERE pls14.visits >= 0 AND pls09.visits >= 0;
|
WHERE pls14.visits >= 0 AND pls09.visits >= 0;
|
||||||
|
|
||||||
-- Listing 8-13: Using GROUP BY to track percent change in library visits by state
|
-- Listing 9-13: Using GROUP BY to track percent change in library visits by state
|
||||||
|
|
||||||
SELECT pls14.stabr,
|
SELECT pls14.stabr,
|
||||||
sum(pls14.visits) AS visits_2014,
|
sum(pls14.visits) AS visits_2014,
|
||||||
@ -284,7 +284,7 @@ WHERE pls14.visits >= 0 AND pls09.visits >= 0
|
|||||||
GROUP BY pls14.stabr
|
GROUP BY pls14.stabr
|
||||||
ORDER BY pct_change DESC;
|
ORDER BY pct_change DESC;
|
||||||
|
|
||||||
-- Listing 8-14: Using HAVING to filter the results of an aggregate query
|
-- Listing 9-14: Using HAVING to filter the results of an aggregate query
|
||||||
|
|
||||||
SELECT pls14.stabr,
|
SELECT pls14.stabr,
|
||||||
sum(pls14.visits) AS visits_2014,
|
sum(pls14.visits) AS visits_2014,
|
||||||
|
|||||||
@ -2,10 +2,10 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 9 Code Examples
|
-- Chapter 10 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Listing 9-1: Importing the FSIS Meat, Poultry, and Egg Inspection Directory
|
-- Listing 10-1: Importing the FSIS Meat, Poultry, and Egg Inspection Directory
|
||||||
-- https://catalog.data.gov/dataset/meat-poultry-and-egg-inspection-directory-by-establishment-name
|
-- https://catalog.data.gov/dataset/meat-poultry-and-egg-inspection-directory-by-establishment-name
|
||||||
|
|
||||||
CREATE TABLE meat_poultry_egg_inspect (
|
CREATE TABLE meat_poultry_egg_inspect (
|
||||||
@ -30,7 +30,7 @@ CREATE INDEX company_idx ON meat_poultry_egg_inspect (company);
|
|||||||
-- Count the rows imported:
|
-- Count the rows imported:
|
||||||
SELECT count(*) FROM meat_poultry_egg_inspect;
|
SELECT count(*) FROM meat_poultry_egg_inspect;
|
||||||
|
|
||||||
-- Listing 9-2: Finding multiple companies at the same address
|
-- Listing 10-2: Finding multiple companies at the same address
|
||||||
SELECT company,
|
SELECT company,
|
||||||
street,
|
street,
|
||||||
city,
|
city,
|
||||||
@ -41,14 +41,14 @@ GROUP BY company, street, city, st
|
|||||||
HAVING count(*) > 1
|
HAVING count(*) > 1
|
||||||
ORDER BY company, street, city, st;
|
ORDER BY company, street, city, st;
|
||||||
|
|
||||||
-- Listing 9-3: Grouping and counting states
|
-- Listing 10-3: Grouping and counting states
|
||||||
SELECT st,
|
SELECT st,
|
||||||
count(*) AS st_count
|
count(*) AS st_count
|
||||||
FROM meat_poultry_egg_inspect
|
FROM meat_poultry_egg_inspect
|
||||||
GROUP BY st
|
GROUP BY st
|
||||||
ORDER BY st;
|
ORDER BY st;
|
||||||
|
|
||||||
-- Listing 9-4: Using IS NULL to find missing values in the st column
|
-- Listing 10-4: Using IS NULL to find missing values in the st column
|
||||||
SELECT est_number,
|
SELECT est_number,
|
||||||
company,
|
company,
|
||||||
city,
|
city,
|
||||||
@ -57,7 +57,7 @@ SELECT est_number,
|
|||||||
FROM meat_poultry_egg_inspect
|
FROM meat_poultry_egg_inspect
|
||||||
WHERE st IS NULL;
|
WHERE st IS NULL;
|
||||||
|
|
||||||
-- Listing 9-5: Using GROUP BY and count() to find inconsistent company names
|
-- Listing 10-5: Using GROUP BY and count() to find inconsistent company names
|
||||||
|
|
||||||
SELECT company,
|
SELECT company,
|
||||||
count(*) AS company_count
|
count(*) AS company_count
|
||||||
@ -65,7 +65,7 @@ FROM meat_poultry_egg_inspect
|
|||||||
GROUP BY company
|
GROUP BY company
|
||||||
ORDER BY company ASC;
|
ORDER BY company ASC;
|
||||||
|
|
||||||
-- Listing 9-6: Using length() and count() to test the zip column
|
-- Listing 10-6: Using length() and count() to test the zip column
|
||||||
|
|
||||||
SELECT length(zip),
|
SELECT length(zip),
|
||||||
count(*) AS length_count
|
count(*) AS length_count
|
||||||
@ -73,7 +73,7 @@ FROM meat_poultry_egg_inspect
|
|||||||
GROUP BY length(zip)
|
GROUP BY length(zip)
|
||||||
ORDER BY length(zip) ASC;
|
ORDER BY length(zip) ASC;
|
||||||
|
|
||||||
-- Listing 9-7: Filtering with length() to find short zip values
|
-- Listing 10-7: Filtering with length() to find short zip values
|
||||||
|
|
||||||
SELECT st,
|
SELECT st,
|
||||||
count(*) AS st_count
|
count(*) AS st_count
|
||||||
@ -82,7 +82,7 @@ WHERE length(zip) < 5
|
|||||||
GROUP BY st
|
GROUP BY st
|
||||||
ORDER BY st ASC;
|
ORDER BY st ASC;
|
||||||
|
|
||||||
-- Listing 9-8: Backing up a table
|
-- Listing 10-8: Backing up a table
|
||||||
|
|
||||||
CREATE TABLE meat_poultry_egg_inspect_backup AS
|
CREATE TABLE meat_poultry_egg_inspect_backup AS
|
||||||
SELECT * FROM meat_poultry_egg_inspect;
|
SELECT * FROM meat_poultry_egg_inspect;
|
||||||
@ -92,21 +92,21 @@ SELECT
|
|||||||
(SELECT count(*) FROM meat_poultry_egg_inspect) AS original,
|
(SELECT count(*) FROM meat_poultry_egg_inspect) AS original,
|
||||||
(SELECT count(*) FROM meat_poultry_egg_inspect_backup) AS backup;
|
(SELECT count(*) FROM meat_poultry_egg_inspect_backup) AS backup;
|
||||||
|
|
||||||
-- Listing 9-9: Creating and filling the st_copy column with ALTER TABLE and UPDATE
|
-- Listing 10-9: Creating and filling the st_copy column with ALTER TABLE and UPDATE
|
||||||
|
|
||||||
ALTER TABLE meat_poultry_egg_inspect ADD COLUMN st_copy varchar(2);
|
ALTER TABLE meat_poultry_egg_inspect ADD COLUMN st_copy varchar(2);
|
||||||
|
|
||||||
UPDATE meat_poultry_egg_inspect
|
UPDATE meat_poultry_egg_inspect
|
||||||
SET st_copy = st;
|
SET st_copy = st;
|
||||||
|
|
||||||
-- Listing 9-10: Checking values in the st and st_copy columns
|
-- Listing 10-10: Checking values in the st and st_copy columns
|
||||||
|
|
||||||
SELECT st,
|
SELECT st,
|
||||||
st_copy
|
st_copy
|
||||||
FROM meat_poultry_egg_inspect
|
FROM meat_poultry_egg_inspect
|
||||||
ORDER BY st;
|
ORDER BY st;
|
||||||
|
|
||||||
-- Listing 9-11: Updating the st column for three establishments
|
-- Listing 10-11: Updating the st column for three establishments
|
||||||
|
|
||||||
UPDATE meat_poultry_egg_inspect
|
UPDATE meat_poultry_egg_inspect
|
||||||
SET st = 'MN'
|
SET st = 'MN'
|
||||||
@ -120,7 +120,7 @@ UPDATE meat_poultry_egg_inspect
|
|||||||
SET st = 'WI'
|
SET st = 'WI'
|
||||||
WHERE est_number = 'M263A+P263A+V263A';
|
WHERE est_number = 'M263A+P263A+V263A';
|
||||||
|
|
||||||
-- Listing 9-12: Restoring original st column values
|
-- Listing 10-12: Restoring original st column values
|
||||||
|
|
||||||
-- Restoring from the column backup
|
-- Restoring from the column backup
|
||||||
UPDATE meat_poultry_egg_inspect
|
UPDATE meat_poultry_egg_inspect
|
||||||
@ -132,14 +132,14 @@ SET st = backup.st
|
|||||||
FROM meat_poultry_egg_inspect_backup backup
|
FROM meat_poultry_egg_inspect_backup backup
|
||||||
WHERE original.est_number = backup.est_number;
|
WHERE original.est_number = backup.est_number;
|
||||||
|
|
||||||
-- Listing 9-13: Creating and filling the company_standard column
|
-- Listing 10-13: Creating and filling the company_standard column
|
||||||
|
|
||||||
ALTER TABLE meat_poultry_egg_inspect ADD COLUMN company_standard varchar(100);
|
ALTER TABLE meat_poultry_egg_inspect ADD COLUMN company_standard varchar(100);
|
||||||
|
|
||||||
UPDATE meat_poultry_egg_inspect
|
UPDATE meat_poultry_egg_inspect
|
||||||
SET company_standard = company;
|
SET company_standard = company;
|
||||||
|
|
||||||
-- Listing 9-14: Use UPDATE to modify field values that match a string
|
-- Listing 10-14: Use UPDATE to modify field values that match a string
|
||||||
|
|
||||||
UPDATE meat_poultry_egg_inspect
|
UPDATE meat_poultry_egg_inspect
|
||||||
SET company_standard = 'Armour-Eckrich Meats'
|
SET company_standard = 'Armour-Eckrich Meats'
|
||||||
@ -149,26 +149,26 @@ SELECT company, company_standard
|
|||||||
FROM meat_poultry_egg_inspect
|
FROM meat_poultry_egg_inspect
|
||||||
WHERE company LIKE 'Armour%';
|
WHERE company LIKE 'Armour%';
|
||||||
|
|
||||||
-- Listing 9-15: Creating and filling the zip_copy column
|
-- Listing 10-15: Creating and filling the zip_copy column
|
||||||
|
|
||||||
ALTER TABLE meat_poultry_egg_inspect ADD COLUMN zip_copy varchar(5);
|
ALTER TABLE meat_poultry_egg_inspect ADD COLUMN zip_copy varchar(5);
|
||||||
|
|
||||||
UPDATE meat_poultry_egg_inspect
|
UPDATE meat_poultry_egg_inspect
|
||||||
SET zip_copy = zip;
|
SET zip_copy = zip;
|
||||||
|
|
||||||
-- Listing 9-16: Modify codes in the zip column missing two leading zeros
|
-- Listing 10-16: Modify codes in the zip column missing two leading zeros
|
||||||
|
|
||||||
UPDATE meat_poultry_egg_inspect
|
UPDATE meat_poultry_egg_inspect
|
||||||
SET zip = '00' || zip
|
SET zip = '00' || zip
|
||||||
WHERE st IN('PR','VI') AND length(zip) = 3;
|
WHERE st IN('PR','VI') AND length(zip) = 3;
|
||||||
|
|
||||||
-- Listing 9-17: Modify codes in the zip column missing one leading zero
|
-- Listing 10-17: Modify codes in the zip column missing one leading zero
|
||||||
|
|
||||||
UPDATE meat_poultry_egg_inspect
|
UPDATE meat_poultry_egg_inspect
|
||||||
SET zip = '0' || zip
|
SET zip = '0' || zip
|
||||||
WHERE st IN('CT','MA','ME','NH','NJ','RI','VT') AND length(zip) = 4;
|
WHERE st IN('CT','MA','ME','NH','NJ','RI','VT') AND length(zip) = 4;
|
||||||
|
|
||||||
-- Listing 9-18: Creating and filling a state_regions table
|
-- Listing 10-18: Creating and filling a state_regions table
|
||||||
|
|
||||||
CREATE TABLE state_regions (
|
CREATE TABLE state_regions (
|
||||||
st varchar(2) CONSTRAINT st_key PRIMARY KEY,
|
st varchar(2) CONSTRAINT st_key PRIMARY KEY,
|
||||||
@ -179,38 +179,38 @@ COPY state_regions
|
|||||||
FROM 'C:\YourDirectory\state_regions.csv'
|
FROM 'C:\YourDirectory\state_regions.csv'
|
||||||
WITH (FORMAT CSV, HEADER, DELIMITER ',');
|
WITH (FORMAT CSV, HEADER, DELIMITER ',');
|
||||||
|
|
||||||
-- Listing 9-19: Adding and updating an inspection_date column
|
-- Listing 10-19: Adding and updating an inspection_date column
|
||||||
|
|
||||||
ALTER TABLE meat_poultry_egg_inspect ADD COLUMN inspection_date date;
|
ALTER TABLE meat_poultry_egg_inspect ADD COLUMN inspection_date date;
|
||||||
|
|
||||||
UPDATE meat_poultry_egg_inspect inspect
|
UPDATE meat_poultry_egg_inspect inspect
|
||||||
SET inspection_date = '2019-12-01'
|
SET inspection_date = '20110-12-01'
|
||||||
WHERE EXISTS (SELECT state_regions.region
|
WHERE EXISTS (SELECT state_regions.region
|
||||||
FROM state_regions
|
FROM state_regions
|
||||||
WHERE inspect.st = state_regions.st
|
WHERE inspect.st = state_regions.st
|
||||||
AND state_regions.region = 'New England');
|
AND state_regions.region = 'New England');
|
||||||
|
|
||||||
-- Listing 9-20: Viewing updated inspection_date values
|
-- Listing 10-20: Viewing updated inspection_date values
|
||||||
|
|
||||||
SELECT st, inspection_date
|
SELECT st, inspection_date
|
||||||
FROM meat_poultry_egg_inspect
|
FROM meat_poultry_egg_inspect
|
||||||
GROUP BY st, inspection_date
|
GROUP BY st, inspection_date
|
||||||
ORDER BY st;
|
ORDER BY st;
|
||||||
|
|
||||||
-- Listing 9-21: Delete rows matching an expression
|
-- Listing 10-21: Delete rows matching an expression
|
||||||
|
|
||||||
DELETE FROM meat_poultry_egg_inspect
|
DELETE FROM meat_poultry_egg_inspect
|
||||||
WHERE st IN('PR','VI');
|
WHERE st IN('PR','VI');
|
||||||
|
|
||||||
-- Listing 9-22: Remove a column from a table using DROP
|
-- Listing 10-22: Remove a column from a table using DROP
|
||||||
|
|
||||||
ALTER TABLE meat_poultry_egg_inspect DROP COLUMN zip_copy;
|
ALTER TABLE meat_poultry_egg_inspect DROP COLUMN zip_copy;
|
||||||
|
|
||||||
-- Listing 9-23: Remove a table from a database using DROP
|
-- Listing 10-23: Remove a table from a database using DROP
|
||||||
|
|
||||||
DROP TABLE meat_poultry_egg_inspect_backup;
|
DROP TABLE meat_poultry_egg_inspect_backup;
|
||||||
|
|
||||||
-- Listing 9-24: Demonstrating a transaction block
|
-- Listing 10-24: Demonstrating a transaction block
|
||||||
|
|
||||||
-- Start transaction and perform update
|
-- Start transaction and perform update
|
||||||
START TRANSACTION;
|
START TRANSACTION;
|
||||||
@ -243,14 +243,14 @@ WHERE company = 'AGRO Merchants Oakland, LLC';
|
|||||||
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|
||||||
-- Listing 9-25: Backing up a table while adding and filling a new column
|
-- Listing 10-25: Backing up a table while adding and filling a new column
|
||||||
|
|
||||||
CREATE TABLE meat_poultry_egg_inspect_backup AS
|
CREATE TABLE meat_poultry_egg_inspect_backup AS
|
||||||
SELECT *,
|
SELECT *,
|
||||||
'2018-02-07'::date AS reviewed_date
|
'2018-02-07'::date AS reviewed_date
|
||||||
FROM meat_poultry_egg_inspect;
|
FROM meat_poultry_egg_inspect;
|
||||||
|
|
||||||
-- Listing 9-26: Swapping table names using ALTER TABLE
|
-- Listing 10-26: Swapping table names using ALTER TABLE
|
||||||
|
|
||||||
ALTER TABLE meat_poultry_egg_inspect RENAME TO meat_poultry_egg_inspect_temp;
|
ALTER TABLE meat_poultry_egg_inspect RENAME TO meat_poultry_egg_inspect_temp;
|
||||||
ALTER TABLE meat_poultry_egg_inspect_backup RENAME TO meat_poultry_egg_inspect;
|
ALTER TABLE meat_poultry_egg_inspect_backup RENAME TO meat_poultry_egg_inspect;
|
||||||
|
|||||||
@ -2,10 +2,10 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 10 Code Examples
|
-- Chapter 11 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Listing 10-1: Create Census 2011-2015 ACS 5-Year stats table and import data
|
-- Listing 11-1: Create Census 2011-2015 ACS 5-Year stats table and import data
|
||||||
|
|
||||||
CREATE TABLE acs_2011_2015_stats (
|
CREATE TABLE acs_2011_2015_stats (
|
||||||
geoid varchar(14) CONSTRAINT geoid_key PRIMARY KEY,
|
geoid varchar(14) CONSTRAINT geoid_key PRIMARY KEY,
|
||||||
@ -24,14 +24,14 @@ WITH (FORMAT CSV, HEADER, DELIMITER ',');
|
|||||||
|
|
||||||
SELECT * FROM acs_2011_2015_stats;
|
SELECT * FROM acs_2011_2015_stats;
|
||||||
|
|
||||||
-- Listing 10-2: Using corr(Y, X) to measure the relationship between
|
-- Listing 11-2: Using corr(Y, X) to measure the relationship between
|
||||||
-- education and income
|
-- education and income
|
||||||
|
|
||||||
SELECT corr(median_hh_income, pct_bachelors_higher)
|
SELECT corr(median_hh_income, pct_bachelors_higher)
|
||||||
AS bachelors_income_r
|
AS bachelors_income_r
|
||||||
FROM acs_2011_2015_stats;
|
FROM acs_2011_2015_stats;
|
||||||
|
|
||||||
-- Listing 10-3: Using corr(Y, X) on additional variables
|
-- Listing 11-3: Using corr(Y, X) on additional variables
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
round(
|
round(
|
||||||
@ -45,7 +45,7 @@ SELECT
|
|||||||
) AS bachelors_travel_r
|
) AS bachelors_travel_r
|
||||||
FROM acs_2011_2015_stats;
|
FROM acs_2011_2015_stats;
|
||||||
|
|
||||||
-- Listing 10-4: Regression slope and intercept functions
|
-- Listing 11-4: Regression slope and intercept functions
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
round(
|
round(
|
||||||
@ -56,7 +56,7 @@ SELECT
|
|||||||
) AS y_intercept
|
) AS y_intercept
|
||||||
FROM acs_2011_2015_stats;
|
FROM acs_2011_2015_stats;
|
||||||
|
|
||||||
-- Listing 10-5: Calculating the coefficient of determination, or r-squared
|
-- Listing 11-5: Calculating the coefficient of determination, or r-squared
|
||||||
|
|
||||||
SELECT round(
|
SELECT round(
|
||||||
regr_r2(median_hh_income, pct_bachelors_higher)::numeric, 3
|
regr_r2(median_hh_income, pct_bachelors_higher)::numeric, 3
|
||||||
@ -76,7 +76,7 @@ FROM acs_2011_2015_stats;
|
|||||||
SELECT covar_pop(median_hh_income, pct_bachelors_higher)
|
SELECT covar_pop(median_hh_income, pct_bachelors_higher)
|
||||||
FROM acs_2011_2015_stats;
|
FROM acs_2011_2015_stats;
|
||||||
|
|
||||||
-- Listing 10-6: The rank() and dense_rank() window functions
|
-- Listing 11-6: The rank() and dense_rank() window functions
|
||||||
|
|
||||||
CREATE TABLE widget_companies (
|
CREATE TABLE widget_companies (
|
||||||
id bigserial,
|
id bigserial,
|
||||||
@ -102,7 +102,7 @@ SELECT
|
|||||||
dense_rank() OVER (ORDER BY widget_output DESC)
|
dense_rank() OVER (ORDER BY widget_output DESC)
|
||||||
FROM widget_companies;
|
FROM widget_companies;
|
||||||
|
|
||||||
-- Listing 10-7: Applying rank() within groups using PARTITION BY
|
-- Listing 11-7: Applying rank() within groups using PARTITION BY
|
||||||
|
|
||||||
CREATE TABLE store_sales (
|
CREATE TABLE store_sales (
|
||||||
store varchar(30),
|
store varchar(30),
|
||||||
@ -130,7 +130,7 @@ SELECT
|
|||||||
rank() OVER (PARTITION BY category ORDER BY unit_sales DESC)
|
rank() OVER (PARTITION BY category ORDER BY unit_sales DESC)
|
||||||
FROM store_sales;
|
FROM store_sales;
|
||||||
|
|
||||||
-- Listing 10-8: Create and fill a 2015 FBI crime data table
|
-- Listing 11-8: Create and fill a 2015 FBI crime data table
|
||||||
|
|
||||||
CREATE TABLE fbi_crime_data_2015 (
|
CREATE TABLE fbi_crime_data_2015 (
|
||||||
st varchar(20),
|
st varchar(20),
|
||||||
@ -151,7 +151,7 @@ WITH (FORMAT CSV, HEADER, DELIMITER ',');
|
|||||||
SELECT * FROM fbi_crime_data_2015
|
SELECT * FROM fbi_crime_data_2015
|
||||||
ORDER BY population DESC;
|
ORDER BY population DESC;
|
||||||
|
|
||||||
-- Listing 10-9: Find property crime rates per thousand in cities with 500,000
|
-- Listing 11-9: Find property crime rates per thousand in cities with 500,000
|
||||||
-- or more people
|
-- or more people
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
|
|||||||
@ -2,10 +2,10 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 11 Code Examples
|
-- Chapter 12 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Listing 11-1: Extracting components of a timestamp value using date_part()
|
-- Listing 12-1: Extracting components of a timestamp value using date_part()
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
date_part('year', '2019-12-01 18:37:12 EST'::timestamptz) AS "year",
|
date_part('year', '2019-12-01 18:37:12 EST'::timestamptz) AS "year",
|
||||||
@ -23,7 +23,7 @@ SELECT
|
|||||||
|
|
||||||
SELECT extract('year' from '2019-12-01 18:37:12 EST'::timestamptz) AS "year";
|
SELECT extract('year' from '2019-12-01 18:37:12 EST'::timestamptz) AS "year";
|
||||||
|
|
||||||
-- Listing 11-2: Three functions for making datetimes from components
|
-- Listing 12-2: Three functions for making datetimes from components
|
||||||
|
|
||||||
-- make a date
|
-- make a date
|
||||||
SELECT make_date(2018, 2, 22);
|
SELECT make_date(2018, 2, 22);
|
||||||
@ -42,7 +42,7 @@ SELECT
|
|||||||
localtimestamp,
|
localtimestamp,
|
||||||
now();
|
now();
|
||||||
|
|
||||||
-- Listing 11-3: Comparing current_timestamp and clock_timestamp() during row insert
|
-- Listing 12-3: Comparing current_timestamp and clock_timestamp() during row insert
|
||||||
|
|
||||||
CREATE TABLE current_time_example (
|
CREATE TABLE current_time_example (
|
||||||
time_id bigserial,
|
time_id bigserial,
|
||||||
@ -59,11 +59,11 @@ SELECT * FROM current_time_example;
|
|||||||
|
|
||||||
-- Time Zones
|
-- Time Zones
|
||||||
|
|
||||||
-- Listing 11-4: Showing your PostgreSQL server's default time zone
|
-- Listing 12-4: Showing your PostgreSQL server's default time zone
|
||||||
|
|
||||||
SHOW timezone; -- Note: You can see all run-time defaults with SHOW ALL;
|
SHOW timezone; -- Note: You can see all run-time defaults with SHOW ALL;
|
||||||
|
|
||||||
-- Listing 11-5: Showing time zone abbreviations and names
|
-- Listing 12-5: Showing time zone abbreviations and names
|
||||||
|
|
||||||
SELECT * FROM pg_timezone_abbrevs;
|
SELECT * FROM pg_timezone_abbrevs;
|
||||||
SELECT * FROM pg_timezone_names;
|
SELECT * FROM pg_timezone_names;
|
||||||
@ -72,7 +72,7 @@ SELECT * FROM pg_timezone_names;
|
|||||||
SELECT * FROM pg_timezone_names
|
SELECT * FROM pg_timezone_names
|
||||||
WHERE name LIKE 'Europe%';
|
WHERE name LIKE 'Europe%';
|
||||||
|
|
||||||
-- Listing 11-6: Setting the time zone for a client session
|
-- Listing 12-6: Setting the time zone for a client session
|
||||||
|
|
||||||
SET timezone TO 'US/Pacific';
|
SET timezone TO 'US/Pacific';
|
||||||
|
|
||||||
@ -101,7 +101,7 @@ SELECT '9/30/1929'::date + '5 years'::interval;
|
|||||||
|
|
||||||
-- Taxi Rides
|
-- Taxi Rides
|
||||||
|
|
||||||
-- Listing 11-7: Creating a table and importing NYC yellow taxi data
|
-- Listing 12-7: Creating a table and importing NYC yellow taxi data
|
||||||
|
|
||||||
CREATE TABLE nyc_yellow_taxi_trips_2016_06_01 (
|
CREATE TABLE nyc_yellow_taxi_trips_2016_06_01 (
|
||||||
trip_id bigserial PRIMARY KEY,
|
trip_id bigserial PRIMARY KEY,
|
||||||
@ -155,7 +155,7 @@ ON nyc_yellow_taxi_trips_2016_06_01 (tpep_pickup_datetime);
|
|||||||
|
|
||||||
SELECT count(*) FROM nyc_yellow_taxi_trips_2016_06_01;
|
SELECT count(*) FROM nyc_yellow_taxi_trips_2016_06_01;
|
||||||
|
|
||||||
-- Listing 11-8: Counting taxi trips by hour
|
-- Listing 12-8: Counting taxi trips by hour
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
date_part('hour', tpep_pickup_datetime) AS trip_hour,
|
date_part('hour', tpep_pickup_datetime) AS trip_hour,
|
||||||
@ -164,7 +164,7 @@ FROM nyc_yellow_taxi_trips_2016_06_01
|
|||||||
GROUP BY trip_hour
|
GROUP BY trip_hour
|
||||||
ORDER BY trip_hour;
|
ORDER BY trip_hour;
|
||||||
|
|
||||||
-- Listing 11-9: Exporting taxi pickups per hour to a CSV file
|
-- Listing 12-9: Exporting taxi pickups per hour to a CSV file
|
||||||
|
|
||||||
COPY
|
COPY
|
||||||
(SELECT
|
(SELECT
|
||||||
@ -177,7 +177,7 @@ COPY
|
|||||||
TO 'C:\YourDirectory\hourly_pickups_2016_06_01.csv'
|
TO 'C:\YourDirectory\hourly_pickups_2016_06_01.csv'
|
||||||
WITH (FORMAT CSV, HEADER, DELIMITER ',');
|
WITH (FORMAT CSV, HEADER, DELIMITER ',');
|
||||||
|
|
||||||
-- Listing 11-10: Calculating median trip time by hour
|
-- Listing 12-10: Calculating median trip time by hour
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
date_part('hour', tpep_pickup_datetime) AS trip_hour,
|
date_part('hour', tpep_pickup_datetime) AS trip_hour,
|
||||||
@ -188,7 +188,7 @@ FROM nyc_yellow_taxi_trips_2016_06_01
|
|||||||
GROUP BY trip_hour
|
GROUP BY trip_hour
|
||||||
ORDER BY trip_hour;
|
ORDER BY trip_hour;
|
||||||
|
|
||||||
-- Listing 11-11: Creating a table to hold train trip data
|
-- Listing 12-11: Creating a table to hold train trip data
|
||||||
|
|
||||||
SET timezone TO 'US/Central';
|
SET timezone TO 'US/Central';
|
||||||
|
|
||||||
@ -210,21 +210,21 @@ VALUES
|
|||||||
|
|
||||||
SELECT * FROM train_rides;
|
SELECT * FROM train_rides;
|
||||||
|
|
||||||
-- Listing 11-12: Calculating the length of each trip segment
|
-- Listing 12-12: Calculating the length of each trip segment
|
||||||
|
|
||||||
SELECT segment,
|
SELECT segment,
|
||||||
to_char(departure, 'YYYY-MM-DD HH12:MI a.m. TZ') AS departure,
|
to_char(departure, 'YYYY-MM-DD HH12:MI a.m. TZ') AS departure,
|
||||||
arrival - departure AS segment_time
|
arrival - departure AS segment_time
|
||||||
FROM train_rides;
|
FROM train_rides;
|
||||||
|
|
||||||
-- Listing 11-13: Calculating cumulative intervals using OVER
|
-- Listing 12-13: Calculating cumulative intervals using OVER
|
||||||
|
|
||||||
SELECT segment,
|
SELECT segment,
|
||||||
arrival - departure AS segment_time,
|
arrival - departure AS segment_time,
|
||||||
sum(arrival - departure) OVER (ORDER BY trip_id) AS cume_time
|
sum(arrival - departure) OVER (ORDER BY trip_id) AS cume_time
|
||||||
FROM train_rides;
|
FROM train_rides;
|
||||||
|
|
||||||
-- Listing 11-14: Better formatting for cumulative trip time
|
-- Listing 12-14: Better formatting for cumulative trip time
|
||||||
|
|
||||||
SELECT segment,
|
SELECT segment,
|
||||||
arrival - departure AS segment_time,
|
arrival - departure AS segment_time,
|
||||||
|
|||||||
@ -2,10 +2,10 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 12 Code Examples
|
-- Chapter 13 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Listing 12-1: Using a subquery in a WHERE clause
|
-- Listing 13-1: Using a subquery in a WHERE clause
|
||||||
|
|
||||||
SELECT geo_name,
|
SELECT geo_name,
|
||||||
state_us_abbreviation,
|
state_us_abbreviation,
|
||||||
@ -17,7 +17,7 @@ WHERE p0010001 >= (
|
|||||||
)
|
)
|
||||||
ORDER BY p0010001 DESC;
|
ORDER BY p0010001 DESC;
|
||||||
|
|
||||||
-- Listing 12-2: Using a subquery in a WHERE clause for DELETE
|
-- Listing 13-2: Using a subquery in a WHERE clause for DELETE
|
||||||
|
|
||||||
CREATE TABLE us_counties_2010_top10 AS
|
CREATE TABLE us_counties_2010_top10 AS
|
||||||
SELECT * FROM us_counties_2010;
|
SELECT * FROM us_counties_2010;
|
||||||
@ -30,7 +30,7 @@ WHERE p0010001 < (
|
|||||||
|
|
||||||
SELECT count(*) FROM us_counties_2010_top10;
|
SELECT count(*) FROM us_counties_2010_top10;
|
||||||
|
|
||||||
-- Listing 12-3: Subquery as a derived table in a FROM clause
|
-- Listing 13-3: Subquery as a derived table in a FROM clause
|
||||||
|
|
||||||
SELECT round(calcs.average, 0) as average,
|
SELECT round(calcs.average, 0) as average,
|
||||||
calcs.median,
|
calcs.median,
|
||||||
@ -43,7 +43,7 @@ FROM (
|
|||||||
)
|
)
|
||||||
AS calcs;
|
AS calcs;
|
||||||
|
|
||||||
-- Listing 12-4: Joining two derived tables
|
-- Listing 13-4: Joining two derived tables
|
||||||
|
|
||||||
SELECT census.state_us_abbreviation AS st,
|
SELECT census.state_us_abbreviation AS st,
|
||||||
census.st_population,
|
census.st_population,
|
||||||
@ -69,7 +69,7 @@ JOIN
|
|||||||
ON plants.st = census.state_us_abbreviation
|
ON plants.st = census.state_us_abbreviation
|
||||||
ORDER BY plants_per_million DESC;
|
ORDER BY plants_per_million DESC;
|
||||||
|
|
||||||
-- Listing 12-5: Adding a subquery to a column list
|
-- Listing 13-5: Adding a subquery to a column list
|
||||||
|
|
||||||
SELECT geo_name,
|
SELECT geo_name,
|
||||||
state_us_abbreviation AS st,
|
state_us_abbreviation AS st,
|
||||||
@ -78,7 +78,7 @@ SELECT geo_name,
|
|||||||
FROM us_counties_2010) AS us_median
|
FROM us_counties_2010) AS us_median
|
||||||
FROM us_counties_2010;
|
FROM us_counties_2010;
|
||||||
|
|
||||||
-- Listing 12-6: Using a subquery expression in a calculation
|
-- Listing 13-6: Using a subquery expression in a calculation
|
||||||
|
|
||||||
SELECT geo_name,
|
SELECT geo_name,
|
||||||
state_us_abbreviation AS st,
|
state_us_abbreviation AS st,
|
||||||
@ -136,7 +136,7 @@ WHERE EXISTS (
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- Listing 12-7: Using a simple CTE to find large counties
|
-- Listing 13-7: Using a simple CTE to find large counties
|
||||||
|
|
||||||
WITH
|
WITH
|
||||||
large_counties (geo_name, st, p0010001)
|
large_counties (geo_name, st, p0010001)
|
||||||
@ -158,7 +158,7 @@ WHERE p0010001 >= 100000
|
|||||||
GROUP BY state_us_abbreviation
|
GROUP BY state_us_abbreviation
|
||||||
ORDER BY count(*) DESC;
|
ORDER BY count(*) DESC;
|
||||||
|
|
||||||
-- Listing 12-8: Using CTEs in a table join
|
-- Listing 13-8: Using CTEs in a table join
|
||||||
|
|
||||||
WITH
|
WITH
|
||||||
counties (st, population) AS
|
counties (st, population) AS
|
||||||
@ -179,7 +179,7 @@ FROM counties JOIN plants
|
|||||||
ON counties.st = plants.st
|
ON counties.st = plants.st
|
||||||
ORDER BY per_million DESC;
|
ORDER BY per_million DESC;
|
||||||
|
|
||||||
-- Listing 12-9: Using CTEs to minimize redundant code
|
-- Listing 13-9: Using CTEs to minimize redundant code
|
||||||
|
|
||||||
WITH us_median AS
|
WITH us_median AS
|
||||||
(SELECT percentile_cont(.5)
|
(SELECT percentile_cont(.5)
|
||||||
@ -201,7 +201,7 @@ WHERE (p0010001 - us_median_pop)
|
|||||||
|
|
||||||
CREATE EXTENSION tablefunc;
|
CREATE EXTENSION tablefunc;
|
||||||
|
|
||||||
-- Listing 12-10: Creating and filling the ice_cream_survey table
|
-- Listing 13-10: Creating and filling the ice_cream_survey table
|
||||||
|
|
||||||
CREATE TABLE ice_cream_survey (
|
CREATE TABLE ice_cream_survey (
|
||||||
response_id integer PRIMARY KEY,
|
response_id integer PRIMARY KEY,
|
||||||
@ -213,7 +213,7 @@ COPY ice_cream_survey
|
|||||||
FROM 'C:\YourDirectory\ice_cream_survey.csv'
|
FROM 'C:\YourDirectory\ice_cream_survey.csv'
|
||||||
WITH (FORMAT CSV, HEADER);
|
WITH (FORMAT CSV, HEADER);
|
||||||
|
|
||||||
-- Listing 12-11: Generating the ice cream survey crosstab
|
-- Listing 13-11: Generating the ice cream survey crosstab
|
||||||
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM crosstab('SELECT office,
|
FROM crosstab('SELECT office,
|
||||||
@ -233,7 +233,7 @@ AS (office varchar(20),
|
|||||||
strawberry bigint,
|
strawberry bigint,
|
||||||
vanilla bigint);
|
vanilla bigint);
|
||||||
|
|
||||||
-- Listing 12-12: Creating and filling a temperature_readings table
|
-- Listing 13-12: Creating and filling a temperature_readings table
|
||||||
|
|
||||||
CREATE TABLE temperature_readings (
|
CREATE TABLE temperature_readings (
|
||||||
reading_id bigserial PRIMARY KEY,
|
reading_id bigserial PRIMARY KEY,
|
||||||
@ -248,7 +248,7 @@ COPY temperature_readings
|
|||||||
FROM 'C:\YourDirectory\temperature_readings.csv'
|
FROM 'C:\YourDirectory\temperature_readings.csv'
|
||||||
WITH (FORMAT CSV, HEADER);
|
WITH (FORMAT CSV, HEADER);
|
||||||
|
|
||||||
-- Listing 12-13: Generating the temperature readings crosstab
|
-- Listing 13-13: Generating the temperature readings crosstab
|
||||||
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM crosstab('SELECT
|
FROM crosstab('SELECT
|
||||||
@ -279,7 +279,7 @@ AS (station varchar(50),
|
|||||||
dec numeric(3,0)
|
dec numeric(3,0)
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Listing 12-14: Re-classifying temperature data with CASE
|
-- Listing 13-14: Re-classifying temperature data with CASE
|
||||||
|
|
||||||
SELECT max_temp,
|
SELECT max_temp,
|
||||||
CASE WHEN max_temp >= 90 THEN 'Hot'
|
CASE WHEN max_temp >= 90 THEN 'Hot'
|
||||||
@ -291,7 +291,7 @@ SELECT max_temp,
|
|||||||
END AS temperature_group
|
END AS temperature_group
|
||||||
FROM temperature_readings;
|
FROM temperature_readings;
|
||||||
|
|
||||||
-- Listing 12-15: Using CASE in a Common Table Expression
|
-- Listing 13-15: Using CASE in a Common Table Expression
|
||||||
|
|
||||||
WITH temps_collapsed (station_name, max_temperature_group) AS
|
WITH temps_collapsed (station_name, max_temperature_group) AS
|
||||||
(SELECT station_name,
|
(SELECT station_name,
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 13 Code Examples
|
-- Chapter 14 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Commonly used string functions
|
-- Commonly used string functions
|
||||||
@ -34,7 +34,7 @@ SELECT right('703-555-1212', 8);
|
|||||||
SELECT replace('bat', 'b', 'c');
|
SELECT replace('bat', 'b', 'c');
|
||||||
|
|
||||||
|
|
||||||
-- Table 13-2: Regular Expression Matching Examples
|
-- Table 14-2: Regular Expression Matching Examples
|
||||||
|
|
||||||
-- Any character one or more times
|
-- Any character one or more times
|
||||||
SELECT substring('The game starts at 7 p.m. on May 2, 2019.' from '.+');
|
SELECT substring('The game starts at 7 p.m. on May 2, 2019.' from '.+');
|
||||||
@ -54,7 +54,7 @@ SELECT substring('The game starts at 7 p.m. on May 2, 2019.' from 'May \d, \d{4}
|
|||||||
|
|
||||||
-- Turning Text to Data with Regular Expression Functions
|
-- Turning Text to Data with Regular Expression Functions
|
||||||
|
|
||||||
-- Listing 13-2: Creating and loading the crime_reports table
|
-- Listing 14-2: Creating and loading the crime_reports table
|
||||||
-- Data from https://sheriff.loudoun.gov/dailycrime
|
-- Data from https://sheriff.loudoun.gov/dailycrime
|
||||||
|
|
||||||
CREATE TABLE crime_reports (
|
CREATE TABLE crime_reports (
|
||||||
@ -75,29 +75,29 @@ WITH (FORMAT CSV, HEADER OFF, QUOTE '"');
|
|||||||
|
|
||||||
SELECT original_text FROM crime_reports;
|
SELECT original_text FROM crime_reports;
|
||||||
|
|
||||||
-- Listing 13-3: Using regexp_match() to find the first date
|
-- Listing 14-3: Using regexp_match() to find the first date
|
||||||
SELECT crime_id,
|
SELECT crime_id,
|
||||||
regexp_match(original_text, '\d{1,2}\/\d{1,2}\/\d{2}')
|
regexp_match(original_text, '\d{1,2}\/\d{1,2}\/\d{2}')
|
||||||
FROM crime_reports;
|
FROM crime_reports;
|
||||||
|
|
||||||
-- Listing 13-4: Using the regexp_matches() function with the 'g' flag
|
-- Listing 14-4: Using the regexp_matches() function with the 'g' flag
|
||||||
SELECT crime_id,
|
SELECT crime_id,
|
||||||
regexp_matches(original_text, '\d{1,2}\/\d{1,2}\/\d{2}', 'g')
|
regexp_matches(original_text, '\d{1,2}\/\d{1,2}\/\d{2}', 'g')
|
||||||
FROM crime_reports;
|
FROM crime_reports;
|
||||||
|
|
||||||
-- Listing 13-5: Using regexp_match() to find the second date
|
-- Listing 14-5: Using regexp_match() to find the second date
|
||||||
-- Note that the result includes an unwanted hyphen
|
-- Note that the result includes an unwanted hyphen
|
||||||
SELECT crime_id,
|
SELECT crime_id,
|
||||||
regexp_match(original_text, '-\d{1,2}\/\d{1,2}\/\d{1,2}')
|
regexp_match(original_text, '-\d{1,2}\/\d{1,2}\/\d{1,2}')
|
||||||
FROM crime_reports;
|
FROM crime_reports;
|
||||||
|
|
||||||
-- Listing 13-6: Using a capture group to return only the date
|
-- Listing 14-6: Using a capture group to return only the date
|
||||||
-- Eliminates the hyphen
|
-- Eliminates the hyphen
|
||||||
SELECT crime_id,
|
SELECT crime_id,
|
||||||
regexp_match(original_text, '-(\d{1,2}\/\d{1,2}\/\d{1,2})')
|
regexp_match(original_text, '-(\d{1,2}\/\d{1,2}\/\d{1,2})')
|
||||||
FROM crime_reports;
|
FROM crime_reports;
|
||||||
|
|
||||||
-- Listing 13-7: Matching case number, date, crime type, and city
|
-- Listing 14-7: Matching case number, date, crime type, and city
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
regexp_match(original_text, '(?:C0|SO)[0-9]+') AS case_number,
|
regexp_match(original_text, '(?:C0|SO)[0-9]+') AS case_number,
|
||||||
@ -127,7 +127,7 @@ SELECT crime_id,
|
|||||||
regexp_match(original_text, '(?:C0|SO)[0-9]+') AS case_number
|
regexp_match(original_text, '(?:C0|SO)[0-9]+') AS case_number
|
||||||
FROM crime_reports;
|
FROM crime_reports;
|
||||||
|
|
||||||
-- Listing 13-8: Retrieving a value from within an array
|
-- Listing 14-8: Retrieving a value from within an array
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
crime_id,
|
crime_id,
|
||||||
@ -135,7 +135,7 @@ SELECT
|
|||||||
AS case_number
|
AS case_number
|
||||||
FROM crime_reports;
|
FROM crime_reports;
|
||||||
|
|
||||||
-- Listing 13-9: Updating the crime_reports date_1 column
|
-- Listing 14-9: Updating the crime_reports date_1 column
|
||||||
|
|
||||||
UPDATE crime_reports
|
UPDATE crime_reports
|
||||||
SET date_1 =
|
SET date_1 =
|
||||||
@ -151,7 +151,7 @@ SELECT crime_id,
|
|||||||
original_text
|
original_text
|
||||||
FROM crime_reports;
|
FROM crime_reports;
|
||||||
|
|
||||||
-- Listing 13-10: Updating all crime_reports columns
|
-- Listing 14-10: Updating all crime_reports columns
|
||||||
|
|
||||||
UPDATE crime_reports
|
UPDATE crime_reports
|
||||||
SET date_1 =
|
SET date_1 =
|
||||||
@ -193,7 +193,7 @@ SET date_1 =
|
|||||||
description = (regexp_match(original_text, ':\s(.+)(?:C0|SO)'))[1],
|
description = (regexp_match(original_text, ':\s(.+)(?:C0|SO)'))[1],
|
||||||
case_number = (regexp_match(original_text, '(?:C0|SO)[0-9]+'))[1];
|
case_number = (regexp_match(original_text, '(?:C0|SO)[0-9]+'))[1];
|
||||||
|
|
||||||
-- Listing 13-11: Viewing selected crime data
|
-- Listing 14-11: Viewing selected crime data
|
||||||
|
|
||||||
SELECT date_1,
|
SELECT date_1,
|
||||||
street,
|
street,
|
||||||
@ -201,7 +201,7 @@ SELECT date_1,
|
|||||||
crime_type
|
crime_type
|
||||||
FROM crime_reports;
|
FROM crime_reports;
|
||||||
|
|
||||||
-- Listing 13-12: Using regular expressions in a WHERE clause
|
-- Listing 14-12: Using regular expressions in a WHERE clause
|
||||||
|
|
||||||
SELECT geo_name
|
SELECT geo_name
|
||||||
FROM us_counties_2010
|
FROM us_counties_2010
|
||||||
@ -214,7 +214,7 @@ WHERE geo_name ~* '.+ash.+' AND geo_name !~ 'Wash.+'
|
|||||||
ORDER BY geo_name;
|
ORDER BY geo_name;
|
||||||
|
|
||||||
|
|
||||||
-- Listing 13-13: Regular expression functions to replace and split
|
-- Listing 14-13: Regular expression functions to replace and split
|
||||||
|
|
||||||
SELECT regexp_replace('05/12/2018', '\d{4}', '2017');
|
SELECT regexp_replace('05/12/2018', '\d{4}', '2017');
|
||||||
|
|
||||||
@ -222,7 +222,7 @@ SELECT regexp_split_to_table('Four,score,and,seven,years,ago', ',');
|
|||||||
|
|
||||||
SELECT regexp_split_to_array('Phil Mike Tony Steve', ' ');
|
SELECT regexp_split_to_array('Phil Mike Tony Steve', ' ');
|
||||||
|
|
||||||
-- Listing 13-14: Finding an array length
|
-- Listing 14-14: Finding an array length
|
||||||
|
|
||||||
SELECT array_length(regexp_split_to_array('Phil Mike Tony Steve', ' '), 1);
|
SELECT array_length(regexp_split_to_array('Phil Mike Tony Steve', ' '), 1);
|
||||||
|
|
||||||
@ -234,21 +234,21 @@ SELECT array_length(regexp_split_to_array('Phil Mike Tony Steve', ' '), 1);
|
|||||||
-- | (OR)
|
-- | (OR)
|
||||||
-- ! (NOT)
|
-- ! (NOT)
|
||||||
|
|
||||||
-- Listing 13-15: Converting text to tsvector data
|
-- Listing 14-15: Converting text to tsvector data
|
||||||
|
|
||||||
SELECT to_tsvector('I am walking across the sitting room to sit with you.');
|
SELECT to_tsvector('I am walking across the sitting room to sit with you.');
|
||||||
|
|
||||||
-- Listing 13-16: Converting search terms to tsquery data
|
-- Listing 14-16: Converting search terms to tsquery data
|
||||||
|
|
||||||
SELECT to_tsquery('walking & sitting');
|
SELECT to_tsquery('walking & sitting');
|
||||||
|
|
||||||
-- Listing 13-17: Querying a tsvector type with a tsquery
|
-- Listing 14-17: Querying a tsvector type with a tsquery
|
||||||
|
|
||||||
SELECT to_tsvector('I am walking across the sitting room') @@ to_tsquery('walking & sitting');
|
SELECT to_tsvector('I am walking across the sitting room') @@ to_tsquery('walking & sitting');
|
||||||
|
|
||||||
SELECT to_tsvector('I am walking across the sitting room') @@ to_tsquery('walking & running');
|
SELECT to_tsvector('I am walking across the sitting room') @@ to_tsquery('walking & running');
|
||||||
|
|
||||||
-- Listing 13-18: Creating and filling the president_speeches table
|
-- Listing 14-18: Creating and filling the president_speeches table
|
||||||
|
|
||||||
-- Sources:
|
-- Sources:
|
||||||
-- https://archive.org/details/State-of-the-Union-Addresses-1945-2006
|
-- https://archive.org/details/State-of-the-Union-Addresses-1945-2006
|
||||||
@ -270,23 +270,23 @@ WITH (FORMAT CSV, DELIMITER '|', HEADER OFF, QUOTE '@');
|
|||||||
|
|
||||||
SELECT * FROM president_speeches;
|
SELECT * FROM president_speeches;
|
||||||
|
|
||||||
-- Listing 13-19: Converting speeches to tsvector in the search_speech_text column
|
-- Listing 14-19: Converting speeches to tsvector in the search_speech_text column
|
||||||
|
|
||||||
UPDATE president_speeches
|
UPDATE president_speeches
|
||||||
SET search_speech_text = to_tsvector('english', speech_text);
|
SET search_speech_text = to_tsvector('english', speech_text);
|
||||||
|
|
||||||
-- Listing 13-20: Creating a GIN index for text search
|
-- Listing 14-20: Creating a GIN index for text search
|
||||||
|
|
||||||
CREATE INDEX search_idx ON president_speeches USING gin(search_speech_text);
|
CREATE INDEX search_idx ON president_speeches USING gin(search_speech_text);
|
||||||
|
|
||||||
-- Listing 13-21: Finding speeches containing the word "Vietnam"
|
-- Listing 14-21: Finding speeches containing the word "Vietnam"
|
||||||
|
|
||||||
SELECT president, speech_date
|
SELECT president, speech_date
|
||||||
FROM president_speeches
|
FROM president_speeches
|
||||||
WHERE search_speech_text @@ to_tsquery('Vietnam')
|
WHERE search_speech_text @@ to_tsquery('Vietnam')
|
||||||
ORDER BY speech_date;
|
ORDER BY speech_date;
|
||||||
|
|
||||||
-- Listing 13-22: Displaying search results with ts_headline()
|
-- Listing 14-22: Displaying search results with ts_headline()
|
||||||
|
|
||||||
SELECT president,
|
SELECT president,
|
||||||
speech_date,
|
speech_date,
|
||||||
@ -299,7 +299,7 @@ SELECT president,
|
|||||||
FROM president_speeches
|
FROM president_speeches
|
||||||
WHERE search_speech_text @@ to_tsquery('Vietnam');
|
WHERE search_speech_text @@ to_tsquery('Vietnam');
|
||||||
|
|
||||||
-- Listing 13-23: Finding speeches with the word "transportation" but not "roads"
|
-- Listing 14-23: Finding speeches with the word "transportation" but not "roads"
|
||||||
|
|
||||||
SELECT president,
|
SELECT president,
|
||||||
speech_date,
|
speech_date,
|
||||||
@ -312,7 +312,7 @@ SELECT president,
|
|||||||
FROM president_speeches
|
FROM president_speeches
|
||||||
WHERE search_speech_text @@ to_tsquery('transportation & !roads');
|
WHERE search_speech_text @@ to_tsquery('transportation & !roads');
|
||||||
|
|
||||||
-- Listing 13-24: Find speeches where "defense" follows "military"
|
-- Listing 14-24: Find speeches where "defense" follows "military"
|
||||||
|
|
||||||
SELECT president,
|
SELECT president,
|
||||||
speech_date,
|
speech_date,
|
||||||
@ -337,7 +337,7 @@ SELECT president,
|
|||||||
FROM president_speeches
|
FROM president_speeches
|
||||||
WHERE search_speech_text @@ to_tsquery('military <2> defense');
|
WHERE search_speech_text @@ to_tsquery('military <2> defense');
|
||||||
|
|
||||||
-- Listing 13-25: Scoring relevance with ts_rank()
|
-- Listing 14-25: Scoring relevance with ts_rank()
|
||||||
|
|
||||||
SELECT president,
|
SELECT president,
|
||||||
speech_date,
|
speech_date,
|
||||||
@ -348,7 +348,7 @@ WHERE search_speech_text @@ to_tsquery('war & security & threat & enemy')
|
|||||||
ORDER BY score DESC
|
ORDER BY score DESC
|
||||||
LIMIT 5;
|
LIMIT 5;
|
||||||
|
|
||||||
-- Listing 13-26: Normalizing ts_rank() by speech length
|
-- Listing 14-26: Normalizing ts_rank() by speech length
|
||||||
|
|
||||||
SELECT president,
|
SELECT president,
|
||||||
speech_date,
|
speech_date,
|
||||||
|
|||||||
@ -2,27 +2,27 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 14 Code Examples
|
-- Chapter 15 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- Listing 14-1: Creating a gis_analysis database
|
-- Listing 15-1: Creating a gis_analysis database
|
||||||
|
|
||||||
CREATE DATABASE gis_analysis;
|
CREATE DATABASE gis_analysis;
|
||||||
-- Note: Switch to this new database before continuing the examples
|
-- Note: Switch to this new database before continuing the examples
|
||||||
|
|
||||||
-- Listing 14-2: Loading the PostGIS extension
|
-- Listing 15-2: Loading the PostGIS extension
|
||||||
|
|
||||||
CREATE EXTENSION postgis;
|
CREATE EXTENSION postgis;
|
||||||
|
|
||||||
SELECT postgis_full_version(); -- shows PostGIS version
|
SELECT postgis_full_version(); -- shows PostGIS version
|
||||||
|
|
||||||
-- Listing 14-3: Retrieving the well-known text for SRID 4326
|
-- Listing 15-3: Retrieving the well-known text for SRID 4326
|
||||||
|
|
||||||
SELECT srtext
|
SELECT srtext
|
||||||
FROM spatial_ref_sys
|
FROM spatial_ref_sys
|
||||||
WHERE srid = 4326;
|
WHERE srid = 4326;
|
||||||
|
|
||||||
-- Listing 14-4: Using ST_GeomFromText() to create spatial objects
|
-- Listing 15-4: Using ST_GeomFromText() to create spatial objects
|
||||||
|
|
||||||
SELECT ST_GeomFromText('POINT(-74.9233606 42.699992)', 4326);
|
SELECT ST_GeomFromText('POINT(-74.9233606 42.699992)', 4326);
|
||||||
|
|
||||||
@ -44,24 +44,24 @@ SELECT ST_GeomFromText('MULTIPOLYGON((
|
|||||||
-74.98 42.64, -74.98 42.66,
|
-74.98 42.64, -74.98 42.66,
|
||||||
-75.0 42.66)))', 4326);
|
-75.0 42.66)))', 4326);
|
||||||
|
|
||||||
-- Listing 14-5: Using ST_GeogFromText() to create spatial objects
|
-- Listing 15-5: Using ST_GeogFromText() to create spatial objects
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
ST_GeogFromText('SRID=4326;MULTIPOINT(-74.9 42.7, -75.1 42.7, -74.924 42.6)');
|
ST_GeogFromText('SRID=4326;MULTIPOINT(-74.9 42.7, -75.1 42.7, -74.924 42.6)');
|
||||||
|
|
||||||
-- Listing 14-6: Functions specific to making points
|
-- Listing 15-6: Functions specific to making points
|
||||||
|
|
||||||
SELECT ST_PointFromText('POINT(-74.9233606 42.699992)', 4326);
|
SELECT ST_PointFromText('POINT(-74.9233606 42.699992)', 4326);
|
||||||
|
|
||||||
SELECT ST_MakePoint(-74.9233606, 42.699992);
|
SELECT ST_MakePoint(-74.9233606, 42.699992);
|
||||||
SELECT ST_SetSRID(ST_MakePoint(-74.9233606, 42.699992), 4326);
|
SELECT ST_SetSRID(ST_MakePoint(-74.9233606, 42.699992), 4326);
|
||||||
|
|
||||||
-- Listing 14-7: Functions specific to making LineStrings
|
-- Listing 15-7: Functions specific to making LineStrings
|
||||||
|
|
||||||
SELECT ST_LineFromText('LINESTRING(-105.90 35.67,-105.91 35.67)', 4326);
|
SELECT ST_LineFromText('LINESTRING(-105.90 35.67,-105.91 35.67)', 4326);
|
||||||
SELECT ST_MakeLine(ST_MakePoint(-74.92, 42.69), ST_MakePoint(-74.12, 42.45));
|
SELECT ST_MakeLine(ST_MakePoint(-74.92, 42.69), ST_MakePoint(-74.12, 42.45));
|
||||||
|
|
||||||
-- Listing 14-8: Functions specific to making Polygons
|
-- Listing 15-8: Functions specific to making Polygons
|
||||||
|
|
||||||
SELECT ST_PolygonFromText('POLYGON((-74.9 42.7, -75.1 42.7,
|
SELECT ST_PolygonFromText('POLYGON((-74.9 42.7, -75.1 42.7,
|
||||||
-75.1 42.6, -74.9 42.7))', 4326);
|
-75.1 42.6, -74.9 42.7))', 4326);
|
||||||
@ -84,7 +84,7 @@ SELECT ST_MPolyFromText('MULTIPOLYGON((
|
|||||||
-- https://www.ams.usda.gov/local-food-directories/farmersmarkets
|
-- https://www.ams.usda.gov/local-food-directories/farmersmarkets
|
||||||
|
|
||||||
|
|
||||||
-- Listing 14-9: Create and load the farmers_markets table
|
-- Listing 15-9: Create and load the farmers_markets table
|
||||||
|
|
||||||
CREATE TABLE farmers_markets (
|
CREATE TABLE farmers_markets (
|
||||||
fmid bigint PRIMARY KEY,
|
fmid bigint PRIMARY KEY,
|
||||||
@ -105,7 +105,7 @@ WITH (FORMAT CSV, HEADER);
|
|||||||
|
|
||||||
SELECT count(*) FROM farmers_markets; -- should return 8,681 rows
|
SELECT count(*) FROM farmers_markets; -- should return 8,681 rows
|
||||||
|
|
||||||
-- Listing 14-10: Creating and indexing a geography column
|
-- Listing 15-10: Creating and indexing a geography column
|
||||||
-- There's also a function: https://postgis.net/docs/AddGeometryColumn.html
|
-- There's also a function: https://postgis.net/docs/AddGeometryColumn.html
|
||||||
|
|
||||||
-- Add column
|
-- Add column
|
||||||
@ -129,7 +129,7 @@ FROM farmers_markets
|
|||||||
WHERE longitude IS NOT NULL
|
WHERE longitude IS NOT NULL
|
||||||
LIMIT 5;
|
LIMIT 5;
|
||||||
|
|
||||||
-- Listing 14-11: Using ST_DWithin() to locate farmers' markets within 10 kilometers of a point
|
-- Listing 15-11: Using ST_DWithin() to locate farmers' markets within 10 kilometers of a point
|
||||||
|
|
||||||
SELECT market_name,
|
SELECT market_name,
|
||||||
city,
|
city,
|
||||||
@ -140,7 +140,7 @@ WHERE ST_DWithin(geog_point,
|
|||||||
10000)
|
10000)
|
||||||
ORDER BY market_name;
|
ORDER BY market_name;
|
||||||
|
|
||||||
-- Listing 14-12: Using ST_Distance() to calculate the miles between Yankee Stadium
|
-- Listing 15-12: Using ST_Distance() to calculate the miles between Yankee Stadium
|
||||||
-- and Citi Field (Mets)
|
-- and Citi Field (Mets)
|
||||||
-- 1609.344 meters/mile
|
-- 1609.344 meters/mile
|
||||||
|
|
||||||
@ -149,7 +149,7 @@ SELECT ST_Distance(
|
|||||||
ST_GeogFromText('POINT(-73.8480153 40.7570917)')
|
ST_GeogFromText('POINT(-73.8480153 40.7570917)')
|
||||||
) / 1609.344 AS mets_to_yanks;
|
) / 1609.344 AS mets_to_yanks;
|
||||||
|
|
||||||
-- Listing 14-13: Using ST_Distance() for each row in farmers_markets
|
-- Listing 15-13: Using ST_Distance() for each row in farmers_markets
|
||||||
|
|
||||||
SELECT market_name,
|
SELECT market_name,
|
||||||
city,
|
city,
|
||||||
@ -173,13 +173,13 @@ ORDER BY miles_from_dt ASC;
|
|||||||
-- Cartographic Boundary Shapefiles - Counties
|
-- Cartographic Boundary Shapefiles - Counties
|
||||||
-- https://www.census.gov/geo/maps-data/data/cbf/cbf_counties.html
|
-- https://www.census.gov/geo/maps-data/data/cbf/cbf_counties.html
|
||||||
|
|
||||||
-- Listing 14-14: Checking the geom column's well-known text representation
|
-- Listing 15-14: Checking the geom column's well-known text representation
|
||||||
|
|
||||||
SELECT ST_AsText(geom)
|
SELECT ST_AsText(geom)
|
||||||
FROM us_counties_2010_shp
|
FROM us_counties_2010_shp
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
|
|
||||||
-- Listing 14-15: Find the largest counties by area using ST_Area()
|
-- Listing 15-15: Find the largest counties by area using ST_Area()
|
||||||
|
|
||||||
SELECT name10,
|
SELECT name10,
|
||||||
statefp10 AS st,
|
statefp10 AS st,
|
||||||
@ -190,7 +190,7 @@ FROM us_counties_2010_shp
|
|||||||
ORDER BY square_miles DESC
|
ORDER BY square_miles DESC
|
||||||
LIMIT 5;
|
LIMIT 5;
|
||||||
|
|
||||||
-- Listing 14-16: Using ST_Within() to find the county belonging to a pair of coordinates
|
-- Listing 15-16: Using ST_Within() to find the county belonging to a pair of coordinates
|
||||||
|
|
||||||
SELECT name10,
|
SELECT name10,
|
||||||
statefp10
|
statefp10
|
||||||
@ -219,7 +219,7 @@ WHERE ST_Within('SRID=4269;POINT(-118.3419063 34.0977076)'::geometry, geom);
|
|||||||
-- https://www.census.gov/geo/reference/mtfcc.html
|
-- https://www.census.gov/geo/reference/mtfcc.html
|
||||||
-- Here, H3010: A natural flowing waterway
|
-- Here, H3010: A natural flowing waterway
|
||||||
|
|
||||||
-- Listing 14-17: Using ST_GeometryType() to determine geometry
|
-- Listing 15-17: Using ST_GeometryType() to determine geometry
|
||||||
|
|
||||||
SELECT ST_GeometryType(geom)
|
SELECT ST_GeometryType(geom)
|
||||||
FROM santafe_linearwater_2016
|
FROM santafe_linearwater_2016
|
||||||
@ -229,7 +229,7 @@ SELECT ST_GeometryType(geom)
|
|||||||
FROM santafe_roads_2016
|
FROM santafe_roads_2016
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
|
|
||||||
-- Listing 14-18: Spatial join with ST_Intersects() to find roads crossing the Santa Fe river
|
-- Listing 15-18: Spatial join with ST_Intersects() to find roads crossing the Santa Fe river
|
||||||
|
|
||||||
SELECT water.fullname AS waterway,
|
SELECT water.fullname AS waterway,
|
||||||
roads.rttyp,
|
roads.rttyp,
|
||||||
@ -239,7 +239,7 @@ FROM santafe_linearwater_2016 water JOIN santafe_roads_2016 roads
|
|||||||
WHERE water.fullname = 'Santa Fe Riv'
|
WHERE water.fullname = 'Santa Fe Riv'
|
||||||
ORDER BY roads.fullname;
|
ORDER BY roads.fullname;
|
||||||
|
|
||||||
-- Listing 14-19: Using ST_Intersection() to show where roads cross the river
|
-- Listing 15-19: Using ST_Intersection() to show where roads cross the river
|
||||||
|
|
||||||
SELECT water.fullname AS waterway,
|
SELECT water.fullname AS waterway,
|
||||||
roads.rttyp,
|
roads.rttyp,
|
||||||
|
|||||||
@ -2,12 +2,12 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 15 Code Examples
|
-- Chapter 17 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- VIEWS
|
-- VIEWS
|
||||||
|
|
||||||
-- Listing 15-1: Creating a view that displays Nevada 2010 counties
|
-- Listing 17-1: Creating a view that displays Nevada 2010 counties
|
||||||
|
|
||||||
CREATE OR REPLACE VIEW nevada_counties_pop_2010 AS
|
CREATE OR REPLACE VIEW nevada_counties_pop_2010 AS
|
||||||
SELECT geo_name,
|
SELECT geo_name,
|
||||||
@ -18,13 +18,13 @@ CREATE OR REPLACE VIEW nevada_counties_pop_2010 AS
|
|||||||
WHERE state_us_abbreviation = 'NV'
|
WHERE state_us_abbreviation = 'NV'
|
||||||
ORDER BY county_fips;
|
ORDER BY county_fips;
|
||||||
|
|
||||||
-- Listing 15-2: Querying the nevada_counties_pop_2010 view
|
-- Listing 17-2: Querying the nevada_counties_pop_2010 view
|
||||||
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM nevada_counties_pop_2010
|
FROM nevada_counties_pop_2010
|
||||||
LIMIT 5;
|
LIMIT 5;
|
||||||
|
|
||||||
-- Listing 15-3: Creating a view showing population change for US counties
|
-- Listing 17-3: Creating a view showing population change for US counties
|
||||||
|
|
||||||
CREATE OR REPLACE VIEW county_pop_change_2010_2000 AS
|
CREATE OR REPLACE VIEW county_pop_change_2010_2000 AS
|
||||||
SELECT c2010.geo_name,
|
SELECT c2010.geo_name,
|
||||||
@ -40,7 +40,7 @@ CREATE OR REPLACE VIEW county_pop_change_2010_2000 AS
|
|||||||
AND c2010.county_fips = c2000.county_fips
|
AND c2010.county_fips = c2000.county_fips
|
||||||
ORDER BY c2010.state_fips, c2010.county_fips;
|
ORDER BY c2010.state_fips, c2010.county_fips;
|
||||||
|
|
||||||
-- Listing 15-4: Selecting columns from the county_pop_change_2010_2000 view
|
-- Listing 17-4: Selecting columns from the county_pop_change_2010_2000 view
|
||||||
|
|
||||||
SELECT geo_name,
|
SELECT geo_name,
|
||||||
st,
|
st,
|
||||||
@ -50,7 +50,7 @@ FROM county_pop_change_2010_2000
|
|||||||
WHERE st = 'NV'
|
WHERE st = 'NV'
|
||||||
LIMIT 5;
|
LIMIT 5;
|
||||||
|
|
||||||
-- Listing 15-5: Creating a view on the employees table
|
-- Listing 17-5: Creating a view on the employees table
|
||||||
|
|
||||||
CREATE OR REPLACE VIEW employees_tax_dept AS
|
CREATE OR REPLACE VIEW employees_tax_dept AS
|
||||||
SELECT emp_id,
|
SELECT emp_id,
|
||||||
@ -64,7 +64,7 @@ CREATE OR REPLACE VIEW employees_tax_dept AS
|
|||||||
|
|
||||||
SELECT * FROM employees_tax_dept;
|
SELECT * FROM employees_tax_dept;
|
||||||
|
|
||||||
-- Listing 15-6: Successful and rejected inserts via the employees_tax_dept view
|
-- Listing 17-6: Successful and rejected inserts via the employees_tax_dept view
|
||||||
|
|
||||||
INSERT INTO employees_tax_dept (first_name, last_name, dept_id)
|
INSERT INTO employees_tax_dept (first_name, last_name, dept_id)
|
||||||
VALUES ('Suzanne', 'Legere', 1);
|
VALUES ('Suzanne', 'Legere', 1);
|
||||||
@ -77,7 +77,7 @@ SELECT * FROM employees_tax_dept;
|
|||||||
|
|
||||||
SELECT * FROM employees;
|
SELECT * FROM employees;
|
||||||
|
|
||||||
-- Listing 15-7: Updating a row via the employees_tax_dept view
|
-- Listing 17-7: Updating a row via the employees_tax_dept view
|
||||||
|
|
||||||
UPDATE employees_tax_dept
|
UPDATE employees_tax_dept
|
||||||
SET last_name = 'Le Gere'
|
SET last_name = 'Le Gere'
|
||||||
@ -90,7 +90,7 @@ UPDATE employees_tax_dept
|
|||||||
SET salary = 100000
|
SET salary = 100000
|
||||||
WHERE emp_id = 5;
|
WHERE emp_id = 5;
|
||||||
|
|
||||||
-- Listing 15-8: Deleting a row via the employees_tax_dept view
|
-- Listing 17-8: Deleting a row via the employees_tax_dept view
|
||||||
|
|
||||||
DELETE FROM employees_tax_dept
|
DELETE FROM employees_tax_dept
|
||||||
WHERE emp_id = 5;
|
WHERE emp_id = 5;
|
||||||
@ -99,7 +99,7 @@ WHERE emp_id = 5;
|
|||||||
-- FUNCTIONS
|
-- FUNCTIONS
|
||||||
-- https://www.postgresql.org/docs/current/static/plpgsql.html
|
-- https://www.postgresql.org/docs/current/static/plpgsql.html
|
||||||
|
|
||||||
-- Listing 15-9: Creating a percent_change function
|
-- Listing 17-9: Creating a percent_change function
|
||||||
-- To delete this function: DROP FUNCTION percent_change(numeric,numeric,integer);
|
-- To delete this function: DROP FUNCTION percent_change(numeric,numeric,integer);
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION
|
CREATE OR REPLACE FUNCTION
|
||||||
@ -114,11 +114,11 @@ LANGUAGE SQL
|
|||||||
IMMUTABLE
|
IMMUTABLE
|
||||||
RETURNS NULL ON NULL INPUT;
|
RETURNS NULL ON NULL INPUT;
|
||||||
|
|
||||||
-- Listing 15-10: Testing the percent_change() function
|
-- Listing 17-10: Testing the percent_change() function
|
||||||
|
|
||||||
SELECT percent_change(110, 108, 2);
|
SELECT percent_change(110, 108, 2);
|
||||||
|
|
||||||
-- Listing 15-11: Testing percent_change() on Census data
|
-- Listing 17-11: Testing percent_change() on Census data
|
||||||
|
|
||||||
SELECT c2010.geo_name,
|
SELECT c2010.geo_name,
|
||||||
c2010.state_us_abbreviation AS st,
|
c2010.state_us_abbreviation AS st,
|
||||||
@ -132,7 +132,7 @@ ON c2010.state_fips = c2000.state_fips
|
|||||||
ORDER BY pct_chg_func DESC
|
ORDER BY pct_chg_func DESC
|
||||||
LIMIT 5;
|
LIMIT 5;
|
||||||
|
|
||||||
-- Listing 15-12: Adding a column to the teachers table and seeing the data
|
-- Listing 17-12: Adding a column to the teachers table and seeing the data
|
||||||
|
|
||||||
ALTER TABLE teachers ADD COLUMN personal_days integer;
|
ALTER TABLE teachers ADD COLUMN personal_days integer;
|
||||||
|
|
||||||
@ -142,7 +142,7 @@ SELECT first_name,
|
|||||||
personal_days
|
personal_days
|
||||||
FROM teachers;
|
FROM teachers;
|
||||||
|
|
||||||
-- Listing 15-13: Creating an update_personal_days() function
|
-- Listing 17-13: Creating an update_personal_days() function
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION update_personal_days()
|
CREATE OR REPLACE FUNCTION update_personal_days()
|
||||||
RETURNS void AS $$
|
RETURNS void AS $$
|
||||||
@ -161,11 +161,11 @@ $$ LANGUAGE plpgsql;
|
|||||||
-- To run the function:
|
-- To run the function:
|
||||||
SELECT update_personal_days();
|
SELECT update_personal_days();
|
||||||
|
|
||||||
-- Listing 15-14: Enabling the PL/Python procedural language
|
-- Listing 17-14: Enabling the PL/Python procedural language
|
||||||
|
|
||||||
CREATE EXTENSION plpythonu;
|
CREATE EXTENSION plpythonu;
|
||||||
|
|
||||||
-- Listing 15-15: Using PL/Python to create the trim_county() function
|
-- Listing 17-15: Using PL/Python to create the trim_county() function
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION trim_county(input_string text)
|
CREATE OR REPLACE FUNCTION trim_county(input_string text)
|
||||||
RETURNS text AS $$
|
RETURNS text AS $$
|
||||||
@ -174,7 +174,7 @@ RETURNS text AS $$
|
|||||||
return cleaned
|
return cleaned
|
||||||
$$ LANGUAGE plpythonu;
|
$$ LANGUAGE plpythonu;
|
||||||
|
|
||||||
-- Listing 15-16: Testing the trim_county() function
|
-- Listing 17-16: Testing the trim_county() function
|
||||||
|
|
||||||
SELECT geo_name,
|
SELECT geo_name,
|
||||||
trim_county(geo_name)
|
trim_county(geo_name)
|
||||||
@ -185,7 +185,7 @@ LIMIT 5;
|
|||||||
|
|
||||||
-- TRIGGERS
|
-- TRIGGERS
|
||||||
|
|
||||||
-- Listing 15-17: Creating the grades and grades_history tables
|
-- Listing 17-17: Creating the grades and grades_history tables
|
||||||
|
|
||||||
CREATE TABLE grades (
|
CREATE TABLE grades (
|
||||||
student_id bigint,
|
student_id bigint,
|
||||||
@ -212,7 +212,7 @@ CREATE TABLE grades_history (
|
|||||||
PRIMARY KEY (student_id, course_id, change_time)
|
PRIMARY KEY (student_id, course_id, change_time)
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Listing 15-18: Creating the record_if_grade_changed() function
|
-- Listing 17-18: Creating the record_if_grade_changed() function
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION record_if_grade_changed()
|
CREATE OR REPLACE FUNCTION record_if_grade_changed()
|
||||||
RETURNS trigger AS
|
RETURNS trigger AS
|
||||||
@ -238,7 +238,7 @@ BEGIN
|
|||||||
END;
|
END;
|
||||||
$$ LANGUAGE plpgsql;
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
-- Listing 15-19: Creating the grades_update trigger
|
-- Listing 17-19: Creating the grades_update trigger
|
||||||
|
|
||||||
CREATE TRIGGER grades_update
|
CREATE TRIGGER grades_update
|
||||||
AFTER UPDATE
|
AFTER UPDATE
|
||||||
@ -246,7 +246,7 @@ CREATE TRIGGER grades_update
|
|||||||
FOR EACH ROW
|
FOR EACH ROW
|
||||||
EXECUTE PROCEDURE record_if_grade_changed();
|
EXECUTE PROCEDURE record_if_grade_changed();
|
||||||
|
|
||||||
-- Listing 15-20: Testing the grades_update trigger
|
-- Listing 17-20: Testing the grades_update trigger
|
||||||
|
|
||||||
-- Initially, there should be 0 records in the history
|
-- Initially, there should be 0 records in the history
|
||||||
SELECT * FROM grades_history;
|
SELECT * FROM grades_history;
|
||||||
@ -267,7 +267,7 @@ SELECT student_id,
|
|||||||
new_grade
|
new_grade
|
||||||
FROM grades_history;
|
FROM grades_history;
|
||||||
|
|
||||||
-- Listing 15-21: Creating a temperature_test table
|
-- Listing 17-21: Creating a temperature_test table
|
||||||
|
|
||||||
CREATE TABLE temperature_test (
|
CREATE TABLE temperature_test (
|
||||||
station_name varchar(50),
|
station_name varchar(50),
|
||||||
@ -278,7 +278,7 @@ CREATE TABLE temperature_test (
|
|||||||
PRIMARY KEY (station_name, observation_date)
|
PRIMARY KEY (station_name, observation_date)
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Listing 15-22: Creating the classify_max_temp() function
|
-- Listing 17-22: Creating the classify_max_temp() function
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION classify_max_temp()
|
CREATE OR REPLACE FUNCTION classify_max_temp()
|
||||||
RETURNS trigger AS
|
RETURNS trigger AS
|
||||||
@ -301,7 +301,7 @@ BEGIN
|
|||||||
END;
|
END;
|
||||||
$$ LANGUAGE plpgsql;
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
-- Listing 15-23: Creating the temperature_insert trigger
|
-- Listing 17-23: Creating the temperature_insert trigger
|
||||||
|
|
||||||
CREATE TRIGGER temperature_insert
|
CREATE TRIGGER temperature_insert
|
||||||
BEFORE INSERT
|
BEFORE INSERT
|
||||||
@ -309,7 +309,7 @@ CREATE TRIGGER temperature_insert
|
|||||||
FOR EACH ROW
|
FOR EACH ROW
|
||||||
EXECUTE PROCEDURE classify_max_temp();
|
EXECUTE PROCEDURE classify_max_temp();
|
||||||
|
|
||||||
-- Listing 15-24: Inserting rows to test the temperature_update trigger
|
-- Listing 17-24: Inserting rows to test the temperature_update trigger
|
||||||
|
|
||||||
INSERT INTO temperature_test (station_name, observation_date, max_temp, min_temp)
|
INSERT INTO temperature_test (station_name, observation_date, max_temp, min_temp)
|
||||||
VALUES
|
VALUES
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 16 Code Examples
|
-- Chapter 18 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
@ -16,12 +16,12 @@ psql -d analysis -U postgres
|
|||||||
\c [database name] [user name]
|
\c [database name] [user name]
|
||||||
\c gis_analysis postgres
|
\c gis_analysis postgres
|
||||||
|
|
||||||
-- Listing 16-1: Entering a single-line query in psql
|
-- Listing 18-1: Entering a single-line query in psql
|
||||||
-- Enter this at the psql prompt:
|
-- Enter this at the psql prompt:
|
||||||
|
|
||||||
SELECT geo_name FROM us_counties_2010 LIMIT 3;
|
SELECT geo_name FROM us_counties_2010 LIMIT 3;
|
||||||
|
|
||||||
-- Listing 16-2: Entering a multi-line query in psql
|
-- Listing 18-2: Entering a multi-line query in psql
|
||||||
-- Type each line separately, followed by Enter
|
-- Type each line separately, followed by Enter
|
||||||
|
|
||||||
SELECT geo_name
|
SELECT geo_name
|
||||||
@ -29,23 +29,23 @@ FROM us_counties_2010
|
|||||||
LIMIT 3;
|
LIMIT 3;
|
||||||
|
|
||||||
|
|
||||||
-- Listing 16-3: Showing open parentheses in the psql prompt
|
-- Listing 18-3: Showing open parentheses in the psql prompt
|
||||||
|
|
||||||
CREATE TABLE wineries (
|
CREATE TABLE wineries (
|
||||||
id bigint,
|
id bigint,
|
||||||
winery_name varchar(100)
|
winery_name varchar(100)
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Listing 16-4: A query with scrolling results
|
-- Listing 18-4: A query with scrolling results
|
||||||
|
|
||||||
SELECT geo_name FROM us_counties_2010;
|
SELECT geo_name FROM us_counties_2010;
|
||||||
|
|
||||||
-- Listings 16-5 and 16-6: Normal and expanded displays of results
|
-- Listings 18-5 and 18-6: Normal and expanded displays of results
|
||||||
-- Use \x to toggle expanded on/off
|
-- Use \x to toggle expanded on/off
|
||||||
|
|
||||||
SELECT * FROM grades;
|
SELECT * FROM grades;
|
||||||
|
|
||||||
-- Listing 16-7: Importing data using \copy
|
-- Listing 18-7: Importing data using \copy
|
||||||
|
|
||||||
DROP TABLE state_regions;
|
DROP TABLE state_regions;
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ CREATE TABLE state_regions (
|
|||||||
|
|
||||||
\copy state_regions FROM 'C:\YourDirectory\state_regions.csv' WITH (FORMAT CSV, HEADER);
|
\copy state_regions FROM 'C:\YourDirectory\state_regions.csv' WITH (FORMAT CSV, HEADER);
|
||||||
|
|
||||||
-- Listing 16-8: Saving query output to a file
|
-- Listing 18-8: Saving query output to a file
|
||||||
|
|
||||||
-- Enter psql settings
|
-- Enter psql settings
|
||||||
\a \f , \pset footer
|
\a \f , \pset footer
|
||||||
|
|||||||
@ -2,18 +2,18 @@
|
|||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 17 Code Examples
|
-- Chapter 19 Code Examples
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
-- VACUUM
|
-- VACUUM
|
||||||
|
|
||||||
-- Listing 17-1: Creating a table to test vacuuming
|
-- Listing 19-1: Creating a table to test vacuuming
|
||||||
|
|
||||||
CREATE TABLE vacuum_test (
|
CREATE TABLE vacuum_test (
|
||||||
integer_column integer
|
integer_column integer
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Listing 17-2: Determining the size of vacuum_test
|
-- Listing 19-2: Determining the size of vacuum_test
|
||||||
|
|
||||||
SELECT pg_size_pretty(
|
SELECT pg_size_pretty(
|
||||||
pg_total_relation_size('vacuum_test')
|
pg_total_relation_size('vacuum_test')
|
||||||
@ -24,7 +24,7 @@ SELECT pg_size_pretty(
|
|||||||
pg_database_size('analysis')
|
pg_database_size('analysis')
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Listing 17-3: Inserting 500,000 rows into vacuum_test
|
-- Listing 19-3: Inserting 500,000 rows into vacuum_test
|
||||||
|
|
||||||
INSERT INTO vacuum_test
|
INSERT INTO vacuum_test
|
||||||
SELECT * FROM generate_series(1,500000);
|
SELECT * FROM generate_series(1,500000);
|
||||||
@ -34,7 +34,7 @@ SELECT pg_size_pretty(
|
|||||||
pg_table_size('vacuum_test')
|
pg_table_size('vacuum_test')
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Listing 17-4: Updating all rows in vacuum_test
|
-- Listing 19-4: Updating all rows in vacuum_test
|
||||||
|
|
||||||
UPDATE vacuum_test
|
UPDATE vacuum_test
|
||||||
SET integer_column = integer_column + 1;
|
SET integer_column = integer_column + 1;
|
||||||
@ -44,7 +44,7 @@ SELECT pg_size_pretty(
|
|||||||
pg_table_size('vacuum_test')
|
pg_table_size('vacuum_test')
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Listing 17-5: Viewing autovacuum statistics for vacuum_test
|
-- Listing 19-5: Viewing autovacuum statistics for vacuum_test
|
||||||
|
|
||||||
SELECT relname,
|
SELECT relname,
|
||||||
last_vacuum,
|
last_vacuum,
|
||||||
@ -59,7 +59,7 @@ SELECT *
|
|||||||
FROM pg_stat_all_tables
|
FROM pg_stat_all_tables
|
||||||
WHERE relname = 'vacuum_test';
|
WHERE relname = 'vacuum_test';
|
||||||
|
|
||||||
-- Listing 17-6: Running VACUUM manually
|
-- Listing 19-6: Running VACUUM manually
|
||||||
|
|
||||||
VACUUM vacuum_test;
|
VACUUM vacuum_test;
|
||||||
|
|
||||||
@ -67,7 +67,7 @@ VACUUM; -- vacuums the whole database
|
|||||||
|
|
||||||
VACUUM VERBOSE; -- provides messages
|
VACUUM VERBOSE; -- provides messages
|
||||||
|
|
||||||
-- Listing 17-7: Using VACUUM FULL to reclaim disk space
|
-- Listing 19-7: Using VACUUM FULL to reclaim disk space
|
||||||
|
|
||||||
VACUUM FULL vacuum_test;
|
VACUUM FULL vacuum_test;
|
||||||
|
|
||||||
@ -78,11 +78,11 @@ SELECT pg_size_pretty(
|
|||||||
|
|
||||||
-- SETTINGS
|
-- SETTINGS
|
||||||
|
|
||||||
-- Listing 17-8: Showing the location of postgresql.conf
|
-- Listing 19-8: Showing the location of postgresql.conf
|
||||||
|
|
||||||
SHOW config_file;
|
SHOW config_file;
|
||||||
|
|
||||||
-- Listing 17-10: Show the location of the data directory
|
-- Listing 19-10: Show the location of the data directory
|
||||||
|
|
||||||
SHOW data_directory;
|
SHOW data_directory;
|
||||||
|
|
||||||
@ -93,12 +93,12 @@ SHOW data_directory;
|
|||||||
|
|
||||||
-- BACKUP AND RESTORE
|
-- BACKUP AND RESTORE
|
||||||
|
|
||||||
-- Listing 17-11: Backing up the analysis database with pg_dump
|
-- Listing 19-11: Backing up the analysis database with pg_dump
|
||||||
pg_dump -d analysis -U [user_name] -Fc > analysis_backup.sql
|
pg_dump -d analysis -U [user_name] -Fc > analysis_backup.sql
|
||||||
|
|
||||||
-- Back up just a table
|
-- Back up just a table
|
||||||
pg_dump -t 'train_rides' -d analysis -U [user_name] -Fc > train_backup.sql
|
pg_dump -t 'train_rides' -d analysis -U [user_name] -Fc > train_backup.sql
|
||||||
|
|
||||||
-- Listing 17-12: Restoring the analysis database with pg_restore
|
-- Listing 19-12: Restoring the analysis database with pg_restore
|
||||||
|
|
||||||
pg_restore -C -d postgres -U postgres analysis_backup_custom.sql
|
pg_restore -C -d postgres -U postgres analysis_backup_custom.sql
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user