JRN 407 Computer Assisted Reporting

Total Page:16

File Type:pdf, Size:1020Kb

JRN 407 Computer Assisted Reporting

JRN 407 Computer Assisted Reporting Jane Briggs-Bunting Michigan State University

SQL Cheat Sheet There are six basic commands in SQL, and though you can eliminate some when asking your questions, you must always keep them in this order: SELECT choosing columns or fields of information you want to compare. So, after the query is run, these columns with their data will appear on the screen. (You can also use Select to calculate or to create a new column—more on that much later!) (Required) FROM the table or tables (database) of information you want to use. (Required) WHERE the rows meet this condition or these conditions. In other words, filtering the records you want to see. (Optional) GROUP BY summarizing information whenever you do a function. (Gathers rows with identical values in a the chosen column or field, or displays a single row for each unique combination of those identical values. (Optional) HAVING allows you to limit records retrieved. It is always used in conjunction with GROUP BY, e.g. show a GROUP BY only if the Group meets this condition. HAVING does for GROUP BY what WHERE does for rows or records. (Optional) ORDER BY sorts the answer from highest to lowest or the reverse by a particular column. (Optional)  SELECT and FROM must always be included in every query. The remaining four can be added as needed.  SELECT, FROM and WHERE search the data  GROUP BY summarizes the data (every field in GROUP BY must be in SELECT, too.)  HAVING limits results of the data searched  ORDER BY organizes how results are presented.

Back to the top More SQL Rules:  Text is always "between quotes". Some numbers you may want to treat as text, zip codes, for example, so they would be treated like "48309".  Numbers are just 12345  Dates are always between #02,013,99#  * (asterisks)are wild cards and can be used to guess at information or help in the sorting of dirty data. You can use * at the beginning, middle, and/or end of a word.  ; (semicolon)—signals the computer to run the query. However, in Access, you click on the Toolbar key with the ! on it to run the query.  < means less than (note how the arrow points left for Less than!)  > means greater than (ditto on how the arrow points right for Greater than)  <= means less than or equal to  >= means greater than or equal to  = means an exact match (can not be used with *)

1  like means similar (can be used with *)  and is inclusive and will return fewer results  or is exclusive and will return more results  not will exclude everything but certain information.  Use sets of ( parenthesis) to set off groups of ands and ors  =null or is null to search for missing values. SQL Aggregate Functions:  Used in a SELECT statement to compute a summary value for a column or field. (Can also be used in a HAVING statement Can not be used in WHERE statement)  Applies only to those rows retrieved by the SELECT/WHERE statements:  If query result not restricted by WHERE or summarized in GROUP BY, the aggregate will calculate the summary value for all rows in the column or field.  If the query result is restricted by WHERE statement, the aggregate will calculate summary value for only the retrieved row(s).  If query result summarized in GROUP BY statement, the aggregate will calculate the summary values for each GROUP BY grouping. Can be used with text or numeric values  COUNT(*) count number of rows in the answer table; includes nulls.  COUNT(Column Name) counts number of Non-Null values in column  COUNT(DISTINCT Column Name) counts number of Unique values in column (ignores duplicates, treats all Null values as equal)  MAX(Column Name) returns highest value in column (ignores Null values)  MIN(Column Name) returns lowest value in column (ignores Null values) Can be used only with numeric values  SUM(Column Name) calculates Total of all non-Null values in column  AVG(Column Name) calculates Mean/Average of all non-Null values in column  XSTDDEV(Column Name) calculates standard deviation of column  XVAR(Column Name) calculates Variance of column Where do you insert it? Aggregate function commands go in the SELECT line of your query. {e.g. SUM(GiftValue) or SUM(GiftValue), COUNT(*), … }

2

Recommended publications