\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 19th November, 2023.\\ 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 column1, column2 \{\{nl\}\} FROM table\_name; \{\{width=47\}\} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} {\bf{SELECT DISTINCT}} & Return only distinct values. & SELECT DISTINCT column1, column2 \{\{nl\}\} FROM table\_name; \tn % Row Count 9 (+ 4) % Row 2 \SetRowColor{LightBackground} {\bf{WHERE}} & Extract records that fulfill a specified condition. & SELECT column1, column2 \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE condition; \tn % Row Count 15 (+ 6) % Row 3 \SetRowColor{white} {\bf{AND, OR and NOT}} \{\{rowspan=4\}\} & Filter records based on more than one condition. Combined with WHERE. & \tn % Row Count 20 (+ 5) % Row 4 \SetRowColor{LightBackground} & {\emph{The AND operator displays a record if all the conditions separated by AND are TRUE.}} & SELECT column1, column2 \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE condition1 AND condition2; \tn % Row Count 27 (+ 7) % Row 5 \SetRowColor{white} & {\emph{The OR operator displays a record if any of the conditions separated by OR is TRUE.}} & SELECT column1, column2 \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE condition1 OR condition2; \tn % Row Count 34 (+ 7) \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 NOT operator displays a record if the condition(s) is NOT TRUE}} & SELECT column1, column2 \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE NOT condition; \tn % Row Count 6 (+ 6) % Row 7 \SetRowColor{white} {\bf{ORDER BY}} & Sort the result-set in ascending or descending order. \{\{nl\}\} {\emph{Ascending order is by default}}. & SELECT column1, column2 \{\{nl\}\} FROM table\_name \{\{nl\}\} ORDER BY column1 ASC|DESC; \tn % Row Count 13 (+ 7) % Row 8 \SetRowColor{LightBackground} {\bf{INSERT INTO}} \{\{rowspan=3\}\} & Insert new records in a table. & \tn % Row Count 16 (+ 3) % Row 9 \SetRowColor{white} & {\emph{1. Specify both the column names and the values.}} & INSERT INTO table\_name \{\{nl\}\} (column1, column2, column3) \{\{nl\}\} VALUES (value1, value2) \tn % Row Count 23 (+ 7) % Row 10 \SetRowColor{LightBackground} & {\emph{ If you are adding values for all the columns of the table, no need to specify the column names.}} & INSERT INTO table\_name \{\{nl\}\} VALUES (value1, value2); \tn % Row Count 30 (+ 7) \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{NULL}} \{\{rowspan=3\}\} & A field with a NULL value is a field with no value. A field with a NULL value is one that has been left blank during record creation. & \tn % Row Count 10 (+ 10) % Row 12 \SetRowColor{white} & {\emph{IS NULL Syntax}} & SELECT column\_names \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE column\_name IS NULL; \tn % Row Count 16 (+ 6) % Row 13 \SetRowColor{LightBackground} & {\emph{IS NOT NULL Syntax}} & SELECT column\_names \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE column\_name IS NOT NULL; \tn % Row Count 22 (+ 6) % Row 14 \SetRowColor{white} {\bf{UPDATE}} & Modify the existing records in a table. & UPDATE table\_name \{\{nl\}\} SET column1 = value1, column2 = value2 \{\{nl\}\} WHERE condition; \tn % Row Count 29 (+ 7) % Row 15 \SetRowColor{LightBackground} {\bf{DELETE}} & Delete existing records in a table. & DELETE FROM table\_name \{\{nl\}\} WHERE condition; \tn % Row Count 33 (+ 4) \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 16 \SetRowColor{LightBackground} {\bf{LIMIT}} & Specify the number of records to return. & SELECT \seqsplit{column\_name(s)} \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE condition \{\{nl\}\} LIMIT number; \tn % Row Count 7 (+ 7) % Row 17 \SetRowColor{white} {\bf{IN}} \{\{rowspan=2\}\} & Allows to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions. & SELECT \seqsplit{column\_name(s)} \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE column\_name IN (value1, value2); \tn % Row Count 15 (+ 8) % Row 18 \SetRowColor{LightBackground} & & SELECT \seqsplit{column\_name(s)} \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE column\_name \{\{nl\}\} IN (SELECT STATEMENT); \tn % Row Count 23 (+ 8) % Row 19 \SetRowColor{white} {\bf{BETWEEN}} & Selects values within a given range. The values can be numbers, text, or dates. {\emph{Is inclusive}}. & SELECT \seqsplit{column\_name(s)} \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE column\_name \{\{nl\}\} BETWEEN value1 AND value2; \tn % Row Count 31 (+ 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 20 \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 \seqsplit{column\_name(s)} \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE condition \{\{nl\}\} GROUP BY \seqsplit{column\_name(s)} \{\{nl\}\} ORDER BY \seqsplit{column\_name(s);} \tn % Row Count 13 (+ 13) % Row 21 \SetRowColor{white} {\bf{HAVING}} & Was added to SQL because the WHERE keyword cannot be used with aggregate functions & SELECT \seqsplit{column\_name(s)} \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE condition \{\{nl\}\} GROUP BY \seqsplit{column\_name(s)} \{\{nl\}\} HAVING condition \{\{nl\}\} ORDER BY \seqsplit{column\_name(s);} \tn % Row Count 24 (+ 11) % Row 22 \SetRowColor{LightBackground} {\bf{EXISTS}} & Test for the existence of any record in a subquery. Returns TRUE if the subquery returns one or more records. & SELECT \seqsplit{column\_name(s)} \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE EXISTS \{\{nl\}\} (SELECT column\_name \{\{nl\}\} FROM table\_name WHERE condition); \tn % Row Count 34 (+ 10) \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 23 \SetRowColor{LightBackground} {\bf{ANY}} & Allow to perform a comparison between a single column value and a range of other values. \{\{nl\}\} {\emph{1.Returns a boolean value as a result 2. Returns TRUE if ANY of the subquery values meet the condition.}} \{\{nl\}\} ANY means that the condition will be true if the operation is true for any of the values in the range. & SELECT \seqsplit{column\_name(s)} \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE column\_name operator ANY \{\{nl\}\} (SELECT column\_name \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE condition); \tn % Row Count 23 (+ 23) % Row 24 \SetRowColor{white} {\bf{ALL}} \{\{rowspan=3\}\} & Returns:\{\{nl\}\} {\emph{ 1. A boolean value as a result \{\{nl\}\} 2. Returns TRUE if ALL of the subquery values meet the condition \{\{nl\}\} 3. Is used with SELECT, WHERE and HAVING statements.}} \{\{nl\}\} ALL means that the condition will be true only if the operation is true for all values in the range. & \tn % Row Count 44 (+ 21) \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 25 \SetRowColor{LightBackground} & {\emph{ALL Syntax With SELECT}} & SELECT ALL \seqsplit{column\_name(s)} \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE condition; \tn % Row Count 6 (+ 6) % Row 26 \SetRowColor{white} & {\emph{ALL Syntax With WHERE or HAVING}} & SELECT \seqsplit{column\_name(s)} \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE column\_name operator ALL \{\{nl\}\} (SELECT column\_name \{\{nl\}\} FROM table\_name \{\{nl\}\} WHERE condition); \tn % Row Count 18 (+ 12) % Row 27 \SetRowColor{LightBackground} {\bf{INSERT INTO SELECT}} & Copies data from one table and Inserts it into another table. Requires that the data types in source and target tables matches.The existing records in the target table are unaffected. & \tn % Row Count 32 (+ 14) \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 28 \SetRowColor{LightBackground} & {\emph{Copy all columns from one table to another table}} & INSERT INTO table2 \{\{nl\}\} SELECT * FROM table1 \{\{nl\}\} WHERE condition; \tn % Row Count 6 (+ 6) % Row 29 \SetRowColor{white} & {\emph{Copy only some columns from one table into another table}} & INSERT INTO table2 (col1, col2 ...) \{\{nl\}\} SELECT col1, col2 ... \{\{nl\}\} FROM table1 \{\{nl\}\} WHERE condition; \tn % Row Count 14 (+ 8) % Row 30 \SetRowColor{LightBackground} {\bf{CASE}} & 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. & CASE \{\{nl\}\} ~ WHEN condition1 THEN result1 \{\{nl\}\} ~ WHEN condition2 THEN result2 \{\{nl\}\} ~ WHEN conditionN THEN resultN \{\{nl\}\} ~ ELSE result \{\{nl\}\} END; \tn % Row Count 37 (+ 23) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \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}{Database}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{CREATE DATABASE}} & Create a new SQL database. & CREATE DATABASE db; \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{SHOW DATABASES}} & Show the list of databases. & SHOW DATABASES; \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{DROP DATABASE}} & Drop a SQL database. & DROP DATABASE db; \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} {\bf{BACKUP DATABASE}} \{\{rowspan=2\}\} & Create a full back up of an existing SQL database. {\emph{'E:\textbackslash{}testDB.bak'}} & BACKUP DATABASE db \{\{nl\}\} TO DISK = 'filepath'; \tn % Row Count 11 (+ 5) % Row 4 \SetRowColor{LightBackground} & {\emph{A differential back up only backs up the parts of the database that have changed since the last full database backup.}} & BACKUP DATABASE db \{\{nl\}\} TO DISK = 'filepath' \{\{nl\}\} WITH DIFFERENTIAL; \tn % Row Count 20 (+ 9) % Row 5 \SetRowColor{white} {\bf{CREATE TABLE}} \{\{rowspan=2\}\} & Create a new table in a database. & CREATE TABLE table ( \{\{nl\}\} ~ column1 datatype, \{\{nl\}\} ~ column2 datatype, \{\{nl\}\} ~ columnN datatype ); \tn % Row Count 29 (+ 9) % Row 6 \SetRowColor{LightBackground} & {\emph{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 new\_table AS \{\{nl\}\} SELECT column1, column2,... \{\{nl\}\} FROM \seqsplit{existing\_table} \{\{nl\}\} WHERE ....; \tn % Row Count 40 (+ 11) \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}{Database (cont)}} \tn % Row 7 \SetRowColor{LightBackground} {\bf{DROP TABLE}} & Drop an existing table in a database. & DROP TABLE table\_name; \tn % Row Count 3 (+ 3) % Row 8 \SetRowColor{white} TRUNCATE TABLE & delete the data inside a table, but not the table itself. & TRUNCATE TABLE table\_name; \tn % Row Count 8 (+ 5) % Row 9 \SetRowColor{LightBackground} ALTER TABLE & is used to add, delete, or modify columns in an existing table. is also used to add and drop various constraints on an existing table. & \tn % Row Count 18 (+ 10) % Row 10 \SetRowColor{white} & ADD Column & ALTER TABLE table\_name ADD column\_name datatype; \tn % Row Count 22 (+ 4) % Row 11 \SetRowColor{LightBackground} & DROP COLUMN & ALTER TABLE table\_name DROP COLUMN column\_name; \tn % Row Count 26 (+ 4) % Row 12 \SetRowColor{white} & RENAME COLUMN & ALTER TABLE table\_name RENAME COLUMN old\_name to new\_name; \tn % Row Count 31 (+ 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}{Database (cont)}} \tn % Row 13 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{Constraints} \tn % Row Count 1 (+ 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}{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 \seqsplit{column\_name(s)} \{\{nl\}\} FROM table1 \{\{nl\}\} INNER JOIN table2 \{\{nl\}\} ON \seqsplit{table1.column\_name} = \{\{nl\}\} \seqsplit{table2.column\_name;} \{\{width=46\}\} \tn % Row Count 10 (+ 10) % 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 19 (+ 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 28 (+ 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 33 (+ 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}