Category Archives: SQL

Grouping and Aggregates

¨  Can also perform grouping on a relation before computing aggregates 🞑 Specify a GROUP BY A1,A2,... clause at end of query ¨ …
Continue reading

Computing Counts

Can count individual attribute values COUNT(branch_name) COUNT(DISTINCT branch_name) ¨  Can also count the total number of tuples COUNT(*) 🞑 If used with grouping,…
Continue reading

Eliminating Duplicates

¨  Sometimes need to eliminate duplicates in SQL queries 🞑 Can use DISTINCT keyword to eliminate duplicates ¨  Example: “Find the number of…
Continue reading

Aggregate Examples (2)

¨  This query produces an error: SELECT branch_name, MAX(amount) AS max_amt FROM loan; ¨  Aggregate functions compute a single value from a multiset…
Continue reading

Aggregate Examples

🞑 Find average balance of accounts at Perryridge branch SELECT AVG(balance) FROM account WHERE branch_name = 'Perryridge'; ¨  Find maximum amount of any…
Continue reading

Aggregate Functions in SQL

¨  SQL provides grouping and aggregate operations, just like relational algebra Aggregate functions: SUM     sums the values in the collection AVG     computes average…
Continue reading

Ordered Results (3)

Say ASC or DESC after attribute name to specify order 🞑 ASC is redundant, but can improve readability in some cases Can list…
Continue reading

Ordered Results (2)

¨  Find bank accounts with a balance under $700: SELECT account_number, balance FROM account WHERE balance < 700; ¨  Order results in increasing…
Continue reading

Ordered Results

¨  SQL query results can be ordered by particular attributes Two main categories of query results: 🞑 “Not ordered by anything” Tuples can…
Continue reading

SQL QUERIES

¨  SQL queries use the SELECT statement General form is: SELECT A1, A2, ... FROM r1, r2, ... WHERE P; 🞑 ri are…
Continue reading