Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Select Statement -
- - Whitespace DOESN'T matter
- - '*' selects ALL columns
- - 'TOP N' will return the top 'N' rows from table
- Example:
- SELECT
- TOP N column_name(s)
- FROM
- table_name
- - 'DISTINCT' will return only unique values, not counting duplicates
- + if done on multiple columns, it will return all unique combinations of these columns
- Example:
- SELECT
- column_name(s)
- FROM
- table_name
- ---------------------------------------------------------------------------
- Filtering data -
- - Primarily done using the 'WHERE' clause
- + statements that evaluate to 'true' or 'false' for each row
- Example:
- SELECT
- column_name(s)
- FROM
- table_name
- WHERE
- condition(s)
- - Comparing strings with wildcards, use the 'LIKE' keyword
- + The '%' character represent 0, 1, or more of any character
- - WHERE column1 LIKE 'Foo%' ; will match anything that starts with 'Foo'
- - WHERE column1 LIKE '%Bar' ; will match anything that ends with 'Bar'
- + The '_' character represents a single instance of any character
- - WHERE column1 LIKE '_bc' ; will match anything that starts with a single character, followed by 'bc'
- Example:
- SELECT
- column_name(s)
- FROM
- table_name
- WHERE
- column LIKE 'value%'
- OR
- column LIKE '_value'
- - Checking Null values
- + Normal operators DO NOT WORK on null values
- + To check for null: WHERE column1 IS NULL
- + To check for NOT null: WHERE column1 IS NOT NULL
- Example:
- SELECT
- column_name(s)
- FROM
- table_name
- WHERE
- column value IS NULL
- OR
- column value IS NOT NULL
- - Comparing to a collection
- + Use the 'IN' keyword, followed by the collection
- Example:
- SELECT
- column_name(s)
- FROM
- table_name
- WHERE
- column IN (value1, value2, ...)
- - Comparing to a range
- + You can compare against a range of values
- + To do this, use 'BETWEEN' keyword, followed by the min and max values
- Example:
- SELECT
- column_name(s)
- FROM
- table_name
- WHERE
- column BETWEEN value1 AND value2
- - Combining where conditions
- + You can combine more than 1 codition in a single where clause
- + Use any combination ofthe 'AND' and 'OR' keywords
- + Use parenthese () to group conditions together and control logic
- Example:
- SELECT
- column_name(s)
- FROM
- table_name
- WHERE
- (column1 = value1 AND column2 = value2) OR column3 = value3
- ---------------------------------------------------------------------------
- Sorting data -
- - Assume all result sets are unsorted
- - Use the 'ORDER BY' keyword to sort the data
- - Use 'ASC' or 'DESC' to control the sort direction
- Example:
- SELECT
- column_name(s)
- FROM
- table_name
- ORDER BY
- column ASC
- OR
- column DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement