A process that matchs rows from the first table and the second table with have the same key to create a result row with combined columns from both tables.
eg.
1 2 3 4 5
SELECT title, rating FROM movies JOIN boxoffice ON movies.id = boxoffice.movie_id ORDERBY rating DESC;
5. OUTER JOINs
1 2 3 4 5 6 7 8
%Select query withLEFT/RIGHT/FULL JOINs on multiple tables SELECTcolumn, another_column, ... FROM mytable INNER/LEFT/RIGHT/FULLJOIN another_table ON mytable.id = another_table.matching_id WHEREcondition(s) ORDERBYcolumn, ... ASC/DESC LIMIT num_limit OFFSET num_offset;
When joining table A to table B, a LEFT JOIN simply includes rows from A regardless of whether a matching row is found in B. The RIGHT JOIN is the same, but reversed, keeping rows in B regardless of whether a match is found in A. Finally, a FULL JOIN simply means that rows from both tables are kept, regardless of whether a matching row exists in the other table.
eg. List all buildings and the distinct employee roles in each building (including empty buildings)
1 2 3
SELECTDISTINCT building_name, role FROM buildings LEFTJOIN employees ON buildings.building_name = employees.building
6. A short note on NULLs
Test a column for NULL values in a WHERE clause by using either IS NULL or IS NOT NULL constraint.
1 2 3 4 5
%Select query with constraints onNULLvalues SELECTcolumn, another_column, ... FROM mytable WHEREcolumnIS/ISNOTNULL AND/OR another_condition
eg.
Find the names of the buildings that hold no employees.
1 2 3 4 5
SELECTDISTINCT building_name FROM buildings LEFTJOIN employees ON building_name = building WHERE role ISNULL;
此题需要仔细思考一下
7. Queries with expressions
1 2 3
%Select query with expression aliases SELECT col_expression AS expr_description, ... FROMtable;
1 2 3
% Example query withbothcolumnandtable name aliases SELECTcolumnAS better_column_name, ... FROM complex_table_name AS easy_table_name;
8. Queries with aggregates(Pt.1)
1 2 3 4
%Select query with aggregate functionoverallrows SELECT AGG_FUNC(column_or_expression) AS aggregate_description, ... FROMtable WHERE constraint_expression;
Common aggregate functions
Function
COUNT(*)
MIN(column)
MAX(column)
AVG(column)
SUM(column)
Grouped aggregate functions
1 2 3 4 5
%Select query with aggregate functions overgroups SELECT AGG_FUNC(column_or_expression) AS aggregate_expression, ... FROMtable WHERE constraint_expression GROUPBYcolumn;
The GROUP BY clause works by grouping rows that have the same value in the column specified.
9. Queries with aggregates (Pt. 2)
1 2 3 4 5 6
%Select query withHAVINGconstraint SELECT group_by_column, AGG_FUNC(column_expression) AS aggregate_result_alias, ... FROMtable WHEREcondition GROUPBYcolumn HAVING group_condition;
The HAVING clause constraints are applied to the grouped rows.
The HAVING clause constraints are written the same way as the WHERE clause constraints.
eg. Find the total number of years employed by all Engineers
1 2 3 4
SELECT role, SUM(years_employed) FROM employees GROUPBY role HAVING role = "Engineer";
result
10. Order of execution of a Query
1 2 3 4 5 6 7 8 9 10
% Complete SELECT query SELECTDISTINCTcolumn, AGG_FUNC(column_or_expression), ... FROMtable JOIN another_table ON table.column = another_table.column WHERE constraint_expression GROUPBYcolumn HAVING constraint_expression ORDERBYcolumnASC/DESC LIMIT count OFFSET count;
% Altering tableto remove column(s) ALTERTABLE table_name DROP column_to_be_deleted;
Renaming the table
1 2 3
% Altering table name ALTERTABLE table_name RENAME TO new_table_name;
16. Dropping tables
Drop table statement
1
DROPTABLE IF EXISTS table_name;
If you have another table that is dependent on columns in table you are removing (for example, with a FOREIGN KEY dependency) then you will have to either update all dependent tables first to remove the dependent rows or to remove those tables entirely.