SQL Cheat Sheet - Functions and Implicit Join
SQL Cheat Sheet - Functions and Implicit Join
COUNT function
returns the
SELECT COUNT(column_name) number of
SELECT COUNT(dep_id) FROM
COUNT FROM table_name WHERE rows that employees;
condition; match a
specified
criterion.
AVG function
SELECT AVG(column_name) returns the
SELECT AVG(salary) FROM
AVG FROM table_name WHERE average value employees;
condition; of a numeric
column.
SUM function
SELECT SUM(column_name) returns the
SELECT SUM(salary) FROM
SUM FROM table_name WHERE total sum of a employees;
condition; numeric
column.
MIN function
returns the
SELECT MIN(column_name)
smallest value SELECT MIN(salary) FROM
MIN FROM table_name WHERE
condition; of the employees;
SELECTED
column.
MAX function
returns the
SELECT MAX(column_name)
largest value SELECT MAX(salary) FROM
MAX FROM table_name WHERE
condition; of the employees;
SELECTED
column.
ROUND function
rounds a
SELECT ROUND(2number, number to a
SELECT ROUND(salary) FROM
ROUND decimals, operation) AS specified employees;
RoundValue; number of
decimal
places.
LENGTH
function
SELECT LENGTH(column_name) returns the SELECT LENGTH(f_name) FROM
LENGTH FROM table; length of a employees;
string (in
bytes).
about:blank 1/4
11/25/24, 5:18 PM about:blank
in each table
in uppercase.
LCASE function
displays the
SELECT LCASE(column_name) SELECT LCASE(f_name) FROM
LCASE FROM table;
column name employees;
in each table
in lowercase.
DISTINCT
function is
SELECT DISTINCT used to SELECT DISTINCT UCASE(f_name)
DISTINCT column_name FROM table; display data FROM employees;
without
duplicates.
DAY function
returns the day SELECT DAY(b_date) FROM
SELECT DAY(column_name)
DAY FROM table
of the month employees where emp_id =
for a given 'E1002';
date.
CURRENT_DATE
is used to
CURRENT_DATE SELECT CURRENT_DATE;
display the SELECT CURRENT_DATE;
current date.
DATEDIFF() is
used to
calculate the
difference
between two
SELECT DATEDIFF(date1, dates or time SELECT DATEDIFF(CURRENT_DATE,
DATEDIFF() date2); stamps. The date_column) FROM table;
default value
generated is
the difference
in number of
days.
FROM_DAYS()is
used to
convert a SELECT
SELECT
FROM_DAYS() FROM_DAYS(number_of_days);
given number FROM_DAYS(DATEDIFF(CURRENT_DATE,
of days to date_column)) FROM table;
YYYY-MM-
DD format.
about:blank 2/4
11/25/24, 5:18 PM about:blank
type=DAY,
the result is a
date 3 days
after what is
mentioned in
date column.
The type
valiable can
also be
months or
years.
DATE_SUB() is
used to
calculate the
date prior to
the record date
by mentioned
number of
units of date
type, i.e. if
n=3 and
SELECT DATE_SUB(date, SELECT DATE_SUB(date, INTERVAL 3
DATE_SUB() INTERVAL n type);
type=DAY, DAY);;
the result is a
date 3 days
before what is
mentioned in
date column.
The type
valiable can
also be
months or
years.
Subquery is a
query within
SELECT emp_id, f_name, l_name,
another SQL salary
query and FROM employees
embedded where salary
within the < (SELECT AVG(salary)
SELECT column_name [, WHERE FROM employees);
column_name ] FROM table1 clause.
[, table2 ] WHERE
Subquery column_name OPERATOR
A subquery is
SELECT * FROM ( SELECT emp_id,
(SELECT column_name [, f_name, l_name, dep_id FROM
column_name ] FROM table1 used to return employees) AS emp4all;
[, table2 ] [WHERE]) data that will
be used in the
main query as
a condition to SELECT * FROM employees WHERE
job_id IN (SELECT job_ident FROM
further restrict jobs);
the data to be
retrieved.
Implicit Inner Join SELECT column_name(s) FROM Implicit SELECT * FROM employees, jobs
table1, table2 WHERE Inner Join where employees.job_id =
table1.column_name = combines two jobs.job_ident;
table2.column_name; or more
records but
about:blank 3/4
11/25/24, 5:18 PM about:blank
displays only
matching
values in both
tables. Inner
join applies
only the
specified
columns.
Implicit
Cross Join is
defined as a
Cartesian
product where
SELECT column_name(s) FROM the number of
Implicit Cross Join table1, table2; rows in the SELECT * FROM employees, jobs;
first table is
multiplied by
the number of
rows in the
second table.
Author(s)
Lakshmi Holla
Abhishek Gagneja
about:blank 4/4