SQL Operators Functions and Keywords
SQL Operators Functions and Keywords
Working with functions and operators in SQL is essential for performing various operations on
data within relational databases.
OPERATORS, OPERANDS, EXPRESSIONS
WHAT IS AN OPERATOR?
AN OPERATOR: is a symbol that tells the compiler/interpreter to perform specific
mathematical, relational or logical operation to produce a desired result.
In 5 + 8 =13, a (+) is an operator.
WHAT IS AN OPERAND?
AN OPERAND: is an object which is operated upon by any operator to produce a result.
In 5 + 8 =13, (5 and 8) are operands.
WHAT IS AN EXPRESSION?
AN EXPRESSION: is a combination of one or more operators and operands which together
produce a meaningful result.
In 5 + 8 =13, (5 + 8) is an expression.
NB: every operator has its own type of operands on which they can operate. They cannot operate
on every kind of operands.
Basically, there are four types of operators.
OPERATORS
ARITHMETIC OPERATORS - (+,-,/,*,%)
COMPARISON OPERATORS - (=,>;<,>=,<=,<>/!=)
LOGICAL OPERATORS - (AND, OR,LIKE,BETWEEN,NOT)
BITWISE OPERATORS - (&,|,^) : This will not be covered
ARITHMETIC OPERATORS
They are used for mathematical calculations.
COMPARISON OPERATORS
They are used to compare the operands and give results in TRUE & FALSE
LOGICAL OPERATORS
They connect two or more Boolean expressions such that the result of the compound expression
depends on the result of the original expressions and on the meaning of the operator. Results are
in True and False.
AND 3!=5 AND 11>5 Gives true if all conditions separated by AND are true
OR 5>4 OR 10=6 Gives true if any conditions separated by OR is
TRUE.
NOT NOT 2<4 Gives true if condition is false, false if condition is
true
LIKE ‘Hello’ LIKE ‘%o’ Gives true if operand matches a pattern
BETWEEN 4 BETWEEN 1 AND 10 Gives true if operand is within the range of
comparisons.
MORE FROM AI
Fundamental operators for constructing SQL queries with various conditions and 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, ...);
Modifies an existing database
ALTER object ALTER TABLE table_name ADD column_name datatype;
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
SELECT * FROM table1 LEFT JOIN table2 ON
LEFT JOIN right table table1.column_name = table2.column_name;
Returns all rows from the right
RIGHT
table, and the matched rows from SELECT * FROM table1 RIGHT JOIN table2 ON
JOIN the left table table1.column_name = table2.column_name;
Returns rows when there is a match
SELECT * FROM table1 FULL JOIN table2 ON
FULL 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;
PRACTICAL EXAMPLE
1. Grouping Data:
-- Group exam scores by subject and calculate average score for each subject
SELECT subject, AVG(score) AS average_score
FROM exam_scores GROUP BY subject;
2. Aggregating Data:
-- Calculate the total number of students and average age of students
SELECT COUNT(*) AS total_students, AVG(age) AS average_age FROM students;
NB: This query will group the exam scores by subject and calculate the average score for each subject, but
it will only display subjects with an average score greater than 85.