\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{ArturPuiu} \pdfinfo{ /Title (sql.pdf) /Creator (Cheatography) /Author (ArturPuiu) /Subject (SQL Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{BD4824} \definecolor{LightBackground}{HTML}{FAF3F1} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{SQL Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{ArturPuiu} via \textcolor{DarkBackground}{\uline{cheatography.com/133667/cs/39867/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}ArturPuiu \\ \uline{cheatography.com/arturpuiu} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 23rd March, 2025.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{2.204 cm} x{2.736 cm} x{2.66 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{General Commands}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{SELECT}} & Select data from a database. & SELECT {\emph{columns}} \{\{nl\}\} FROM {\emph{table}}; \{\{width=45\}\} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} {\bf{SELECT DISTINCT}} & Return only distinct values. & SELECT DISTINCT {\emph{columns}} \{\{nl\}\} FROM {\emph{table}}; \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} {\bf{WHERE}} & Extract records that fulfill a specified condition. & SELECT {\emph{columns}} \{\{nl\}\} FROM {\emph{table}} \{\{nl\}\} WHERE {\emph{condition}}; \tn % Row Count 13 (+ 5) % Row 3 \SetRowColor{white} {\bf{ORDER BY}} & Sort the result-set in ascending or descending order. \{\{nl\}\} {\emph{Ascending order is by default}}. & SELECT {\emph{columns}} \{\{nl\}\} FROM {\emph{table}} \{\{nl\}\} ORDER BY {\emph{column}} ASC|DESC; \tn % Row Count 20 (+ 7) % Row 4 \SetRowColor{LightBackground} {\bf{LIMIT}} & Specify the number of records to return. OFFSET is used to skip a specified number of rows. & SELECT {\emph{columns}} \{\{nl\}\} FROM {\emph{table}} \{\{nl\}\} WHERE {\emph{condition}} \{\{nl\}\} LIMIT {\emph{number}} \{\{nl\}\} OFFSET {\emph{number}}; \tn % Row Count 28 (+ 8) % Row 5 \SetRowColor{white} {\bf{AND, OR and NOT}} \{\{rowspan=4\}\} & Filter records based on more than one condition. Combined with WHERE. & \tn % Row Count 33 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.204 cm} x{2.736 cm} x{2.66 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{General Commands (cont)}} \tn % Row 6 \SetRowColor{LightBackground} & {\emph{The AND operator displays a record if all the conditions separated by AND are TRUE.}} & SELECT {\emph{columns}} \{\{nl\}\} FROM {\emph{table}} \{\{nl\}\} WHERE {\emph{cond1}} AND {\emph{cond2}}; \tn % Row Count 7 (+ 7) % Row 7 \SetRowColor{white} & {\emph{The OR operator displays a record if any of the conditions separated by OR is TRUE.}} & SELECT {\emph{columns}} \{\{nl\}\} FROM {\emph{table}} \{\{nl\}\} WHERE {\emph{cond1}} OR {\emph{cond2}}; \tn % Row Count 14 (+ 7) % Row 8 \SetRowColor{LightBackground} & {\emph{The NOT operator displays a record if the condition(s) is NOT TRUE}} & SELECT {\emph{columns}} \{\{nl\}\} FROM {\emph{table}} \{\{nl\}\} WHERE NOT {\emph{cond}}; \tn % Row Count 19 (+ 5) % Row 9 \SetRowColor{white} {\bf{BETWEEN}} & Selects values within a given range. The values can be numbers, text, or dates. {\emph{Is inclusive}}. & SELECT {\emph{columns}} \{\{nl\}\} FROM {\emph{table}} \{\{nl\}\} WHERE {\emph{column}} \{\{nl\}\} BETWEEN {\emph{value1}} AND {\emph{value2}}; \tn % Row Count 26 (+ 7) % Row 10 \SetRowColor{LightBackground} {\bf{IN}} & Allows to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions. & SELECT {\emph{columns}} \{\{nl\}\} FROM {\emph{table}} \{\{nl\}\} WHERE {\emph{col}} IN ({\emph{value1, value2}}); \tn % Row Count 34 (+ 8) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.204 cm} x{2.736 cm} x{2.66 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{General Commands (cont)}} \tn % Row 11 \SetRowColor{LightBackground} {\bf{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 {\emph{columns}} \{\{nl\}\} FROM {\emph{table}} \{\{nl\}\} WHERE {\emph{condition}} \{\{nl\}\} GROUP BY {\emph{columns}} \{\{nl\}\} ORDER BY {\emph{columns}}; \tn % Row Count 13 (+ 13) % Row 12 \SetRowColor{white} {\bf{LIKE}} \{\{rowspan=2\}\} & Is used in a WHERE clause to search for a specified pattern in a column. & SELECT {\emph{columns}}, \{\{nl\}\} FROM {\emph{table}} \{\{nl\}\} WHERE {\emph{column}} LIKE {\emph{pattern}}; \tn % Row Count 19 (+ 6) % Row 13 \SetRowColor{LightBackground} & 1.{\emph{The percent sign (\%) represents zero, one, or multiple characters}} \{\{nl\}\} 2.{\emph{The underscore sign (\_) represents one, single character}} & \tn % Row Count 29 (+ 10) % Row 14 \SetRowColor{white} {\bf{CASE}} \{\{rowspan=2\}\} & 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. & \tn % Row Count 52 (+ 23) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.204 cm} x{2.736 cm} x{2.66 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{General Commands (cont)}} \tn % Row 15 \SetRowColor{LightBackground} & CASE \{\{nl\}\} WHEN {\emph{condition1}} THEN {\emph{result1}} \{\{nl\}\} WHEN {\emph{condition2}} THEN {\emph{result2}} \{\{nl\}\} WHEN {\emph{conditionN}} THEN {\emph{resultN}} \{\{nl\}\} ELSE {\emph{result}} \{\{nl\}\} END; & \tn % Row Count 12 (+ 12) % Row 16 \SetRowColor{white} {\bf{HAVING}} & Was added to SQL because the WHERE keyword cannot be used with aggregate functions. & SELECT {\emph{columns}} \{\{nl\}\} FROM {\emph{table}} \{\{nl\}\} WHERE {\emph{condition}} \{\{nl\}\} GROUP BY {\emph{columns}} \{\{nl\}\} HAVING {\emph{condition}} \{\{nl\}\} ORDER BY {\emph{columns}}; \tn % Row Count 22 (+ 10) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.052 cm} x{2.812 cm} x{2.736 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{DATABASE/TABLE}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=20\}\} {\bf{CREATE SCHEMA}} & Create a new SQL schema & \{\{width=40\}\} CREATE SCHEMA {\emph{sch}}; \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} {\bf{CREATE DATABASE}} & Create a new SQL schema & CREATE SCHEMA {\emph{sch}}; \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{DROP SCHEMA}} & Drop a SQL schema & DROP SCHEMA {\emph{Sch}}; \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} {\bf{DROP DATABASE}} & Drop a SQL database & DROP DATABASE {\emph{db}}; \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} {\bf{SHOW DATABASES}} & Check the list of databases & SHOW DATABASES \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} {\bf{BACKUP DATABASE}} & Create a full back up of an existing SQL database. Ex: 'E:\textbackslash{}testDB.bak' & BACKUP DATABASE {\emph{db}} \{\{nl\}\} TO DISK = {\emph{'filepath'}}; \tn % Row Count 16 (+ 5) % Row 6 \SetRowColor{LightBackground} & A differential back up only backs up the parts of the database that have changed since the last full database backup. & BACKUP DATABASE {\emph{db}} \{\{nl\}\} TO DISK = {\emph{'filepath'}} \{\{nl\}\} WITH DIFFERENTIAL; \tn % Row Count 25 (+ 9) % Row 7 \SetRowColor{white} {\bf{CREATE TABLE}} \{\{rowspan=2\}\} & Create a new table in a database & CREATE TABLE {\emph{tb}} (\{\{nl\}\} {\emph{col1 datatype}}, \{\{nl\}\} {\emph{col2 datatype}}, \{\{nl\}\} {\emph{col3 datatype}} \{\{nl\}\}); \tn % Row Count 33 (+ 8) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.052 cm} x{2.812 cm} x{2.736 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{DATABASE/TABLE (cont)}} \tn % Row 8 \SetRowColor{LightBackground} & A copy of an existing table can also be created using CREATE TABLE. The new table will be filled with the existing values from the old table & CREATE TABLE {\emph{tb}} AS \{\{nl\}\} SELECT {\emph{col1, col2}},... \{\{nl\}\} FROM {\emph{existing tb}} \{\{nl\}\} WHERE ....; \tn % Row Count 10 (+ 10) % Row 9 \SetRowColor{white} {\bf{DROP TABLE}} & Drop an existing table in a database & DROP TABLE {\emph{tb}}; \tn % Row Count 13 (+ 3) % Row 10 \SetRowColor{LightBackground} {\bf{TRUNCATE TABLE}} & Delete the data inside a table, but not the table itself. & TRUNCATE TABLE {\emph{tb}}; \tn % Row Count 18 (+ 5) % Row 11 \SetRowColor{white} {\bf{ALTER TABLE}} \{\{rowspan=8\}\} & Is used to add, delete, or modify columns in an existing table & \tn % Row Count 23 (+ 5) % Row 12 \SetRowColor{LightBackground} & Add a column in a table & ALTER TABLE {\emph{tb}} \{\{nl\}\} ADD {\emph{col datatype}}; \tn % Row Count 27 (+ 4) % Row 13 \SetRowColor{white} & Delete a column in a table & ALTER TABLE {\emph{tb}} \{\{nl\}\} DROP COLUMN {\emph{col}}; \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.052 cm} x{2.812 cm} x{2.736 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{DATABASE/TABLE (cont)}} \tn % Row 14 \SetRowColor{LightBackground} & Rename a column in a table & ALTER TABLE {\emph{tb}} \{\{nl\}\} RENAME COLUMN {\emph{old\_name}} \{\{nl\}\} TO {\emph{new\_name}}; \tn % Row Count 5 (+ 5) % Row 15 \SetRowColor{white} & Rename a column in a table in SQL Server & EXEC sp\_rename {\emph{'tb\_name.old\_name'}}, \{\{nl\}\} {\emph{'new\_name'}}, {\emph{'COLUMN'}}; \tn % Row Count 10 (+ 5) % Row 16 \SetRowColor{LightBackground} & Change the data type of a column in a table & {\bf{SQL Server / MS Access}} \{\{nl\}\} ALTER TABLE {\emph{tb\_name}} \{\{nl\}\} ALTER COLUMN {\emph{col\_name datatype}}; \tn % Row Count 17 (+ 7) % Row 17 \SetRowColor{white} & & {\bf{My SQL / Oracle}} \{\{nl\}\} ALTER TABLE {\emph{tb\_name}} \{\{nl\}\} MODIFY COLUMN {\emph{col\_name datatype}}; \tn % Row Count 24 (+ 7) % Row 18 \SetRowColor{LightBackground} & & {\bf{Oracle 10G and later}} \{\{nl\}\} ALTER TABLE {\emph{tb\_name}} \{\{nl\}\} MODIFY {\emph{col\_name datatype}}; \tn % Row Count 31 (+ 7) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.052 cm} x{2.812 cm} x{2.736 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{DATABASE/TABLE (cont)}} \tn % Row 19 \SetRowColor{LightBackground} {\bf{Constraints}} & Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table is created with the ALTER TABLE statement. & CREATE TABLE {\emph{tb\_name}} \{\{nl\}\} ( \{\{nl\}\} {\emph{col1 datatype constraint, \{\{nl\}\} col2 datatype constraint}} \{\{nl\}\}); \tn % Row Count 11 (+ 11) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.2 cm} x{6.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Numeric Functions}} \tn % Row 0 \SetRowColor{LightBackground} MIN() & Returns the minimum value in a set of values. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} MAX() & Returns the maximum value in a set of values. \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{COUNT()} & Returns the number of records. NULL values are not counted. \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} AVG() & Returns the average value of an expression. NULL values are ignored. \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} SUM() & Calculates the sum of a set of values. NULL values are ignored. \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.88 cm} x{7.12 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Arithmetic Operators}} \tn % Row 0 \SetRowColor{LightBackground} + & Add \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} - & Subtract \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} * & Multiply \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} / & Divide \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \% & Modulo \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} x{7.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Bitwise Operators}} \tn % Row 0 \SetRowColor{LightBackground} \& & Bitwise AND \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} | & Bitwise OR \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \textasciicircum{} & Bitwise exclusive OR \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} x{7.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Comparison Operators}} \tn % Row 0 \SetRowColor{LightBackground} = & Equal to \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \textgreater{} & Greater than \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \textless{} & Less than \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \textgreater{}= & Greater than or equal to \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \textless{}= & Less than or equal to \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \textless{}\textgreater{} & Not equal to \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.88 cm} x{7.12 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Compound Operators}} \tn % Row 0 \SetRowColor{LightBackground} += & Add equals \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} -= & Subtract equals \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} *= & Multiply equals \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} /= & Divide equals \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \%= & Modulo equals \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \&= & Bitwise AND equals \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \textasciicircum{}-= & Bitwise exclusive equals \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} |*= & Bitwise OR equals \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.2 cm} x{6.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Logical Operators}} \tn % Row 0 \SetRowColor{LightBackground} ALL & TRUE if all of the subquery values meet the condition \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} AND & TRUE if all the conditions separated by AND is TRUE \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} ANY & TRUE if any of the subquery values meet the condition \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{BETWEEN} & TRUE if the operand is within the range of comparisons \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{EXISTS} & TRUE if the subquery returns one or more records \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} IN & TRUE if the operand is equal to one of a list of expressions \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} LIKE & TRUE if the operand matches a pattern \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} NOT & Displays a record if the condition(s) is NOT TRUE \tn % Row Count 16 (+ 2) % Row 8 \SetRowColor{LightBackground} OR & TRUE if any of the conditions separated by OR is TRUE \tn % Row Count 18 (+ 2) % Row 9 \SetRowColor{white} SOME & TRUE if any of the subquery values meet the condition \tn % Row Count 20 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.672 cm} x{2.964 cm} x{2.964 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Joining Tables}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{INNER JOIN}} & Selects records that have matching values in both tables. & SELECT {\emph{columns}} \{\{nl\}\} FROM {\emph{table1}} \{\{nl\}\} INNER JOIN {\emph{table2}} \{\{nl\}\} ON {\emph{table1.col}} = {\emph{table2.col}}; \{\{width=48\}\} \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} {\bf{LEFT JOIN}} & Returns all records from the left table (table1), and the matching records (if any) from the right table (table2). & SELECT \seqsplit{column\_name(s)} \{\{nl\}\} FROM table1 \{\{nl\}\} LEFT JOIN table2 \{\{nl\}\} ON \seqsplit{table1.column\_name} = \{\{nl\}\} \seqsplit{table2.column\_name;} \tn % Row Count 17 (+ 9) % Row 2 \SetRowColor{LightBackground} {\bf{RIGHT JOIN}} & Returns all records from the right table (table2), and the matching records (if any) from the left table (table1). & SELECT \seqsplit{column\_name(s)} \{\{nl\}\} FROM table1 \{\{nl\}\} RIGHT JOIN table2 \{\{nl\}\} ON \seqsplit{table1.column\_name} = \{\{nl\}\} \seqsplit{table2.column\_name;} \tn % Row Count 26 (+ 9) % Row 3 \SetRowColor{white} {\bf{CROSS JOIN}} & Returns all records from both tables (table1 and table2). & SELECT \seqsplit{column\_name(s)} \{\{nl\}\} FROM table1 \{\{nl\}\} CROSS JOIN table2; \tn % Row Count 31 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{1.672 cm} x{2.964 cm} x{2.964 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Joining Tables (cont)}} \tn % Row 4 \SetRowColor{LightBackground} {\bf{SELF JOIN}} & A self join is a regular join, but the table is joined with itself. & SELECT \seqsplit{column\_name(s)} \{\{nl\}\} FROM table1 T1, table1 T2 \{\{nl\}\} WHERE condition; \tn % Row Count 6 (+ 6) % Row 5 \SetRowColor{white} {\bf{UNION}} \{\{rowspan=3\}\} & \{\{colspan=2\}\} Combine the result-set of two or more SELECT statements. {\emph{1) Every SELECT statement within UNION must have the same number of columns 2) The columns must also have similar data types 3) The columns in every SELECT statement must also be in the same order}} & \tn % Row Count 24 (+ 18) % Row 6 \SetRowColor{LightBackground} & UNION Syntax. {\emph{Selects only distinct values by default. To allow duplicate values, use UNION ALL}} & SELECT \seqsplit{column\_name(s)} FROM table1 \{\{nl\}\} UNION \{\{nl\}\} SELECT \seqsplit{column\_name(s)} FROM table2; \tn % Row Count 31 (+ 7) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{1.672 cm} x{2.964 cm} x{2.964 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Joining Tables (cont)}} \tn % Row 7 \SetRowColor{LightBackground} & UNION ALL & SELECT \seqsplit{column\_name(s)} FROM table1 \{\{nl\}\} UNION ALL \{\{nl\}\} SELECT \seqsplit{column\_name(s)} FROM table2; \tn % Row Count 7 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Date Functions}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{ADDDATE() OR DATE\_ADD()}} & adds a time/date interval to a date and then returns the date.\{\{nl\}\} {\emph{ADDDATE(date, INTERVAL value addunit)}} \{\{nl\}\}{\emph{ADDDATE(date, days)}} \{\{nl\}\}{\emph{DATE\_ADD(date, INTERVAL value addunit)}} \tn % Row Count 9 (+ 9) % Row 1 \SetRowColor{white} {\bf{ADDTIME()}} & Adds a time interval to a time/datetime and then returns the time/datetime. {\emph{ADDTIME(datetime, addtime)}} \tn % Row Count 14 (+ 5) % Row 2 \SetRowColor{LightBackground} {\bf{CURDATE()}} & Returns the current date. The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). This function equals the CURDATE() function. {\emph{CURDATE()}} \tn % Row Count 22 (+ 8) % Row 3 \SetRowColor{white} {\bf{CURRENT\_DATE()}} & Returns the current date. The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). This function equals the CURRENT\_DATE() function. {\emph{CURRENT\_DATE()}} \tn % Row Count 30 (+ 8) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Date Functions (cont)}} \tn % Row 4 \SetRowColor{LightBackground} {\bf{CURRENT\_TIME()}} & Returns the current time. The time is returned as "HH-MM-SS" (string) or as HHMMSS.uuuuuu (numeric). This function equals the CURTIME() function. {\emph{CURRENT\_TIME()}} \tn % Row Count 8 (+ 8) % Row 5 \SetRowColor{white} {\bf{CURTIME()}} & Returns the current time. The time is returned as "HH-MM-SS" (string) or as HHMMSS.uuuuuu (numeric). This function equals the CURRENT\_TIME() function. {\emph{CURTIME()}} \tn % Row Count 16 (+ 8) % Row 6 \SetRowColor{LightBackground} {\bf{CURRENT\_TIMESTAMP()}} & Returns the current date and time. The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS.uuuuuu (numeric). {\emph{CURRENT\_TIMESTAMP()}} \tn % Row Count 24 (+ 8) % Row 7 \SetRowColor{white} {\bf{DATE()}} & Extracts the date part from a datetime expression. {\emph{DATE(expression)}} \tn % Row Count 28 (+ 4) % Row 8 \SetRowColor{LightBackground} {\bf{DATEDIFF()}} & Returns the number of days between two date values. {\emph{DATEDIFF(date1, date2)}} \tn % Row Count 32 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Date Functions (cont)}} \tn % Row 9 \SetRowColor{LightBackground} {\bf{DATE\_FORMAT()}} & Formats a date as specified.{\emph{ DATE\_FORMAT(date, format)}} \tn % Row Count 3 (+ 3) % Row 10 \SetRowColor{white} {\bf{DATE\_SUB()}} & Subtracts a time/date interval from a date and then returns the date. {\emph{DATE\_SUB(date, INTERVAL value interval)}} \tn % Row Count 9 (+ 6) % Row 11 \SetRowColor{LightBackground} DAY() OR DAYOFMONTH & returns the day of the month for a given date (a number from 1 to 31). {\emph{DAY(date)}} {\emph{DAYOFMONTH(date)}} \tn % Row Count 14 (+ 5) % Row 12 \SetRowColor{white} DAYNAME() & returns the weekday name for a given date. {\emph{DAYNAME(date)}} \tn % Row Count 17 (+ 3) % Row 13 \SetRowColor{LightBackground} DAYOFWEEK() & returns the weekday index for a given date (a number from 1 to 7). 1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday. {\emph{DAYOFWEEK(date)}} \tn % Row Count 25 (+ 8) % Row 14 \SetRowColor{white} DAYOFWEEK(date) & returns the day of the year for a given date (a number from 1 to 366). {\emph{DAYOFYEAR(date)}} \tn % Row Count 29 (+ 4) % Row 15 \SetRowColor{LightBackground} EXTRACT() & extracts a part from a given date. {\emph{EXTRACT(part FROM date)}} \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Date Functions (cont)}} \tn % Row 16 \SetRowColor{LightBackground} FROM\_DAYS() & returns a date from a numeric datevalue.is to be used only with dates within the Gregorian calendar. is the opposite of the TO\_DAYS() function. {\emph{FROM\_DAYS(number)}} \tn % Row Count 8 (+ 8) % Row 17 \SetRowColor{white} HOUR() & returns the hour part for a given date (from 0 to 838). HOUR(datetime) \tn % Row Count 12 (+ 4) % Row 18 \SetRowColor{LightBackground} LAST\_DAY() & extracts the last day of the month for a given date. LAST\_DAY(date) \tn % Row Count 16 (+ 4) % Row 19 \SetRowColor{white} LOCALTIME() OR LOCALTIMESTAMP() & returns the current date and time. The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS.uuuuuu (numeric). LOCALTIME() \tn % Row Count 23 (+ 7) % Row 20 \SetRowColor{LightBackground} MAKEDATE() & creates and returns a date based on a year and a number of days value. MAKEDATE(year, day) \tn % Row Count 28 (+ 5) % Row 21 \SetRowColor{white} MAKETIME() & Create and return a time value based on an hour, minute, and second value. MAKETIME(hour, minute, second) \tn % Row Count 33 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Date Functions (cont)}} \tn % Row 22 \SetRowColor{LightBackground} MICROSECOND() & Return the microsecond part of a datetime. MICROSECOND(datetime) \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}