Section 7: Common Syntax#
Note
Key Notes:
SQL not case sensitive but generally use uppercase for keywords
Use ‘–’ to comment lines
Ctrl-Shift-Enter to run all
Terminate statement with semi-colon
Order: SELECT, FROM, WHERE, ORDER BY, LIMIT
Clause |
Description |
Syntax |
Example |
---|---|---|---|
USE |
Select database for query |
|
|
Select |
Returns the table with the columns specified |
|
|
AS |
Set a column name to something else. You can use arithmetic with columns (column_name + 10) |
|
|
WHERE |
Used to extract only the records that fulfill the specified condition |
|
Select all customers from Mexico: |
SELECT DISTINCT |
Select columns but with no duplicates |
|
|
Operators |
Operators used in WHERE clause: =, <=>, BETWEEN (between certain range), LIKE (search for a pattern), IN (specify multiple possible values for a column) |
|
|
AND/OR |
Allows for multiple conditions AND has higher precedence |
|
|
NOT |
Used to negate conditions |
|
|
IN |
The IN operator allows you to specify multiple values in a WHERE clause. |
|
|
BETWEEN |
The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates. |
|
|
LIKE |
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. Common pattern notations here |
|
|
REGEXP |
The REGEXP operator is used in a WHERE clause to search for values that match a regular expression pattern in a column. It allows more complex pattern matching than LIKE. Common pattern notations here |
|
|
IS NULL |
Checks for null values |
|
|
ORDER BY |
Used to sort the result-set in ascending or descending order. |
|
|
LIMIT |
The LIMIT clause is used to specify the number of records to return. You can use the offset value to skip that number of rows. |
|
|
‘INNER’ JOIN |
The INNER JOIN keyword selects records that have matching values in both tables. Prefix the table with the database name if it’s not in the current database |
|
|
Self Join |
A self join is a regular join, but the table is joined with itself. |
|
|
Joining Multiple Tables |
Running a join on multiple datasets |
|
|
Compound Join Conditions |
When you have multiple conditions to join 2 tables. |
|
|
Implicit Join |
A way to join tables by listing them in the FROM clause, separated by commas, and specifying the join condition in the WHERE clause |
|
|
LEFT JOIN |
Returns all records from the left table (table1), and the matching records (if any) from the right table (table2) |
|
|
RIGHT JOIN |
Returns all records from the right table (table2), and the matching records (if any) from the left table (table1). |
|
|
OUTER Joining Multiple Tables |
Running an outer join on multiple datasets |
|
|
USING |
Simplifies table join operations by letting you specify common columns between tables |
|
|
NATURAL JOIN |
Performs a join between two tables based on columns that have the same name and compatible data types. This join operation automatically determines the common columns between the tables and removes duplicates in the result set. |
|
|
CROSS JOIN |
Returns all records from both tables (table1 and table2) |
|
|
IMPLICIT CROSS JOIN |
A way to join tables by listing them in the FROM clause, separated by commas |
|
|
UNION |
Used to combine the result-set of two or more queries |
|
|
INSERT INTO |
Used to insert new records in a table. |
1. Specify both the column names and the values to be inserted: |
|
CREATE TABLE |
Used to create a new table in a database |
|
|
UPDATE / SET |
Used to modify the existing records in a table. |
|
|
DELETE FROM |
Used to delete existing records in a table. |
|
|
Like Operator Patterns#
LIKE Operator |
Description |
---|---|
WHERE CustomerName LIKE ‘a%’ |
Finds any values that start with “a” |
WHERE CustomerName LIKE ‘%a’ |
Finds any values that end with “a” |
WHERE CustomerName LIKE ‘%or%’ |
Finds any values that have “or” in any position |
WHERE CustomerName LIKE ‘_r%’ |
Finds any values that have “r” in the second position |
WHERE CustomerName LIKE ‘a_%’ |
Finds any values that start with “a” and are at least 2 characters in length |
WHERE CustomerName LIKE ‘a__%’ |
Finds any values that start with “a” and are at least 3 characters in length |
WHERE CustomerName LIKE ‘a%o’ |
Finds any values that start with “a” and ends with “o” |
Commonly Used Metacharacters in SQL REGEX#
Pattern |
What the Pattern matches |
---|---|
* |
Zero or more instances of string preceding it |
+ |
One or more instances of strings preceding it |
. |
Any single character |
? |
Match zero or one instances of the strings preceding it. |
^ |
caret(^) matches Beginning of string |
$ |
End of string |
[abc] |
Any character listed between the square brackets |
[^abc] |
Any character not listed between the square brackets |
[A-Z] |
match any upper case letter. |
[a-z] |
match any lower case letter |
[0-9] |
match any digit from 0 through to 9. |
[[:<:]] |
matches the beginning of words. |
[[:>:]] |
matches the end of words. |
[:class:] |
matches a character class i.e. [:alpha:] to match letters, [:space:] to match white space, [:punct:] is match punctuations and [:upper:] for upper class letters. |
p1|p2|p3 |
Alternation; matches any of the patterns p1, p2, or p3 |
{n} |
Exactly n instances of preceding element |
{m,n} |
between m and n instances of preceding element |
Commonly Used Data Types#
Data Type |
Description |
---|---|
CHAR(size) |
A FIXED length string (can contain letters, numbers, and special characters). The size parameter specifies the column length in characters - can be from 0 to 255. Default is 1. |
VARCHAR(size) |
A VARIABLE length string (can contain letters, numbers, and special characters). The size parameter specifies the maximum column length in characters - can be from 0 to 65535 |
INT(size) |
Signed range is from -2147483648 to 2147483647. Unsigned range is from 0 to 4294967295. The size parameter specifies the maximum display width |
DECIMAL(size, d) |
An exact fixed-point number. The total number of digits is specified in size. The number of digits after the decimal point is specified in the d parameter. The maximum number for size is 65. The maximum number for d is 30. The default value for size is 10. The default value for d is 0. |
DATE |
A date. Format: YYYY-MM-DD. |
TIME(fsp) |
A time. Format: hh:mm:ss. |
DATETIME(fsp) |
A date and time combination. Format: YYYY-MM-DD hh:mm:ss. |
YEAR |
A year in four-digit format. Values allowed in four-digit format: 1901 to 2155, and 0000. |