SQL Statements
INSERT
Used to add new records to a given table:
We can also insert multiple records at once by separating them with a comma:
SELECT
Basic syntax:
DROP
ALTER
Used to change the name of any table and any of its fields or to delete or add a new column to an existing table.
To rename a column:
We can change a column’s datatype:
We can drop a column :
UPDATE
Used to update specific records within a table, based on certain conditions.
Basic syntax:
For example:
QUERY
Sort Results
ORDER BY
By default, the sort is done in ascending order, but we can also sort the results by ASC
or DESC
:
Sort by multiple columns:
LIMIT
If the results are too big, we can limit the results to what we want only:
If we wanted to LIMIT results with an offset, we could specify the offset before the LIMIT count:
WHERE
e.g
e.g
LIKE Clause
enabling selecting records by matching a certain pattern.
e.g %
symbol acts as a wildcard and matches all characters after admin
.
_
symbol is used to match exactly one character.
Operators
AND
OR
NOT
Symbol
The AND
, OR
and NOT
operators can also be represented as &&
, ||
and !
, respectively.
Operators in queries
The following query lists all records where the username
is NOT john
:
Selects users who have their id
greater than 1
AND username
NOT equal to john
: