diff options
| author | mo khan <mo@mokhan.ca> | 2017-01-26 20:43:52 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2017-01-26 20:43:52 -0700 |
| commit | c39bc89a8b65ce06e5408e96dffa688bfd99742b (patch) | |
| tree | a9e118e7c997d76ecdbe77012500e0b3a3190272 /README.md | |
| parent | 9fa05b16245358581ed85ac1c308c6ce2cee6e0a (diff) | |
update README.
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 86 |
1 files changed, 86 insertions, 0 deletions
@@ -1,6 +1,7 @@ # Schema | BUSINESSES | | COMPUTERS | | EVENTS | | +| ------------------------ | ------------ | ----------- | ------- | ----------- | ------------ | | id | int | id | int | id | int | | name | varchar(255) | active | tinyint | computer_id | int | | business_relationship_id | int | business_id | int | occurred_at | datetime | @@ -9,6 +10,91 @@ # Lessons +Links: +* https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ + +`WHERE` is applied before `GROUP BY`, `HAVING` is applied after and can filter on aggregates. + + +## Inner join + +Produces a set of records that match in both Table A and Table B. + +```mysql +SELECT * FROM table_a +INNER JOIN table_b +ON table_a.name = table_b.name; +``` + +```text + A | | B + =========== +( | X | ) + =========== +``` + + +## Full Outer Join + +Produces the set of results from both table A and table B, with matching records from both sides where available. +If there is no match the missing side will contain null. + +```mysql +SELECT * FROM table_a +FULL OUTER JOIN table_b +ON table_a.name = table_b.name; +``` + +```text + A | | B + =========== +( X | X | X ) + =========== +``` + +## LEFT OUTER JOIN + +Produces a complete set of records from table A, with the matching records (where available) in table B. +If there is no match, the right side will contain a null. + + +To produce a set of results found in table_a but not table_b. + +```mysql +SELECT * +FROM table_a +LEFT OUTER JOIN table_b +ON table_a.name table_b.name +where table_b.id IS NULL; +``` + +```text + A | | B + =========== +( X | | ) + =========== +``` + +To produce a set of results unique to table_a and table_b. + +```mysql +SELECT * +FROM table_a +FULL OUTER JOIN table_b +ON table_a.name = table_b.name +WHERE table_a.id IS NULL +OR table_b.id IS NULL; +``` + +```text + A | | B + =========== +( X | | X ) + =========== + ``` + +# Questions + 1. Which businesses have more than N computers? 2. Which computer had the most events in a single day? 3. What are the top 10 most active computers (day, week, month)? .i.e emits the most events. |
