Basic Concepts
|
display list of databases available |
|
display list of tables in a selected database |
SHOW COLUMNS FROM <table_name>;
|
show columns in a table |
SELECT <column,list> FROM <table_name>;
|
retrieve data in columns of a table |
SELECT * FROM <table_name>;
|
list all data in a table |
SELECT DISTINCT <column,names> FROM <table_name>;
|
avoid duplicates and return unique ones |
|
limit number of results |
|
from array index of 3 return 4 items |
SELECT <table.column> FROM <table>;
|
fully qualified column.name |
... ORDER BY <column_name>;
|
sort alphabetically of numerically default ascending order |
... ORDER BY <col1,col2>;
|
first order 1 col then form 2nd col |
Join, Table Operations
SELECT customers.ID, customers.Name, orders.Name, orders.Amount FROM customers, orders WHERE customers.ID=orders.Customer_ID ORDER BY customers.ID;
|
joining tables |
SELECT ct.ID, ct.Name, ord.Name, ord.Amount FROM customers AS ct, orders AS ord WHERE ct.ID=ord.Customer_ID ORDER BY ct.ID;
|
renaming tables |
|
|
Filtering, Functions, Subqueries
... WHERE <condition>;
|
ex; ID = 7; name = 'John'/ ='can''t' |
=, !=, >, <, >=, <=, BETWEEN |
comparison operators |
... ... BETWEEN <val1> AND <val2;>
|
returns val1 to val2 |
|
logical operators |
... ... AND (val1=00 OR val2=88);
|
combining logics |
... ... IN ('val1', 'val2', 'val3');
|
multiple OR alternative |
... ... NOT IN ('val1', 'val2', 'val3');
|
exclude list items |
SELECT CONCAT(col1, ', ' , col2) FROM <table>;
|
returns the concatenating string. |
SELECT CONCAT(col1, ', ' , col2) AS new_col FROM <table>;
|
concat results in new col |
... Salary+500 AS Salary FROM <table>;
|
do operation on filed value |
... val(UPPER) AS new_val ...
|
change to uppercase |
LOWER(val), SQRT(val), MIN(col), AVG(col), SUM(col), POWER(val)
|
math / str functions |
... ... ORDER BY <col> DESC
|
order by desc or ASC |
... WHERE Salary > (SELECT AVG(Salary) FROM employees) ...
|
sub quaries |
|
pattern = _ single char, % multiple char |
|