Syntax
SELECT name, city
FROM customers
WHERE country = 'Mexico';
ORDER BY postalcode ASC;
Basics
It is not case sensitive (yay! select
is the same as
SELECT
) and some db require semicolon ( ; ) at the end of SQL statement.
To retrieve information from the database we use the command SELECT.
SELECT
*
retrieves everything.
Operators
AND, OR, NOT
Keywords and statements
ORDER BY ASC
ORDER BY DESC
GROUP BY
Example: SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country ORDER BY COUNT(CustomerID) DESC;
Functions
MIN()
MAX()
COUNT() number of rows
AVG()
SUM()
Example: SELECT MIN(Price) AS SmallestPrice FROM Products;
Clauses and operators
WHERE
equal =
not equal
<>
multiple values
IN ('a','b')
ranges
BETWEEEN x AND y
pattern LIKE 'a%'
operators
>, <, >=, <=
condition1 AND condition2
condition1 OR condition2
NOT condition
IS NULL
IS NOT NULL
Examples:SELECT * FROM Customers
WHERE City IN ('Paris','London');
SELECT * FROM Products
WHERE Price BETWEEN 50 AND 60;
SELECT * FROM Customers
WHERE City LIKE 's%';
> Strasbourg, Sao Paulo, Sevilla...
Wildcards
a%
%a
%ab%
a_%_%