\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{thecodereaper27} \pdfinfo{ /Title (transact-sql.pdf) /Creator (Cheatography) /Author (thecodereaper27) /Subject (Transact-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}{00AFF0} \definecolor{LightBackground}{HTML}{EFFAFE} \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{Transact-SQL Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{thecodereaper27} via \textcolor{DarkBackground}{\uline{cheatography.com/66567/cs/16616/}}} \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}thecodereaper27 \\ \uline{cheatography.com/thecodereaper27} \\ \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 13th August, 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{8.2896 cm} x{8.9804 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Overview}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Transact-SQL is the language used to query data in Microsoft SQL Server and Azure SQL Database.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{SQL is declarative, not procedural.} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Data is stored in tables, which may be related to one another through common key fields ({\emph{primary}} and {\emph{foreign}}).} \tn % Row Count 6 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{Schemas are namespaces for database objects.} \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} Fully-qualified name for an object & `{[}server\_name{]}.{[}database\_name{]}.{[}schema\_name{]}.object\_name` \tn % Row Count 10 (+ 3) % Row 5 \SetRowColor{white} Best practice within database context & ` \seqsplit{schema\_name.object\_name`} \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Always terminate a SQL statement with a semi-colon, `;`.} \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{Relational Databases} \tn % Row Count 15 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{p{1.647 cm} x{4.2822 cm} x{5.2704 cm} x{5.2704 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{SQL Statement Types}} \tn % Row 0 \SetRowColor{LightBackground} DML & Data \seqsplit{Manipulation} Language & Statements for querying and modifying data. & `SELECT, INSERT, UPDATE, DELETE` \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} DDL & Data \seqsplit{Definition} Language & Statements for defining database objects. & `CREATE, ALTER, DROP` \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} DCL & Data Control Language & Statements for assigning \seqsplit{permissions.} & `GRANT, REVOKE, DENY` \tn % Row Count 12 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NULL}} \tn % Row 0 \SetRowColor{LightBackground} `NULL` & NULL is used to indicate an unknown or missing value. & NULL is not equivalent to zero or an empty string. Arithmetic or string \seqsplit{concatenation} operations involving one or more NULL operands return NULL. For example, 12 + NULL = NULL. \tn % Row Count 14 (+ 14) % Row 1 \SetRowColor{white} \seqsplit{`ISNULL(column/variable}, value)` & Return {\emph{value}} if the column or variable is `NULL`. & If you need to compare a value to NULL, use the IS operator instead of the = operator. \tn % Row Count 21 (+ 7) % Row 2 \SetRowColor{LightBackground} \seqsplit{`NULLIF(column/variable}, value)` & Returns `NULL` if the column or variable is {\emph{value}}. & The NULLIF function returns NULL when a column or variable contains a specified value. \tn % Row Count 28 (+ 7) % Row 3 \SetRowColor{white} \seqsplit{`COALESCE(column/variable1}, \seqsplit{column/variable2}, ...)` & Returns the {\emph{value}} of the first non-`NULL` column or variable in the list. & The COALESCE function returns the first non-NULL value in a specified list of columns or variables. \tn % Row Count 36 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{2.6992 cm} x{5.061 cm} x{9.1098 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{`SELECT` Statement}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{{\bf{Main query clauses in {\emph{keyed-in order}}}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \seqsplit{`SELECT`} & \textless{}select list\textgreater{} & Defines which columns to return. \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{`FROM`} & \textless{}table source\textgreater{} & Defines table(s) to query. \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{`WHERE`} & \textless{}search condition\textgreater{} & Filters rows using a predicate. \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{`GROUP} BY` & \textless{}group by list\textgreater{} & Arranges rows by groups. \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{`HAVING`} & \textless{}search condition\textgreater{} & Filters groups using a predicate. \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{`ORDER} BY` & \textless{}order by list\textgreater{} & Sorts the output. \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{3}{x{17.67cm}}{{\bf{Logical query processing order}}} \tn % Row Count 14 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{`FROM`} \tn % Row Count 15 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{3}{x{17.67cm}}{`WHERE`} \tn % Row Count 16 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{`GROUP BY`} \tn % Row Count 17 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{3}{x{17.67cm}}{`HAVING`} \tn % Row Count 18 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{`SELECT`} \tn % Row Count 19 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{3}{x{17.67cm}}{`ORDER BY`} \tn % Row Count 20 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{{\bf{Notes}}} \tn % Row Count 21 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{3}{x{17.67cm}}{Use the `SELECT` statement to retrieve a rowset of data from tables and views in a database.} \tn % Row Count 23 (+ 2) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{In the `SELECT` clause, you can use `*` to return all columns, but generally you should specify explicit columns.} \tn % Row Count 26 (+ 3) % Row 17 \SetRowColor{white} \mymulticolumn{3}{x{17.67cm}}{You can specify expressions in the `SELECT` clause to return the results of calculations.} \tn % Row Count 28 (+ 2) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{You can use the `AS` keyword to specify aliases for columns in the rowset returned by the `SELECT` statement.} \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{2.6992 cm} x{5.061 cm} x{9.1098 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{`SELECT` Statement (cont)}} \tn % Row 19 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{By default, the SELECT statement returns all rows. If mulitple rows contain the same values for every column, they are duplicated in the results. Using the DISTINCT keyword eliminates duplicates, ensuring that only one row for each distinct combination of column values is returned.} \tn % Row Count 6 (+ 6) % Row 20 \SetRowColor{white} \mymulticolumn{3}{x{17.67cm}}{The order of rows in the result of a SELECT statement is not guaranteed unless you explicitly specify one or more columns in an ORDER BY clause. You can specify sort direction as ASC (the default) or DESC.} \tn % Row Count 11 (+ 5) % Row 21 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{You can combine the ORDER BY clause with the TOP keyword to retrict the results so that they include only the top n rows (where n is the number or percentage of rows you want to return).} \tn % Row Count 15 (+ 4) % Row 22 \SetRowColor{white} \mymulticolumn{3}{x{17.67cm}}{You can implement a query to retrieve a specified "page" of results by using the OFFSET and FETCH keywords with the ORDER BY clause.} \tn % Row Count 18 (+ 3) % Row 23 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{Use the WHERE clause to filter the results returned by a SELECT query based on a search condition.} \tn % Row Count 20 (+ 2) % Row 24 \SetRowColor{white} \mymulticolumn{3}{x{17.67cm}}{A search condition is composed of one or more predicates.} \tn % Row Count 22 (+ 2) % Row 25 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{Predicates include conditional operators (such as =, \textgreater{}, and \textless{}), IN, LIKE, and NOT.} \tn % Row Count 24 (+ 2) % Row 26 \SetRowColor{white} \mymulticolumn{3}{x{17.67cm}}{You can use AND and OR to combine predicates based on Boolean logic.} \tn % Row Count 26 (+ 2) % Row 27 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{{\bf{Example}}} \tn % Row Count 27 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{SELECT country, YEAR(hiredate) AS yearhired, COUNT({\emph{) AS numemployees \newline FROM HR.Employees \newline WHERE hiredate \textgreater{}= '20140101' \newline GROUP BY country, YEAR(hiredate) \newline HAVING COUNT(}}) \textgreater{} 1 \newline ORDER BY country, yearhired DESC;} \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}{Data Types}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Transact-SQL supports a wide range of data types, which can be broadly categorized as exact numeric, approximate numeric, character, date/time, binary, and other (which includes specialized data types for handling data such as XML and spatial data).} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Some data types are compatible, and values can be implicitly converted between them. Conversion between other data types requires the use of explicit conversion functions.} \tn % Row Count 9 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}