Chapter 3 updates post tech review; Chapter 7 WIP

This commit is contained in:
anthonydb 2020-08-02 13:40:54 -04:00
parent 58cfdea31d
commit 16e22e90ff
2 changed files with 51 additions and 39 deletions

View File

@ -9,33 +9,46 @@
SELECT * FROM teachers;
-- Note that this standard SQL shorthand also works:
TABLE teachers;
-- Listing 3-2: Querying a subset of columns
SELECT last_name, first_name, salary FROM teachers;
-- Listing 3-3: Querying distinct values in the school column
SELECT DISTINCT school
FROM teachers;
-- Listing 3-4: Querying distinct pairs of values in the school and salary
-- columns
SELECT DISTINCT school, salary
FROM teachers;
-- Listing 3-5: Sorting a column with ORDER BY
-- Listing 3-3: Sorting a column with ORDER BY
SELECT first_name, last_name, salary
FROM teachers
ORDER BY salary DESC;
-- Listing 3-6: Sorting multiple columns with ORDER BY
-- Note you can also specify the sort column by
-- using a number representing its position in the result.
SELECT first_name, last_name, salary
FROM teachers
ORDER BY 3 DESC;
-- Listing 3-4: Sorting multiple columns with ORDER BY
SELECT last_name, school, hire_date
FROM teachers
ORDER BY school ASC, hire_date DESC;
-- Listing 3-5: Querying distinct values in the school column
SELECT DISTINCT school
FROM teachers
ORDER BY school;
-- Listing 3-6: Querying distinct pairs of values in the school and salary
-- columns
SELECT DISTINCT school, salary
FROM teachers
ORDER BY school, salary;
-- Listing 3-7: Filtering rows using WHERE
SELECT last_name, school, hire_date
@ -52,7 +65,7 @@ WHERE first_name = 'Janet';
-- School names not equal to F.D. Roosevelt HS
SELECT school
FROM teachers
WHERE school != 'F.D. Roosevelt HS';
WHERE school <> 'F.D. Roosevelt HS';
-- Teachers hired before Jan. 1, 2000
SELECT first_name, last_name, hire_date
@ -69,6 +82,10 @@ SELECT first_name, last_name, school, salary
FROM teachers
WHERE salary BETWEEN 40000 AND 65000;
SELECT first_name, last_name, school, salary
FROM teachers
WHERE salary >= 40000 AND salary < 65001;
-- Listing 3-8: Filtering with LIKE AND ILIKE
SELECT first_name

View File

@ -1,47 +1,42 @@
-- FIRST EDITION FILE; IGNORE
--------------------------------------------------------------
-- Practical SQL: A Beginner's Guide to Storytelling with Data
---------------------------------------------------------------------------
-- Practical SQL: A Beginner's Guide to Storytelling with Data, 2nd Edition
-- by Anthony DeBarros
-- Chapter 7 Code Examples
--------------------------------------------------------------
----------------------------------------------------------------------------
-- Listing 7-1: Creating the departments and employees tables
CREATE TABLE departments (
dept_id bigserial,
dept varchar(100),
city varchar(100),
dept_id integer,
dept text,
city text,
CONSTRAINT dept_key PRIMARY KEY (dept_id),
CONSTRAINT dept_city_unique UNIQUE (dept, city)
);
CREATE TABLE employees (
emp_id bigserial,
first_name varchar(100),
last_name varchar(100),
salary integer,
emp_id integer,
first_name text,
last_name text,
salary numeric(10,2),
dept_id integer REFERENCES departments (dept_id),
CONSTRAINT emp_key PRIMARY KEY (emp_id),
CONSTRAINT emp_dept_unique UNIQUE (emp_id, dept_id)
);
INSERT INTO departments (dept, city)
INSERT INTO departments
VALUES
('Tax', 'Atlanta'),
('IT', 'Boston');
(1, 'Tax', 'Atlanta'),
(2, 'IT', 'Boston');
INSERT INTO employees (first_name, last_name, salary, dept_id)
INSERT INTO employees
VALUES
('Nancy', 'Jones', 62500, 1),
('Lee', 'Smith', 59300, 1),
('Soo', 'Nguyen', 83000, 2),
('Janet', 'King', 95000, 2);
(1, 'Julia', 'Reyes', 115300, 1),
(2, 'Janet', 'King', 98000, 1),
(3, 'Arthur', 'Pappas', 72700, 2),
(4, 'Michael', 'Taylor', 89500, 2);
-- Listing 7-2: Joining the employees and departments tables