SELECT |
Select data from a database. |
SELECT column1, column2 FROM table_name; |
SELECT DISTINCT |
Return only distinct values. |
SELECT DISTINCT column1, column2 FROM table_name; |
WHERE |
Extract records that fulfill a specified condition. |
SELECT column1, column2 FROM table_name WHERE condition; |
AND, OR and NOT |
Filter records based on more than one condition. Combined with WHERE. |
The AND operator displays a record if all the conditions separated by AND are TRUE. |
SELECT column1, column2 FROM table_name WHERE condition1 AND condition2; |
The OR operator displays a record if any of the conditions separated by OR is TRUE. |
SELECT column1, column2 FROM table_name WHERE condition1 OR condition2; |
The NOT operator displays a record if the condition(s) is NOT TRUE |
SELECT column1, column2 FROM table_name WHERE NOT condition; |
ORDER BY |
Sort the result-set in ascending or descending order. Ascending order is by default. |
SELECT column1, column2 FROM table_name ORDER BY column1 ASC|DESC; |
INSERT INTO |
Insert new records in a table. |
1. Specify both the column names and the values. |
INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2) |
If you are adding values for all the columns of the table, no need to specify the column names. |
INSERT INTO table_name VALUES (value1, value2); |
NULL |
A field with a NULL value is a field with no value. A field with a NULL value is one that has been left blank during record creation. |
IS NULL Syntax |
SELECT column_names FROM table_name WHERE column_name IS NULL; |
IS NOT NULL Syntax |
SELECT column_names FROM table_name WHERE column_name IS NOT NULL; |
UPDATE |
Modify the existing records in a table. |
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition; |
DELETE |
Delete existing records in a table. |
DELETE FROM table_name WHERE condition; |
LIMIT |
Specify the number of records to return. |
SELECT column_name(s) FROM table_name WHERE condition LIMIT number; |
IN |
Allows to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions. |
SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2); |
|
SELECT column_name(s) FROM table_name WHERE column_name IN (SELECT STATEMENT); |
BETWEEN |
Selects values within a given range. The values can be numbers, text, or dates. Is inclusive. |
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2; |
GROUP BY |
Groups rows that have the same values into summary rows. Is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns. |
SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) ORDER BY column_name(s); |
HAVING |
Was added to SQL because the WHERE keyword cannot be used with aggregate functions |
SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition ORDER BY column_name(s); |
EXISTS |
Test for the existence of any record in a subquery. Returns TRUE if the subquery returns one or more records. |
SELECT column_name(s) FROM table_name WHERE EXISTS (SELECT column_name FROM table_name WHERE condition); |
ANY |
Allow to perform a comparison between a single column value and a range of other values. 1.Returns a boolean value as a result 2. Returns TRUE if ANY of the subquery values meet the condition. ANY means that the condition will be true if the operation is true for any of the values in the range. |
SELECT column_name(s) FROM table_name WHERE column_name operator ANY (SELECT column_name FROM table_name WHERE condition); |
ALL |
Returns: 1. A boolean value as a result 2. Returns TRUE if ALL of the subquery values meet the condition 3. Is used with SELECT, WHERE and HAVING statements. ALL means that the condition will be true only if the operation is true for all values in the range. |
ALL Syntax With SELECT |
SELECT ALL column_name(s) FROM table_name WHERE condition; |
ALL Syntax With WHERE or HAVING |
SELECT column_name(s) FROM table_name WHERE column_name operator ALL (SELECT column_name FROM table_name WHERE condition); |
INSERT INTO SELECT |
Copies data from one table and Inserts it into another table. Requires that the data types in source and target tables matches.The existing records in the target table are unaffected. |
|
Copy all columns from one table to another table |
INSERT INTO table2 SELECT * FROM table1 WHERE condition; |
|
Copy only some columns from one table into another table |
INSERT INTO table2 (col1, col2 ...) SELECT col1, col2 ... FROM table1 WHERE condition; |
CASE |
Goes through conditions and returns a value when the first condition is met (like an if-then-else statement). So, once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the ELSE clause. If there is no ELSE part and no conditions are true, it returns NULL. |
CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 WHEN conditionN THEN resultN ELSE result END; |