Skip to content

Queries

Select All Elements

SELECT * FROM table;

Remove Duplicated Elements

SELECT DISTINCT col FROM table;

Selection with Conditions

SELECT * FROM mytable WHERE attr = 'something';

Comparison Operators

OperatorMean
=Equal
<> or !=Not equal
<Less than
>More than
<=Less than or equal
>=More than or equal
<=>Safe equal
IS NULL / IS NOT NULLEqual to NULL

Only <=> can be used to judge two NULL are equal.

Logical Operators

OperatorMean
AND or &&AND
OR or `OR
NOT or !NOT

Other Condition Operators

OperatorMean
BETWEEN value1 AND value2value1 <= value AND value <= value2
IN (...)In the set (...)
NOT IN (...)Not in the set (...)
LIKEFuzzy match.
REGEXP or RLIKERegular expression match.

Wildcard Match

% can match multiple random characters.

_ can match only one random character.

CONCAT

SELECT CONCAT(col_1, ' ', col_2) 'Alias Name' FROM table;

Sort

Default is ascending.

SELECT col_1, col_2 FROM table ORDER BY col_1;

Sort in descending order:

SELECT col_1, col_2 FROM table ORDER BY col_1 DESC;

Sub Query

SELECT * FROM
  (SELECT * FROM table WHERE attr1 LIKE 'A%') AS new_table
  WHERE new_table.attr2 LIKE 'AB%';
Last updated on