Division of Mathematics and Computer Science Maryville College February 16, 2016
Outline 1 2 3
Syntax [ALL DISTINCT DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [, select_expr...] [FROM table_references [ where_condition] [GROUP BY {col_name expr position} [ASC DESC],... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name expr position} [ASC DESC],...] [LIMIT {[offset,] row_count row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE file_name [CHARACTER SET charset_name] export_options INTO DUMPFILE file_name INTO var_name [, var_name]] [FOR UPDATE LOCK IN SHARE MODE]]
Syntax (The goodparts version) [ALL DISTINCT DISTINCTROW ] select_expr [, select_expr...] [FROM table_references [ where_condition] [GROUP BY {col_name expr position} [HAVING where_condition]] [ORDER BY {col_name expr position} [ASC DESC],...] [LIMIT {[offset,] row_count row_count OFFSET offset}]]
select expr A select expression can take several forms: Most commonly, it is the name of a column (sometimes with an alias) column_name [AS alias ] You can select all columns (which is ill-advised in program code): * You can also operate on columns A + 5 A + B SIN( A ) Or you could have just a raw computation. 2+2 SIN(3.14159)
Functions and Operators Normal set of math operators: +,,, /, % Comparison Operators: =, <, >, <=, >=, <> Functions (There are many) Some common ones are: COUNT(*) - Counts the number of rows NOW() - Returns the current DATETIME Trig Functions (SIN, COS, TAN, etc.) SQRT(x) - Returns the Square Root CONCAT(...) - Concatenates String Arguments Type Specific functions. See the manual!
table reference (goodparts version) table_reference: table_name [[AS] alias ] join_table Ok, so not much to it really, except for the business. We ll explore that later.
The Clause The clause imposes a condition on applies the select expressions to the rows which match the clause. A where expression is a comparison statement which consists of the following: Column Names Literal Expressions Math Operators and Functions Conditional Operators Logical Statements: AND, OR, NOT, IS, IS NOT, IN(...), LIKE
IS or IS NOT, that is the question NULL really hurts where expressions. You cannot compare against NULL with conventional operators! We can, however, do this: column_name IS NULL column_name IS NOT NULL So IS and IS NOT are used when conditioning on NULL
Some Sample Queries COUNT(*) FROM City ; * FROM City Population >= 1000000; Name FROM Country Name LIKE V% ; * FROM City Population >= 2000000 AND CountryCode IN( USA, RUS );
Joining Tables Joins are used to select data from related tables. Columns from all joined tables can be used select expressions and where clauses. There are three main types of joins: INNER LEFT OUTER RIGHT OUTER
Syntax join_table: table_reference [INNER] table_list [join_co table_reference {LEFT RIGHT} [OUTER] table_ join_condition You resolve ambiguity in columns by specifying them like this: tablename. columnname INNER is the most common for querying across relationships which are 1-many where many means 1 or more. LEFT OUTER is used for querying across relationships which are 1-many where many means 0 or more.
Example Query Country. Name AS Country, City. Name AS City, City. Population AS Population FROM Country INNER City ON City. CountryCode = Country. Code Country. Population >= 20000 AND Country. Name LIKE Z% ;