0% found this document useful (0 votes)
4 views

SQL Cheat Sheet - Functions and Implicit Join

The document is a SQL cheat sheet that outlines various SQL functions such as COUNT, AVG, SUM, MIN, MAX, ROUND, LENGTH, UCASE, and LCASE, along with their syntax and examples. It also covers date functions like DATEDIFF, DATE_ADD, and DATE_SUB, as well as subqueries and implicit joins. The cheat sheet serves as a quick reference for SQL commands and their usage in database queries.

Uploaded by

kaif mohammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL Cheat Sheet - Functions and Implicit Join

The document is a SQL cheat sheet that outlines various SQL functions such as COUNT, AVG, SUM, MIN, MAX, ROUND, LENGTH, UCASE, and LCASE, along with their syntax and examples. It also covers date functions like DATEDIFF, DATE_ADD, and DATE_SUB, as well as subqueries and implicit joins. The cheat sheet serves as a quick reference for SQL commands and their usage in database queries.

Uploaded by

kaif mohammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

11/25/24, 5:18 PM about:blank

SQL Cheat Sheet: FUNCTIONS and Implicit JOIN

Command Syntax (MySQL/DB2) Description Example (MySQL/DB2)

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).

UCASE SELECT UCASE(column_name) UCASE function SELECT UCASE(f_name) FROM


FROM table; displays the employees;
column name

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.

DATE_ADD() SELECT DATE_ADD(date, DATE_ADD() is SELECT DATE_ADD(date, INTERVAL 3


INTERVAL n type); used to DAY);;
calculate the
date after
lapse of
mentioned
number of
units of date
type, i.e. if
n=3 and

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

You might also like