MySQL Clauses Flashcards
SELECT
This clause is used to view something in MySQL. It can be called with/without a table.
EX - SELECT * FROM table_name;
SELECT 1 + 2;
WHERE
This clause is used to provide condition to the SELECT clause.
Evaluated before SELECT.
FROM
Used to specify the table which we need to display/manipulate.
This is evaluated first.
FROM -> WHERE -> SELECT -> DISTINCT -> ORDER BY
ORDER BY
Used to order the result set in ASC/DESC order.
Ex - SELECT * FROM table_name ORDER BY column_name ASC/DESC;
DISTINCT
Used to return only DISTINCT values on the result set.
EX - SELECT DISTINCT(column_name) FROM table_name;
IN/NOT IN
This clause is used when the column can take only certain discrete values.
Used when we wish to compare to a list of values.
EX - SELECT * FROM emp WHERE city IN (‘NYC’, ‘AU’, ‘DC’);
SELECT * FROM emp WHERE city NOT IN (‘NYC’, ‘AU’, ‘DC’);
BETWEEN
Used when we want to check if a condition falls in a range.
Ex - SELECT * FROM table_name WHERE column_name BETWEEN lower AND upper;
LIKE
Used to match pattern (like regex)
Ex - SELECT * FROM table_name WHERE column_name LIKE ‘a%’;
% means 0 or more characters
_ means 1 character