w2d3 Flashcards
What do queries do
get output in the way you need it
subqueries
return a table (special ones can return a single constant)
can act as tables in a from
can be combined into a single table
can be used with special modifiers
ARE MOST USEFUL WHEN DEALING WITH A SINGLE TABLE
In clause
used instead of where when you have a relation
because subqueries return tables as a default and you can not use an equality between an attribute and a relation
ANY/SOME
- works with subqueries that give a single column
WHERE xxx condition ANY(Is true if xxx meets the condition for one or more results of a query)
All
like ANY/some but true if xxx meets the condition for all results of the subquery
EXISTS/NOT EXISTS
true if sub query produces one or more rows/records
ALIAS/PREFIXES
Each table can be given an alias in the FROM clause
FROM TableA aliasA, TableB aliasB
- aliases typically one or two characters
- alias is used to prefix attributes from a particular table in cases where the same attribute name is used in both tables
- where attribute names are unique they do not need prefixes
Simple Join
attributes from both tables
common attributes prefixed
Foreign keys always….
point at a unique set of attributes, usually a primary key
What can sort a join
any of the attributes defined in the select clause
Inner join
contains records in result only where corresponding records were found in both tables
Full outer join
Contains records in result where any records were found in either input table, nulls placed in missing attributes
Left Outer Join
contains records in results where records were in the first input table
- place nulls in result attributes that are missing because of a missing record in the second input table
By making all joins as multi-table queries
ensures you make your intentions explicit
joining tables
requires both tables have the same structure
- same # of columns
- corresponding columns are of the same type and length