Category Archives: SQL

Comparison with Subquery Result

¨  Can use scalar subqueries in WHERE clause comparisons Example: 🞑 Want to find the name of the branch with the smallest number…
Continue reading

Subqueries in WHERE Clause

Widely used: 🞑 Direct comparison with scalar-subquery results 🞑 Set-membership tests: IN, NOT IN 🞑 Empty-set tests: EXISTS, NOT EXISTS ¨  Less frequently…
Continue reading

Kinds of Subqueries

¨  Some subqueries produce only a single result SELECT MAX(assets) FROM branch; 🞑 Called a scalar subquery 🞑 Still a relation, just with…
Continue reading

Nested Subqueries

¨  SQL provides broad support for nested subqueries 🞑 A SQL query is a “select-from-where” expression 🞑 Nested subqueries are “select-from-where” expressions embedded…
Continue reading

The HAVING Clause

The HAVING clause can use aggregate functions in its predicate 🞑 It’s applied after grouping/aggregation, so those values are available 🞑 The WHERE…
Continue reading

Filtering Results

¨  To apply filtering to the results of grouping and aggregation, use a HAVING clause 🞑 Exactly like WHERE clause, except applied after…
Continue reading

Filtering Tuples

The WHERE clause is applied before any grouping occurs SELECT G1,G2,..., F1(A1),F2(A2),... FROM r1,r2,... WHERE P GROUP BY G1,G2,... 🞑 Translates into relational…
Continue reading

Grouping and Aggregates (4)

¨  SQL doesn’t require that you specify the grouping attributes in the SELECT clause 🞑 Only requirement is that the grouping attributes are…
Continue reading

Grouping and Aggregates (3)

¨  Note the difference between relational algebra notation and SQL syntax Relational algebra syntax: G1,G2,…,GnGF1(A1),F2(A2),…,Fm(Am)(E) 🞑 Grouping attributes only appear on left of…
Continue reading

Grouping and Aggregates (2)

¨  Can group on multiple attributes 🞑 Each group has unique values for the entire set of grouping attributes ¨  Example: “How many…
Continue reading