- IDE
- Azure Data Studio
- Supports both PostgreSQL and SQL Server
- Azure Data Studio
-
SubqueryProcessing
- Table expression
- Scalar expression
- Row expression
-
A scalar is an element of a field which is used to define a vector space. A quantity described by multiple scalars, such as having both direction and magnitude, is called a vector.
-
Non-Correlated Subquery
SELECT Foo, (SELECT AVG(Foot) FROM Bart ) AS Bar FROM FooBar;
-
Correlated Subquery
SELECT Foo, ( SELECT AVG(Foot) FROM Bart WHERE Bart.Foot = Foobar.Foo ) AS Bar FROM FooBar;
-
EXISTS
- Return result only if the subquery returns at least one row
SELECT * FROM Foo WHERE EXISTS ( SELECT * FROM Bart WHERE Bart.Foot = Foo.Bar );
-
There are 3 types of set operators
-
UNION
- ALL | DISTINCT
-
EXCEPT
- ALL | DISTINCT
-
INTERSECT
- ALL | DISTINCT
-
Note: Default:
DISTINCT
-
-
Side note:
SELECT ALL * FROM Foo;/*SELECT ALL is default*/
MULTISET
for every set operators- Only supported by Oracle
-
Set Operators vs. Joins
-
UNION ALL
-
UNION DISTINCT
-
INTERSECT ALL
-
INTERSECT DISTINCT
-
EXCEPT ALL
-
EXCEPT DISTINCT
-
EXCEPT Direction
-
Set Operators and NULL
-
- Anti Join
- Semi Join
- Anti Semi Join
- Hybrid of Anti and Semi joins
- Show which breeds were never adopted.
- NULL breeds need to be considered carefully.
- The answer is that only Turkish Angora Cats were never adopted.
- Try to solve using OUTER JOIN, NOT EXISTS, and NOT IN first.
-
Self Joins
-
A use case:
- In graph theory and computer science, an adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a vertex in the graph
-
Adjacency Lists
-
-
Non-Equality Joins
-
'Stand Alone' Data Sources
SELECT * FROM Foo AS F1 CROSS JOIN ( SELECT Foot, COUNT(*) AS Count FROM FootBart AS F2 WHERE F2.Bart = 5 GROUP BY Foot ) AS X;
-
F1 and F2 are independent
-
If we change the code like this:
SELECT * FROM Foo AS F1 CROSS JOIN ( SELECT Foot, COUNT(*) AS Count FROM FootBart AS F2 WHERE F2.Bart = F1.Bar GROUP BY Foot ) AS X;
- We get an error, saying
The multi-part identifier "F1.Bar" could not be bound.
becauseF1.Bar
doesn't exist in the context of the subquery.
- We get an error, saying
-
-
Support for LATERAL JOIN
- Oracle, PostgreSQL, MySQL
-
SQL Server can emulate the
LATERAL JOIN
usingCROSS APPLY
andOUTER APPLY
-
Our shelter has been experiencing financial difficulties.
- !!! PLEASE consider donating to your local animal shelter !!!
- The board of directors decided to explore additional revenue sources and came up with an idea.
- Instead of spaying and neutering all animals, the shelter should consider responsible breeding of purebred animals.
- !!! This is a hypothetical question – ALWAYS spay and neuter your pets !!!
-
Your challenge is to figure out which animals are breeding candidates.
-
Expected result:
Species Breed Male Female Cat Sphynx Salem Nova Cat Turkish Angora Tigger Ivy Dog Bullmastiff Toby Penelope Dog Bullmastiff Toby Skye Dog Bullmastiff Jake Penelope Dog Bullmastiff Jake Skye Dog English setter Frankie Callie Dog English setter Frankie Nala Dog English setter Gus Callie Dog English setter Gus Nala Dog English setter Benji Callie Dog English setter Benji Nala Dog English setter Mac Callie Dog English setter Mac Nala Dog Schnauzer Boomer Emma Dog Schnauzer Boomer Lily Dog Schnauzer Brody Emma Dog Schnauzer Brody Lily Dog Weimaraner Brutus Lucy Dog Weimaraner Brutus Poppy Dog Weimaraner Brutus Roxy Dog Weimaraner Jax Lucy Dog Weimaraner Jax Poppy Dog Weimaraner Jax Roxy -
Guidelines:
- Candidates should be male and female of the same species and breed.
- You may use any database you wish.
- Results are ordered by species and breed
-
Ordered Set Functions
-
Ordered set functions
ARE
aggregate functions!, but which is affected by their orders -
Syntax:
Function (Expression) WITHIN GROUP (ORDER BY Expressions)
-
Hypothetical Set
RANK
DENSE_RANK
PERCENT_RANK
CUME_DIST
-
Inverse Distribution
-
PERCENTILE_CONT
-
PERCENTILE_DISC
-
Differences:
PERCENTILE_CONT
is that it interpolates a value.PERCENTILE_DISC
picks an existing value.
-
-
-
Multi-Level Grouping
-
Grouping Sets
SELECT YEAR(Adoption_Date) AS Year, MONTH(Adoption_Date) AS Month, COUNT(*) AS Monthly_Adoptions FROM Adoptions GROUP BY GROUPING SETS ( ( YEAR(Adoption_Date), MONTH(Adoption_Date) ) );
- Your last challenge is to write a query that returns a statistical report of vaccinations.
- The report should include the total number of vaccinations for several dimensions:
- Annual
- Per species
- For each species per year
- By each staff member
- By each staff member per species
- And to make it interesting, let's throw the latest vaccination year for each of these groups.
- Guidelines:
- ORDER BY Year, Species, First_Name, Last_Name and be careful with the order by aliases...
- The report should include the total number of vaccinations for several dimensions:
-
Recursive WITH Clause
WITH RECURSIVE REX AS ( SELECT Foo, Bar FROM FooBar /*Anchor query*/ UNION [ALL|DISTINCT] SELECT Foot, Bart FROM REX WEHRE <Stop Condition> /*Recursive query*/ )
- Note: Except for SQL Server...
- code
- generate_series
- A set function
- generate_series
-
Cursors
-
Sequential access means that a group of elements, such as data in a memory array or a disk file or on magnetic tape data storage, is accessed in a predetermined, ordered sequence.
-
SQL is declarative, cursors are not...
DECLARE McCursy CURSOR FOR SELECT Foo, Bar FROM FooBar ORDER BY Foo ASC; OPEN McCursy; FETCH FIRST FROM McCursy INTO Var1, Var2;
-
-
What if a new row is added into the result set for the existing cursor
-
ISO/IEC CD 9075-2
- 4.38 Cursors
- If a holdable cursor is open during an SQL-transaction T and it is held open for a subsequent SQL-transaction, then whether any significant changes made to SQL-data (by T or any subsequent SQL-transaction in which the cursor is held open) are visible through that cursor in the subsequent SQL-transaction before that cursor is closed is determined as follows:
- If the cursor is insensitive, then significant changes are not visible.
- If the cursor is sensitive, then the visibility of significant changes is implementation-defined.
- If the cursor is asensitive, then the visibility of significant changes is implementation-dependent.
- If a holdable cursor is open during an SQL-transaction T and it is held open for a subsequent SQL-transaction, then whether any significant changes made to SQL-data (by T or any subsequent SQL-transaction in which the cursor is held open) are visible through that cursor in the subsequent SQL-transaction before that cursor is closed is determined as follows:
- 4.38 Cursors