SELECT column, another_column, …
FROM mytable
WHERE condition
AND/OR another_condition
AND/OR …;
Operator | Condition | SQL Example |
, ! , < <=, >, >= |
Standard numerical operators | column-name != 4 |
BETWEEN … AND … | Number is within range of two values (inclusive) | column-name BETWEEN 1.5 AND 10.5 |
NOT BETWEEN … AND … | Number is not within range of two values (inclusive) | column-name NOT BETWEEN 1 AND 10 |
IN (…) | Number exists in a list | column-name IN (2, 4, 6) |
NOT IN (…) | Number does not exist in a list | column-name NOT IN (1, 3, 5) |
Operator | Condition | Example |
= | Case sensitive exact string comparison (notice the single equals) | column-name = “abc” |
!= or <> | Case sensitive exact string inequality comparison | column-name != “abcd” |
LIKE | Case insensitive exact string comparison | column-name LIKE “ABC” |
NOT LIKE | Case insensitive exact string inequality comparison | column-name NOT LIKE “ABCD” |
% | Used anywhere in a string to match a sequence of zero or more characters | column-name LIKE “%AT%” |
(matches “AT”, “ATTIC”, “CAT” or even “BATS”) | ||
- | Used anywhere in a string to match a single character (only with LIKE or NOT LIKE) | column-name LIKE “AN_” (matches ’AND’, but no ’AN’) |
IN (…) | String exists in a list | column-name IN (“A”, “B”, “C”) |
NOT IN (…) | String does not exist in a list | column-name NOT IN (“D”, “E”, “F”) |
SELECT DISTINCT column, another_column, …
FROM mytable
WHERE condition(s);
SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC;
SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC
LIMIT num_limit OFFSET num_offset;
LIMIT returns up to numlimit rows and OFFSET specifies from where to start counting rows.
Using the JOIN clause in a query, we can combine row data across two separate tables using this unique key. The first of the joins that we will introduce is the INNER JOIN.
SELECT column, another_table_column, …
FROM mytable
INNER JOIN another_table
ON mytable.id = another_table.id
WHERE condition(s)
ORDER BY column, … ASC/DESC
LIMIT num_limit OFFSET num_offset;
The INNER JOIN is a process that matches rows from the first table and the second table which have the same key (as defined by the ON constraint) to create a result row with the combined columns from both tables. After the tables are joined, the other clauses we learned previously are then applied.
SELECT column, another_column, …
FROM mytable
INNER/LEFT/RIGHT/FULL JOIN another_table
ON mytable.id = another_table.matching_id
WHERE condition(s)
ORDER BY column, … 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.
SELECT particle_speed / 2.0 AS half_particle_speed
FROM physics_data
WHERE ABS(particle_position) * 10.0 > 500;
SELECT column AS better_column_name, …
FROM a_long_widgets_table_name AS mywidgets
INNER JOIN widget_sales
ON mywidgets.id = widget_sales.widget_id;
SELECT group_by_column, AGG_FUNC(column_expression) AS aggregate_result_alias, …
FROM mytable
WHERE condition
GROUP BY column
SELECT group_by_column, AGG_FUNC(column_expression) AS aggregate_result_alias, …
FROM mytable
WHERE condition
GROUP BY column
HAVING group_condition;
GROUP BY divides the query result into groups of rows.
HAVING filters rows resulting from a GROUP.
SQL also supports the use of aggregate expressions (or functions) that allow you to summarize information about a group of rows of data.
SELECT AGG_FUNC(column_or_expression) AS aggregate_description, …
FROM mytable
WHERE constraint_expression;
Function | Description |
MIN(column) | Finds the smallest numerical value in the specified column for all rows in the group. |
MAX(column) | Finds the largest numerical value in the specified column for all rows in the group. |
AVG(column) | Finds the average numerical value in the specified column for all rows in the group. |
SUM(column) | Finds the sum of all numerical values in the specified column for the rows in the group. |
Count(*) | A common function used to counts the number of rows in the group if no column name is specified. |
Count(Column) | Count the number of rows in the group with non-NULL values in the specified column. |
SELECT DISTINCT column, AGG_FUNC(column_or_expression), …
FROM mytable
JOIN another_table
ON mytable.column = another_table.column
WHERE constraint_expression
GROUP BY column
HAVING constraint_expression
ORDER BY column ASC/DESC
LIMIT count OFFSET COUNT;
INSERT INTO mytable
VALUES (value_or_expr, another_value_or_expr, …),
(value_or_expr_2, another_value_or_expr_2, …),
…;
INSERT INTO mytable
(column, another_column, …)
VALUES (value_or_expr, another_value_or_expr, …),
(value_or_expr_2, another_value_or_expr_2, …),
…;
INSERT INTO boxoffice
(movie_id, rating, sales_in_millions)
VALUES (1, 9.9, 283742034 / 1000000);
UPDATE mytable
SET column = value_or_expr,
other_column = another_value_or_expr,
…
WHERE condition;
DELETE FROM mytable
WHERE condition;
CREATE TABLE IF NOT EXISTS mytable (
column DataType TableConstraint DEFAULT default_value,
another_column DataType TableConstraint DEFAULT default_value,
…
);
Type | Description |
---|---|
INTEGER, BOOLEAN | Same as in any programming language |
FLOAT, DOUBLE, REAL | Floating point numbers withy varying precision |
CHARACTER(num), VARCHAR(num), TEXT | Data type for strings. TEXT stores strings in all sorts of locales, the other data types impose a limit on the character number for performance |
DATE, DATETIME | It’s in the name |
BLOB | Binary Large OBject: usually multimedia or binary executables, usually stored with some form of metadata for identification purposes |
Constraint | Description |
---|---|
PRIMARY KEY | This means that the values in this column are unique, and each value can be used to identify a single row in this table. |
AUTOINCREMENT | For integer values, this means that the value is automatically filled in and incremented with each row insertion. Not supported in all databases. |
UNIQUE | The values in this column must be unique. It does not have to be a key for a row, unlike PRIMARY KEY. |
NOT NULL | This column cannot be ’NULL’ |
FOREIGN KEY | This is a consistency check which ensures that each value in this column corresponds to another value in a column in another table. |
ALTER TABLE mytable
ADD column DataType OptionalTableConstraint
DEFAULT default_value;
ALTER TABLE mytable
DROP column_to_be_deleted;
ALTER TABLE mytable