\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{mckoy} \pdfinfo{ /Title (sql-ei20.pdf) /Creator (Cheatography) /Author (mckoy) /Subject (SQL (EI20) 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}{3354A3} \definecolor{LightBackground}{HTML}{F2F4F9} \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 (EI20) Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{mckoy} via \textcolor{DarkBackground}{\uline{cheatography.com/59730/cs/15642/}}} \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}mckoy \\ \uline{cheatography.com/mckoy} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 30th April, 2018.\\ Updated 30th April, 2018.\\ 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{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{CREATE A TABLE}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{CREATE TABLE restaurants ( \newline id serial PRIMARY KEY, \newline name text NOT NULL, \newline nyc\_restaurant\_id integer, \newline borough borough\_options, \newline cuisine text, \newline \seqsplit{address\_building\_number} text, \newline address\_street text, \newline address\_zipcode text \newline );} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{INSERT A ROW}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{INSERT INTO restaurants \newline (name, borough, cuisine, \seqsplit{address\_building\_number}, address\_street, address\_zipcode) \newline VALUES \newline ('Prince Taco', 'Queens', 'Mexican', '620', 'Astoria Boulevard', '11372');} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{ENUMERATIONS}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy'); \newline \newline CREATE TABLE person ( \newline name text, \newline current\_mood mood \newline ); \newline \newline INSERT INTO person VALUES ('Moe', 'happy'); \newline INSERT INTO person VALUES ('Larry', 'sad'); \newline INSERT INTO person VALUES ('Curly', 'ok');} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{`SELECT {\emph{ FROM person WHERE current\_mood = 'happy';` \newline {\bf{name | current\_mood }} \newline -{}-{}-{}--+-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline Moe | happy \newline \newline `SELECT }} FROM person WHERE current\_mood \textgreater{} 'sad';` \newline {\bf{ name | current\_mood}} \newline -{}-{}-{}--+-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline Moe | happy \newline Curly | ok} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{EXAMPLE QUERYING}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{SELECT * from restaurants; \newline \newline SELECT id, name, borough, cuisine \newline FROM restaurants; \newline \newline SELECT DISTINCT borough \newline FROM restaurants; \newline \newline SELECT id, name, borough, cuisine \newline FROM restaurants \newline WHERE id='225'; \newline \newline SELECT name, borough, cuisine FROM restaurants \newline WHERE borough = 'Brooklyn' \newline AND cuisine = 'Italian'; \newline \newline SELECT name, borough, cuisine FROM restaurants \newline WHERE borough = 'Brooklyn' \newline AND cuisine in ('Italian', 'Chinese');} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{UPDATING}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{UPDATE restaurants \newline SET name = 'Famous Original Ray''s Pizza' \newline WHERE id = 5269; \newline \newline UPDATE restaurants \newline SET cuisine = 'Vegetarian' \newline WHERE cuisine = 'Pizza';} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{ORDERING AND LIMITING RESULTS}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{SELECT id, name from restaurants \newline WHERE borough = 'Bronx' \newline AND cuisine = 'Japanese' \newline ORDER BY name DESC; \newline \newline SELECT id, name \newline FROM restaurants \newline ORDER BY name DESC; \newline LIMIT 3;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{AGGREGATE: COUNT, MAX, MIN, \& AVG}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{SELECT count(*) FROM grades; \newline \newline SELECT max(score) FROM grades; \newline \newline SELECT min(score) FROM grades; \newline \newline SELECT avg(score) FROM grades;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{DELETING}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{DELETE FROM restaurants WHERE id = 225; \newline \newline DELETE FROM grades WHERE grade = 'Z'; \newline \newline DELETE FROM grades; {\bf{-{}- BEWARE: DELETES ALL ROWS IN GRADES!}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{DROP A TABLE}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{DROP TABLE restaurants;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}