Chapter 3 updates post tech review; Chapter 7 WIP
This commit is contained in:
parent
58cfdea31d
commit
16e22e90ff
@ -9,33 +9,46 @@
|
|||||||
|
|
||||||
SELECT * FROM teachers;
|
SELECT * FROM teachers;
|
||||||
|
|
||||||
|
-- Note that this standard SQL shorthand also works:
|
||||||
|
|
||||||
|
TABLE teachers;
|
||||||
|
|
||||||
-- Listing 3-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 3-3: Querying distinct values in the school column
|
-- Listing 3-3: Sorting a column with ORDER BY
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
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 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
|
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 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
|
-- Listing 3-7: Filtering rows using WHERE
|
||||||
|
|
||||||
SELECT last_name, school, hire_date
|
SELECT last_name, school, hire_date
|
||||||
@ -52,7 +65,7 @@ WHERE first_name = 'Janet';
|
|||||||
-- School names not equal to F.D. Roosevelt HS
|
-- School names not equal to F.D. Roosevelt HS
|
||||||
SELECT school
|
SELECT school
|
||||||
FROM teachers
|
FROM teachers
|
||||||
WHERE school != 'F.D. Roosevelt HS';
|
WHERE school <> 'F.D. Roosevelt HS';
|
||||||
|
|
||||||
-- Teachers hired before Jan. 1, 2000
|
-- Teachers hired before Jan. 1, 2000
|
||||||
SELECT first_name, last_name, hire_date
|
SELECT first_name, last_name, hire_date
|
||||||
@ -69,6 +82,10 @@ SELECT first_name, last_name, school, salary
|
|||||||
FROM teachers
|
FROM teachers
|
||||||
WHERE salary BETWEEN 40000 AND 65000;
|
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
|
-- Listing 3-8: Filtering with LIKE AND ILIKE
|
||||||
|
|
||||||
SELECT first_name
|
SELECT first_name
|
||||||
|
|||||||
@ -1,47 +1,42 @@
|
|||||||
-- FIRST EDITION FILE; IGNORE
|
---------------------------------------------------------------------------
|
||||||
|
-- Practical SQL: A Beginner's Guide to Storytelling with Data, 2nd Edition
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------
|
|
||||||
-- Practical SQL: A Beginner's Guide to Storytelling with Data
|
|
||||||
-- by Anthony DeBarros
|
-- by Anthony DeBarros
|
||||||
|
|
||||||
-- Chapter 7 Code Examples
|
-- Chapter 7 Code Examples
|
||||||
--------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
-- Listing 7-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 integer,
|
||||||
dept varchar(100),
|
dept text,
|
||||||
city varchar(100),
|
city text,
|
||||||
CONSTRAINT dept_key PRIMARY KEY (dept_id),
|
CONSTRAINT dept_key PRIMARY KEY (dept_id),
|
||||||
CONSTRAINT dept_city_unique UNIQUE (dept, city)
|
CONSTRAINT dept_city_unique UNIQUE (dept, city)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE employees (
|
CREATE TABLE employees (
|
||||||
emp_id bigserial,
|
emp_id integer,
|
||||||
first_name varchar(100),
|
first_name text,
|
||||||
last_name varchar(100),
|
last_name text,
|
||||||
salary integer,
|
salary numeric(10,2),
|
||||||
dept_id integer REFERENCES departments (dept_id),
|
dept_id integer REFERENCES departments (dept_id),
|
||||||
CONSTRAINT emp_key PRIMARY KEY (emp_id),
|
CONSTRAINT emp_key PRIMARY KEY (emp_id),
|
||||||
CONSTRAINT emp_dept_unique UNIQUE (emp_id, dept_id)
|
CONSTRAINT emp_dept_unique UNIQUE (emp_id, dept_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
INSERT INTO departments (dept, city)
|
INSERT INTO departments
|
||||||
VALUES
|
VALUES
|
||||||
('Tax', 'Atlanta'),
|
(1, 'Tax', 'Atlanta'),
|
||||||
('IT', 'Boston');
|
(2, 'IT', 'Boston');
|
||||||
|
|
||||||
INSERT INTO employees (first_name, last_name, salary, dept_id)
|
INSERT INTO employees
|
||||||
VALUES
|
VALUES
|
||||||
('Nancy', 'Jones', 62500, 1),
|
(1, 'Julia', 'Reyes', 115300, 1),
|
||||||
('Lee', 'Smith', 59300, 1),
|
(2, 'Janet', 'King', 98000, 1),
|
||||||
('Soo', 'Nguyen', 83000, 2),
|
(3, 'Arthur', 'Pappas', 72700, 2),
|
||||||
('Janet', 'King', 95000, 2);
|
(4, 'Michael', 'Taylor', 89500, 2);
|
||||||
|
|
||||||
-- Listing 7-2: Joining the employees and departments tables
|
-- Listing 7-2: Joining the employees and departments tables
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user