Dr. Andrew Besmer
NULL
NULL is considered to be different from every other NULL 1SELECT * FROM Product WHERE Category = NULL;
NULLs have three valued logic
| TRUE | FALSE | NULL | |
|---|---|---|---|
| TRUE | TRUE | FALSE | NULL |
| FALSE | FALSE | FALSE | FALSE |
| NULL | NULL | FALSE | NULL |
| TRUE | FALSE | NULL | |
|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE |
| FALSE | TRUE | FALSE | NULL |
| NULL | TRUE | NULL | NULL |
| In | Out |
|---|---|
| TRUE | FALSE |
| FALSE | TRUE |
| NULL | NULL |
IS NULL or IS NOT NULL when looking to compare in WHERE clause
TRUE or FALSE instead of NULLSELECT * FROM Product WHERE Category IS NULL;
COUNT() - Count items in a groupSUM() - Sum items in a groupAVG() - Average items in a groupSTD() - Std Dev of items in a groupMIN() - Min of items in a groupMAX() - Max of items in a groupNULLs are simply discardedSELECT COUNT(*) FROM Product;
vs
SELECT COUNT(Company) FROM Product;
IN
v with a set (or multiset) of values VTRUE if v in VSELECT * FROM Product WHERE Company IN (1,2);
SELECT * FROM Product WHERE (Company,Category) IN ((1,1),(1,2));
SELECT Company.Name FROM Company
WHERE Company.id IN (Select Product.Company
FROM Product WHERE Product.Price > 15.00);
same as
SELECT Company.Name FROM Company JOIN Product ON
Company.id = Product.Company WHERE
Product.Price > 15.00;
FROM clauseSELECT * FROM (Select * FROM Product) AS P;
SELECT * FROM Product AS P WHERE P.Price > (Select AVG(Product.Price)
FROM Product WHERE Product.Category = P.Category);
EXISTS Checks for the existence of any results in the nested query
SELECT * FROM Company WHERE EXISTS (Select * FROM Product
WHERE Product.Company = Company.id);
GROUP BY Partitions results into subsets of tuples based on the grouping attributes
NULLs get their own group
SELECT Company, AVG(Price) FROM Product GROUP BY Company;
GROUP BY or that are not the result of the aggregation functionsSELECT name, AVG(Price) FROM Product GROUP BY Company;
OUTERHAVING Provides a condition on the summary information, applied after grouping
HAVING but not WHERESELECT Company, AVG(Price) FROM Product
GROUP BY Company HAVING AVG(Price) < 15.00;
SELECT Company, AVG(Price) FROM Product
GROUP BY Company HAVING AVG(Price) < 15.00;
SELECT Category, Company, AVG(Price) FROM Product
GROUP BY Category, Company;
FROM & JOINWHERE & ONGROUP BYHAVINGORDER BYSELECTSELECT <projection attribute(s)>
FROM <table(s)>
[WHERE <condition(s)>]
[GROUP BY <attribute(s)>]
[HAVING <group condition>]
[ORDER BY <attribute list>];
Except when aggregating.↩