## SQL Data Query Language (DQL) --- ## DQL Statement * SELECT --- ## Relational Algebra * Set operations (union, difference, Cartesian product) * Projection * Selection * Aggregation * Join * Rename --- ## Projection * presents a subset of the attributes of a relation * depending on the data, may produce duplicate rows * duplicates, can be eliminated using the `DISTINCT` keyword * `*` represents all the attributes --- ## Projection ```sql SELECT first_name, gender FROM employees; ``` --- ## Distinct Projection ```sql SELECT DISTINCT first_name, gender FROM employees; ``` --- ## Selection * presents a subset of tuples of a relation * uses `WHERE` clause to specify criteria * often used in conjunction with a projection operation --- ## Selection ```sql SELECT first_name, last_name FROM employees WHERE birth_date > "1960-01-01" ``` --- ## Aggregation * computes a function on a column * count, sum, average, minimum, maximum * can be calculated over entire table, or distinct groups of attributes * often used in conjuntion with selection to work on a subset of rows --- ## Aggregation ```sql SELECT MAX(salary) FROM salaries; ``` --- ## Aggregation ```sql SELECT MIN(salary) FROM salaries WHERE from_date LIKE "1986%"; ``` --- ## Aggregation with Grouping ```sql SELECT MAX(salary) FROM salaries GROUP BY YEAR(from_date); ``` --- ## Aggregation with Grouping ```sql SELECT YEAR(from_date), MAX(salary) FROM salaries GROUP BY YEAR(from_date); ``` --- ## Aggregation with Grouping ```sql SELECT YEAR(from_date), MAX(salary), COUNT(*) FROM salaries GROUP BY YEAR(from_date); ``` --- ## Joins * Inner * Left Outer * Right Outer