SQL Functions Operators and Key Words.docx
SQL Functions Operators and Key Words.docx
logic.
> Greater than SELECT * FROM table_name WHERE column_name > value;
< Less than SELECT * FROM table_name WHERE column_name < value;
>= Greater than or equal to SELECT * FROM table_name WHERE column_name >= value;
<= Less than or equal to SELECT * FROM table_name WHERE column_name <= value;
SELECT * FROM table_name WHERE column_name BETWEEN
BETWEEN Between a range value1 AND value2;
SELECT * FROM table_name WHERE column_name LIKE
LIKE Pattern matching pattern;
SELECT * FROM table_name WHERE column_name IN (value1,
IN Matches any value in a list value2, ...);
SELECT * FROM table_name WHERE column_name NOT IN
NOT IN Does not match any value in a list (value1, value2, ...);
IS NULL Checks if a value is NULL SELECT * FROM table_name WHERE column_name IS NULL;
IS NOT
NULL Checks if a value is not NULL SELECT * FROM table_name WHERE column_name IS NOT NULL;
SELECT * FROM table_name WHERE condition1 AND
AND Logical AND condition2;
SELECT * FROM table_name WHERE condition1 OR
OR Logical OR condition2;
NOT Negates a condition SELECT * FROM table_name WHERE NOT condition;
Used with IN or NOT IN for pattern
SELECT * FROM table_name WHERE column_name LIKE
LIKE matching pattern;
Function
Name Purpose Example SQL Statement
Counts the number of rows
COUNT() in a result set SELECT COUNT(*) FROM table_name WHERE condition;
SUM() Calculates the sum of values SELECT SUM(column_name) FROM table_name WHERE condition;
Calculates the average of
AVG() values SELECT AVG(column_name) FROM table_name WHERE condition;
MIN() Finds the minimum value SELECT MIN(column_name) FROM table_name WHERE condition;
MAX() Finds the maximum value SELECT MAX(column_name) FROM table_name WHERE condition;
Function
Name Purpose Example SQL Statement
Converts a string to
UPPER() uppercase SELECT UPPER(column_name) FROM table_name WHERE condition;
Converts a string to
LOWER() lowercase SELECT LOWER(column_name) FROM table_name WHERE condition;
Concatenates two or more
SELECT CONCAT(column1, column2) AS concatenated_string
CONCAT() strings FROM table_name WHERE condition;
Extracts a substring from a
SELECT SUBSTRING(column_name, start_index, length) FROM
SUBSTRING() string table_name WHERE condition;
SELECT DATE_FORMAT(date_column, 'format_string') FROM
DATE_FORMAT() Formats date values table_name WHERE condition;
Calculates the difference
SELECT DATEDIFF(end_date, start_date) AS date_difference
DATEDIFF() between dates FROM table_name WHERE condition;
Returns the first non-null
SELECT COALESCE(column1, column2, default_value) FROM
COALESCE() expression table_name WHERE condition;
Returns one of two values
SELECT IF(condition, value_if_true, value_if_false) FROM
IF() depending on a condition table_name;
CASE SELECT CASE WHEN condition1 THEN result1 WHEN condition2
statement Conditional logic THEN result2 ELSE default_result END FROM table_name;
DELETE Removes rows from a table DELETE FROM table_name WHERE condition;
Creates a new table, index, view, or
CREATE TABLE table_name (column1 datatype, column2
CREATE other database object datatype, ...);
ALTER Modifies an existing database object ALTER TABLE table_name ADD column_name datatype;
DROP Deletes an existing database object DROP TABLE table_name;
Removes all rows from a table, but
TRUNCATE keeps the structure TRUNCATE TABLE table_name;
Combines rows from two or more SELECT * FROM table1 JOIN table2 ON table1.column_name
JOIN tables based on a related column = table2.column_name;
eyword Description Example SQL Statement
INNER
Returns only the rows that have SELECT * FROM table1 INNER JOIN table2 ON
JOIN matching values in both tables table1.column_name = table2.column_name;
Returns all rows from the left table,
and the matched rows from the
LEFT SELECT * FROM table1 LEFT JOIN table2 ON
JOIN right table table1.column_name = table2.column_name;
Returns all rows from the right table,
RIGHT
and the matched rows from the left SELECT * FROM table1 RIGHT JOIN table2 ON
JOIN table table1.column_name = table2.column_name;
Returns rows when there is a match
FULL SELECT * FROM table1 FULL JOIN table2 ON
JOIN in one of the tables table1.column_name = table2.column_name;
Filters rows based on specified
WHERE conditions SELECT * FROM table_name WHERE condition;
Groups rows that have the same
SELECT column_name1, COUNT(column_name2) FROM
GROUP BY values into summary rows table_name GROUP BY column_name1;
Filters groups based on specified SELECT column_name1, COUNT(column_name2) FROM
table_name GROUP BY column_name1 HAVING
HAVING conditions COUNT(column_name2) > value;
Sorts the result set by one or more SELECT * FROM table_name ORDER BY column_name
ORDER BY columns ASC/DESC;
Limits the number of rows returned
LIMIT by a query SELECT * FROM table_name LIMIT number_of_rows;
Specifies the starting point for the
SELECT * FROM table_name LIMIT number_of_rows OFFSET
OFFSET results returned by the LIMIT clause offset_value;
DISTINCT Returns unique values in a column SELECT DISTINCT column_name FROM table_name;
Renames a column or table in the
AS result set SELECT column_name AS alias_name FROM table_name;