\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{sjm} \pdfinfo{ /Title (sql-commands.pdf) /Creator (Cheatography) /Author (sjm) /Subject (SQL commands 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}{6CC3F5} \definecolor{LightBackground}{HTML}{ECF7FD} \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 commands Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{sjm} via \textcolor{DarkBackground}{\uline{cheatography.com/62460/cs/16026/}}} \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}sjm \\ \uline{cheatography.com/sjm} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 24th July, 2019.\\ Updated 24th July, 2019.\\ 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.72 cm} x{5.28 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Terminology - Basic Manipulation}} \tn % Row 0 \SetRowColor{LightBackground} {\emph{SQL}} & A programming language designed to manipulate \& manage data stored in relational databases \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} {\emph{relational database}} & A database that organizes information into one or more tables. \tn % Row Count 7 (+ 3) % Row 2 \SetRowColor{LightBackground} {\emph{table}} & A collection of data organized into rows \& columns. \tn % Row Count 9 (+ 2) % Row 3 \SetRowColor{white} {\emph{statement}} & A string of characters that the database recognizes as a valid command. \tn % Row Count 12 (+ 3) % Row 4 \SetRowColor{LightBackground} {\emph{primary key}} & Column in table that is unique to each row w/ no NULL values. \tn % Row Count 15 (+ 3) % Row 5 \SetRowColor{white} {\emph{foreign key}} & Primary key of table1 that appears in table2. \tn % Row Count 17 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.36 cm} x{4.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Commands - Basic Manipulation}} \tn % Row 0 \SetRowColor{LightBackground} `SHOW DATABASES` & list all available databases \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `USE` database & use specified database \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} `SHOW TABLES` {[}FROM database{]} & list tables in database \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} `DESCRIBE` table & list column headers in table \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} `SHOW FIELDS FROM` table & list all fields \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} `SHOW COLUMNS FROM` table & list all columns (fields) + column type etc \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} `SHOW COLUMNS FROM` table & list all columns (fields) + column type etc \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} `SHOW INDEX FROM` table & list all indexes from table \tn % Row Count 15 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.76 cm} x{6.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Terminology - queries}} \tn % Row 0 \SetRowColor{LightBackground} {\emph{operators}} & Operators create a condition that can be evaluated as either {\emph{true}} or {\emph{false}}. \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.8 cm} x{5.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Commands - operators}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{=}} & equal to \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{!=}} & not equal to \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} {\bf{\textgreater{}}} & greater than \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} {\bf{\textless{}}} & less than \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} {\bf{\textgreater{}=}} & greater than or equal to \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} {\bf{\textless{}=}} & less than or equal to \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} `IS NULL` & is null \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} `IS NOT NULL` & is not null \tn % Row Count 8 (+ 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}{Wildcards}} \tn % Row 0 \SetRowColor{LightBackground} * & Matches any number or type of character(s). \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \_ & Matches any individual character. \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \% & Matches zero or more missing letters in the pattern. \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{0.836 cm} x{3.42 cm} x{3.344 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Commands - queries}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{`SELECT`} & Identify columns to return in query. & SELECT column FROM table; \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `AS` & Renames a column or table using an alias. & SELECT column AS 'alias' FROM table; \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \seqsplit{`DISTINCT`} & Used to return unique values in the output. Filters out all duplicate values in the specified column(s). & SELECT DISTINCT column FROM table; \tn % Row Count 11 (+ 6) % Row 3 \SetRowColor{white} \seqsplit{`LIKE`} & Operator used with WHERE clause to search for a specific pattern in a column. & WHERE column LIKE 'text'; (or NOT LIKE) \tn % Row Count 16 (+ 5) % Row 4 \SetRowColor{LightBackground} `AND` & Operator used to combine multiple conditions in a WHERE clause; ALL must be true. & WHERE column condition1 AND column condition2; \tn % Row Count 21 (+ 5) % Row 5 \SetRowColor{white} `OR` & Operator used to combine multiple conditions in a WHERE clause; ANY must be true. & WHERE column condition1 OR column condition2; \tn % Row Count 26 (+ 5) % Row 6 \SetRowColor{LightBackground} \seqsplit{`BETWEEN`} & Operator used in a WHERE clause to filter the result set within a certain range (numbers, text, or dates). & WHERE column BETWEEN 'A' AND 'B'; \tn % Row Count 32 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\emph{ `BETWEEN` two letters }}is not{\emph{ inclusive of the 2nd letter. \newline }} `BETWEEN` two numbers {\emph{is}} inclusive of the 2nd number.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.72 cm} x{5.28 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Terminology - Aggregate Functions}} \tn % Row 0 \SetRowColor{LightBackground} {\emph{aggregates}} & Calculations performed on multiple rows of a table. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\emph{aggregate functions}} & Combine multiple rows together to form a single value of more meaningful information. \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} {\emph{clause}} & A clause is used with aggregate functions; used in collaboration with the SELECT statement. \tn % Row Count 10 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.296 cm} x{2.232 cm} x{2.952 cm} p{0.72 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Commands - Aggregate Functions}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{`COUNT()`} & Count the number of rows & `SELECT COUNT(`column`) FROM `table`;` & \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{`SUM()`} & The sum of the values in a column & `SELECT SUM(`column`) FROM `table`;` & \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \seqsplit{`MAX()`} / \seqsplit{`MIN()`} & The \seqsplit{largest/smallest} value in a column & `SELECT MAX(`column`) FROM `table`;` & \tn % Row Count 10 (+ 4) % Row 3 \SetRowColor{white} \seqsplit{`AVG()`} & The average (mean) of the values in a column & `SELECT AVG(`column`) FROM `table`;` & \tn % Row Count 14 (+ 4) % Row 4 \SetRowColor{LightBackground} \seqsplit{`ROUND()`} & Round the values in a column & `SELECT ROUND(`column, integer`) FROM `table`;` & \tn % Row Count 17 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.72 cm} x{0.936 cm} x{3.024 cm} x{2.52 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Clauses}} \tn % Row 0 \SetRowColor{LightBackground} 1. & \seqsplit{`WHERE`} & Restrict the results of a query based on values of individual rows within a column. & \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} 2. & \seqsplit{`GROUP} BY` & A clause used with aggregate functions to combine data from one or more columns. Arrange identical data into groups. & \tn % Row Count 14 (+ 8) % Row 2 \SetRowColor{LightBackground} 3. & \seqsplit{`HAVING`} & Limit the results of a query based on an aggregate property. & \tn % Row Count 18 (+ 4) % Row 3 \SetRowColor{white} 4. & \seqsplit{`ORDER} BY` & Sort results by column. & `ORDER BY` column `ASC/DESC` \tn % Row Count 20 (+ 2) % Row 4 \SetRowColor{LightBackground} 5. & \seqsplit{`LIMIT`} & Maximum number of rows to return. & \tn % Row Count 23 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}----} \SetRowColor{LightBackground} \mymulticolumn{4}{x{8.4cm}}{ie. \newline `SELECT column, AGG(column)` \newline `FROM table` \newline `{\bf{CLAUSE}} column;` \newline \newline Clauses can refer to a column name, or to a column reference number (assigned by order column referred to in statement).} \tn \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{If-then - CASE}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`{\bf{SELECT}}` columns, \newline ~ `{\bf{CASE}}` \newline ~~~ `{\bf{WHEN}}` column condition1 `{\bf{THEN}}` action1 \newline ~~~ `{\bf{WHEN}}` column condition2 `{\bf{THEN}}` action2 \newline ~~~ `{\bf{ELSE}}` action3 \newline ~ `{\bf{END}}` `{\bf{AS}}` 'renamed\_column' \newline `{\bf{FROM}}` table;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.748 cm} x{2.964 cm} x{2.888 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Combining tables - JOIN}} \tn % Row 0 \SetRowColor{LightBackground} `JOIN` {\emph{(inner join)}} & combine rows from different tables if the join condition is true; drops unmatched rows & \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} `LEFT JOIN / RIGHT JOIN` & return every row in the {\emph{left/right}} table; if join condition not met, `NULL` values used to fill in columns from the {\emph{right/left}} table & \tn % Row Count 16 (+ 10) % Row 2 \SetRowColor{LightBackground} `OUTER JOIN` & return unmatched rows from {\emph{both}} tables; unmatched fields filled with `NULL` & \tn % Row Count 22 (+ 6) % Row 3 \SetRowColor{white} `CROSS JOIN` & combine all rows of 1 table with all rows of another table; does NOT require joining on a specific column & \tn % Row Count 29 (+ 7) % Row 4 \SetRowColor{LightBackground} `UNION` & stacks 1 dataset on top of another; tables must have same \# columns \& same data types/order columns & `SELECT * FROM `table1` UNION SELECT * FROM` table2; \tn % Row Count 36 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{`{\bf{SELECT}} {\emph{` \newline `{\bf{FROM}}` table1 \newline `{\bf{JOIN}}` table2 \newline ~~`{\bf{ON}}` table1.id = table2.id; \newline \newline ie. \newline `{\bf{SELECT}}` table1.column1, \newline ~~`{\bf{COUNT(*) AS}}` }}renamed\_output* \newline ~~`{\bf{FROM}}` table1 \newline `{\bf{CROSS JOIN}}` table2 \newline `{\bf{WHERE}}` table2.column1 `{\bf{\textless{}=}}` table1.column1 \newline ~~`{\bf{AND}}` table2.column2 `{\bf{\textgreater{}=}}` table1.column1 \newline `{\bf{GROUP BY}}` table1.column1;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Combining tables - WITH statements}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{FYI! MySQL prior to version 8.0 doesn't support the WITH clause.} \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`{\bf{WITH}}` {\emph{previousQueryAlias}} `{\bf{AS (}}` \newline ~~`{\bf{SELECT}}` column1, \newline ~~`{\bf{COUNT(}}`column2`{\bf{) AS}}` {\emph{renamedOutputColumn}} \newline ~~`{\bf{FROM}}` table1 \newline ~~`{\bf{GROUP BY}}` column1 \newline ) \newline `{\bf{SELECT}}` table2.column1, \newline ~~{\emph{previousQueryAlias.renamedOutputColumn}} \newline `{\bf{FROM}}` {\emph{previousQueryAlias}} \newline `{\bf{JOIN}}` table2 \newline ~~`{\bf{ON}}` table2.column1 = \seqsplit{previousQueryAlias.column1;}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.96 cm} x{3.04 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Commands - String Functions}} \tn % Row 0 \SetRowColor{LightBackground} `STRCMP`("string1","string2") & compare strings \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `LOWER`("string") & convert to lower case \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `UPPER`("string") & convert to upper case \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{`LTRIM`/`RTRIM`("string")} & left or right trim \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `SUBSTRING`("string","inx1","inx2") & substring of a string \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} `CONCAT`("string1","string2") & concatenate \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}