SQL Query Syntax
Basic Query
Any query need SELECT FROM WHERE
With Multiple relationsA $\times $ BTemporarily renaming
SELECT e.name, d.name
FROM Employee e, department d
WHERE d.name = 'marketing' AND e.name = 'Horton';
Self joins requires renaming
* in SELECT means all attributes of this relation
Rename attributes
Conditions allowed, note != is \<>
Put tuples in order, add this as the final clause, default in ascending order, add DESC to force descending
The attribute list can include expressions:Keywords and identifiers are not case-sensitive and white space are ignored
Expressions can be used in SELECT clauses,
operands: attributes, constants operators: arithmetic ops, string ops
Expressions that are a constant
SELECT, dept, cNum, 'satisfies' AS breadthRequirement
FROM Course
WHERE breadth;
Pattern operators
```sql
<<attribute>> LIKE <<pattern>>
<<attribute>> NOT LIKE <<pattern>>
%
any string _
any string char Aggregation
SUM, AVG, COUNT, MIN, MAX
can be applied to a column in a SELECT
COUNT(*)
counts the number of tuples
DISTINCT
inside the brackets can stop duplicates from contributing to the aggregation.
GROUP BY <attributes>
The tuples are grouped according to the values of those attributes and any aggregation give us a single value per group
If any aggregation is used, then each element of the SELECT list must be aggregated or an attribute on the GROUP BY list.
HAVING
decide which groups to keep.
Outside subqueries, HAVING may refer to attributes only if they are either aggregated or an attribute on the GROUP BY list.
Order of execution of a query
Writing query (execution order):
Set operations
A table can have duplicate tuples, unless it violate an integrity constraint.
Set operations with Bags
Using Multiset operations rather than set operations
To force the result of a query to be a set, use SELECT DISTINCT
To force the set operations to be a bag, use UNION/INTERSECT/EXCEPT ALL
Views
Use virtual views to temporarily refer to the result of a query
Break down a large query
Joins
Joins allowed in FROM
Inner join is not the best practice since a working query can be broken by adding a column to a schema.
Outer Join
preserves dangling tuples by padding them with NULL
in the other relation
When use LEFT, RIGHT, FULL
it will be outer join, if not added, then inner
NULL
Used for indicating missing value or inapplicable attribute
Cant be checked by IS NULL
and IS NOT NULL
, e.x.
Also, if one or both operands to a comparison is NULL
, the truth value will always be UNKNOWN
NULL can be imagined to have value 0.5, compared to TRUE = 1, FALSE = 0 when consider logical operations
Notice that WHERE
will only pick rows with TRUE
Subqueries
FROM
clause, but you must name the result and parenthesized. Hence can refer to it in the outer query
WHERE
clause if the subquery guaranteed to produce exactly one tuple.
However, we can also use keywords ANY/SOME
for existence, ALL
for for all
IN, NOT IN, EXISTS
is some syntax sugar