quartz/content/notes/19-advanced-sql-2.md
Jet Hughes 0324a7cb71 update
2022-06-08 14:12:07 +12:00

4.5 KiB

title aliases tags sr-due sr-interval sr-ease
19-advanced-sql-2
info201
lecture
2022-06-30 27 250

CASE

case example

basically a switch statement

  • derived valye where calcyulation isnt simple
  • standardising values e.g., booleans: t/f, y/n, 0/1
  • flipping between row and col orientation (privoting)
    • all students vs all on one students papers

Set operators

relations are sets of tuples ⇒ we can use set operators

combine rows of two table vertically

source table must be compativble

  • same set of columns
  • same data types
  • = same heading

get new table with same headings as inputs

identical rows deleted

set example 1

union: all rows

  • union
  • union result

intersect: rows that appear in both

  • intersect
  • intersect results

difference (except, minus): rows in top that arent in bottom

  • diff
  • diff results

Aggregation and grouping

recall

group by

  • groups rows that have equal values across all the columns in <column-list>
  • always used with aggregate function(s) in select clause
  • one row in the result for each differect combined value of the grouped columns

group example

  • all non computed columns in select clause must normally appear in group y, and vice versa

restricting by groups (having)

  • similiar to where, restricts output of group by
  • cant include aggregate functions (where can't);

Analytic functions (FYI)

  • enchancement of aggregation
  • aggregate without reshaping the ouput
  • many more functions avaiable
  • sliding windows supported
  • dont use when simple group by is sufficient

Select

slide

  • select
  • from
  • where
  • group by
  • having
  • order by

order of evaluation

  • from
  • where
  • group by
  • having
  • select
  • order by

Joins

recall recall join syntax

inner join : matching rows only

outer join: may include non-matching rows from the left table, right table, or both tables

also, cross, and semi join

Cross join product

cross join cross join result

every possible combination of rows from two tables

uses:

  • cards
    • suits table
    • ranks table
    • deck = suits cross join ranks
  • timetable
    • days table
    • timeslots table
    • timetable = days cross join timeslots

Semi join

output only trows from one table that match rows from the other

semijoin semijoin results

inverse is antijoin: ouput only rows from one table that dont match rows from the other

Subqueries

a select expression embedded inside another

  • inselect clause to compute value using data from other tables

  • in from: inline view

  • in where clause with in, all, some , exists

  • can refer to data from "outer" expression (correlated subquery)

  • tricky to write, so be careful. maybe stepwise

  • can rewrite joins as subqueries, but not vice versa

prefer joins to subqueries

how to develop

  • identify components opf multi part query
  • implement and test the components as separeate select statements
  • combine into one query, nesting one within the other.

subquery part 1 subquery part 2

Inline views

a named subqueriy embedded in the from clause is effectively a temporary view

visible within thescope of current select expression only

inline view

saving sub queries (WITH)

  • sometimes need to re-use same or very similar subquery severl times in the same query
  • with saves named subqueries for later re-use (in the same query)
  • visible iwthin scope of entire select statement

with

Scope in select

  • row variable only exist inside the select expression that defines them
  • a select expression can only directly refer to items declared in:
    • its own select and from clause
    • select and from clauses of any elclosing expressions
    • any preceedin with expression
  • particularly important for correlated subqueries