- Introduction
- Constructing the source data set
- Filtering with WHERE
- Grouping and group filtering
- Ordering and paging
- Conclusion
-
I'll use the term 'Database' for 'Relational Database Management System'
- SQLite
- PostgreSQL
- Oracle
- Microsoft SQL Server
- MySQL
-
I'll use MS SQL Server
-
DBeaver
- https://dbeaver.io/
- Free multi-platform database tool for developers, database administrators, analysts and all people who need to work with databases. Supports all popular databases: MySQL, PostgreSQL, SQLite, Oracle, DB2, SQL Server, Sybase, MS Access, Teradata, Firebird, Apache Hive, Phoenix, Presto, etc.
-
DB fiddle
- Animal Shelter demo database
- SQL query logical processing order
- FROM
-
- Source Data
-
- WHERE
-
- Row Filter
-
- GROUP BY
-
- Grouping
-
- HAVING
-
- Group Filter
-
- SELECT
-
- Return Expressions
-
- ORDER BY
-
- Presentation Order
-
- OFFSET FETCH
-
- Paging
-
- FROM
-
FROM-less SELECT
SELECT 'Hello World!'
Dummy Column Dummy Value => no alias 'Hello World!'
-
Source Data Set Requirements
- Each set is uniquely aliased
- Each column is uniquely aliased
- Sets are unordered
-
Side note:
- The relational model deals exclusively with sets. Sets consist of unique elements.
- SQL allows for "sets" with duplicate rows called "multi-sets", or "bags".
-
Single Data Source
SELECT Foo FROM XXX
XXX
can be anything compliant:- table
- view
- function
- subquery derived table
-
Single Source Data Set
SELECT Foo FROM Bar;
-
Step 1 - Cartesian Product
-
Step 2 - Qualification
-
Step 3 - Left Reservations
-
Step 3 - Right Reservation
-
Step 3 - Full Reservations
-
Join Syntax Shortcuts
-
A INNER JOIN B ON...
- A JOIN B ON...
-
A LEFT OUTER JOIN B ON...
- A LEFT JOIN B ON...
-
A RIGHT OUTER JOIN B ON...
- A RIGHT JOIN B ON...
-
A INNER JOIN B ON A.X = B.X
- A INNER JOIN B USING (X)
-
Avoid natural join
- A NATURAL JOIN B
- Assuming X is the only common alias!
- Natural Join: Guidelines
- The associated tables have one or more pairs of identically named columns.
- The columns must be the same data type.
- Don’t use ON clause in a natural join.
- A NATURAL JOIN B
-
SELECT Foo, Bar
FROM A INNER JOIN B
ON A.X = B.X
INNER JOIN C
ON C.Y = B.Y
INNER JOIN D
ON D.Z = C.Z
-
Chiastic Order
( A INNER JOIN ( B INNER JOIN ( C INNER JOIN D ON D.Z = C.Z) ON C.Y = B.Y) ON A.X = B.X)
-
When the going gets tough, the tough get going.
- Challenge - Animal vaccinations report
- Write a query to report animals and their vaccinations.
- Include animals that have not been vaccinated.
- The report should show the animal's name, species, breed, and primary color, vaccination time and the vaccine name, the staff member's first name, last name, and role.
- Guidelines:
- Use the minimal number of tables required.
- Use the correct logical join types and force join order as needed.
-
WHERE Processing
-
JOIN vs. WHERE - Round 1
-
JOIN vs. WHERE - Round 2
-
NULL is
NOT
a value.- It is an indicator to the absence of a value.
-
NULL Types
- A-Values
- "The value exists, we just don't know it."
- I-Values
- "This attribute is irrelevant for this tuple."
- A-Values
-
Ternary Logic Evaluation
a b a OR b a AND b a = b NOT a True True True True True False True False True False False False True Unknown True Unknown Unknown False False True True False False True False Flase False False True True False Unknown Unknown False Unknown True Unknown True True Unknown Unknown Unknown Unknown False Unknown False Unknown Unknown Unknown Unknown Unknown Unknown Unknown Unknown
-
After the data set is groupd, we can only reference the grouping expressions since they are guaranteed to have the same value for all rows within a group.
-
All other expressions must be enclosed in an aggregate function to guarantee the same.
-
Grouping NULLs
-
Aggregating NULLs
-
Challenge - Animal vaccinations report
-
Write a query to report the number of vaccinations each animal has received.
-
Include animals that were never adopted.
-
Exclude all rabbits.
-
Exclude all Rabies vaccinations.
-
Exclude all animals that were last vaccinated on or after October first, 2019.
-
The report should return the following attributes:
- Animals Name, Species, Primary Color, Breed,
- and the number of vaccinations this animal has received,
-
Guidelines
- Use the correct logical join types and force order if needed.
- Use the correct logical group by expressions.
-
- Application Tiers
- Data
- Business
- Presentation
-
Law and Order
-
NULL Order
-
PostgreSQL and Oracle
ORDER BY Value ASC NULLS LAST
-
-
Albert Einstein said,
- "In theory, theory and practice are the same. In practice, they are not."