Sql Cheat Sheet Body

2y ago
101 Views
9 Downloads
3.51 MB
12 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Duke Fulford
Transcription

SQLCHEAT SHEETcreated byTomi Mester

I originally created this cheat sheet for my SQL course and workshop participants.*But I have decided to open-source it and make it available for everyone who wantsto learn SQL.It's designed to give you a meaningful structure but also to let you add your ownnotes (that's why the empty boxes are there). It starts from the absolute basics(SELECT * FROM table name;) and guides you to the intermediate level (JOIN,HAVING, subqueries). I added everything that you will need as a data analyst/scientist.The ideal use case of this cheat sheet is that you print it in color and keep it next toyou while you are learning and practicing SQL on your computer.Enjoy!Cheers,Tomi Mester*The workshops and courses I mentioned:Online SQL tutorial (free): data36.com/sql-tutorialLive SQL workshop: data36.com/sql-workshopPractice SQL - an online SQL course for practicing: data36.com/practice-sql

SQL CHEAT SHEETBASE QUERYSELECT * FROM table name;This query returns every column and every row of the table called table name.SELECT * FROM table name LIMIT 10;It returns every column and the first 10 rows from table name.SELECTING SPECIFIC COLUMNSSELECT column1, column2, column3 FROM table name;This query returns every row of column1, column2 and column3 from table name.[your notes]DATA TYPES IN SQLIn SQL we have more than 40 different data types. But these seven are the mostimportant ones:1. Integer. A whole number without a fractional part. E.g. 1, 156, 20124122. Decimal. A number with a fractional part. E.g. 3.14, 3.141592654, 961.12412503. Boolean. A binary value. It can be either TRUE or FALSE.4. Date. Speaks for itself. You can also choose the format. E.g. 2017-12-315. Time. You can decide the format of this, as well. E.g. 23:59:596. Timestamp. The date and the time together. E.g. 2017-12-31 23:59:597. Text. This is the most general data type. But it can be alphabetical letters only,or a mix of letters and numbers and any other characters. E.g. hello, R2D2,Tomi, 124.56.128.41CREATED BY TOMI MESTER DATA36.COM!1

SQL CHEAT SHEETFILTERING (the WHERE CLAUSE)SELECT * FROM table name WHERE column1 'expression';"Horizontal filtering." This query returns every column from table name - but onlythose rows where the value in column1 is 'expression'. Obviously this can besomething other than text: a number (integer or decimal), date or any other dataformat, too.ADVANCED FILTERINGComparison operators help you compare two values. (Usually a value that youdefine in your query and values that exist in your SQL table.) Mostly, they aremathematical symbols, with a few exceptions:Comparison operatorWhat does it mean? Equal to Not equal to! Not equal to Less than Less than or equal to Greater than Greater than or equal toLIKE ‘%expression%’Contains ‘expression’IN (‘exp1’, ‘exp2’, ‘exp3’)Contains any of ‘exp1’, ‘exp2’, or ‘exp3’CREATED BY TOMI MESTER DATA36.COM!2

SQL CHEAT SHEETA few examples:SELECT * FROM table name WHERE column1 ! 'expression';This query returns every column from table name, but only those rows where thevalue in column1 is NOT 'expression'.SELECT * FROM table name WHERE column2 10;It returns every column from table name, but only those rows where the value incolumn2 is greater or equal to 10.SELECT * FROM table name WHERE column3 LIKE ‘%xzy%’;It returns every column from table name, but only those rows where the value incolumn3 contains the 'xyz' string.MULTIPLE CONDITIONSYou can use more than one condition to filter. For that, we have two logicaloperators: OR, AND.SELECT * FROM table name WHERE column1 ! ‘expression’ AND column3 LIKE‘%xzy%’;This query returns every column from table name, but only those rows where thevalue in column1 is NOT ‘expression’ AND the value in column3 contains the 'xyz'string.SELECT * FROM table name WHERE column1 ! ‘expression’ OR column3 LIKE‘%xzy%’;This query returns every column from table name, but only those rows where thevalue in column1 is NOT ‘expression’ OR the value in column3 contains the 'xyz'string.CREATED BY TOMI MESTER DATA36.COM!3

SQL CHEAT SHEETPROPER FORMATTINGYou can use line breaks and indentations for nicer formatting. It won't have anyeffect on your output. Be careful and put a semicolon at the end of the querythough!SELECT *FROM table nameWHERE column1 ! 'expression'AND column3 LIKE '%xzy%'LIMIT 10;SORTING VALUESSELECT * FROM table name ORDER BY column1;This query returns every row and column from table name, ordered by column1, inascending order (by default).SELECT * FROM table name ORDER BY column1 DESC;This query returns every row and column from table name, ordered by column1, indescending order.UNIQUE VALUESSELECT DISTINCT(column1) FROM table name;It returns every unique value from column1 from table name.CREATED BY TOMI MESTER DATA36.COM!4

SQL CHEAT SHEETCORRECT KEYWORD ORDERSQL is extremely sensitive to keyword order.So make sure you keep it right:1.2.3.4.5.SELECTFROMWHEREORDER BYLIMITSQL FUNCTIONS FOR AGGREGATIONIn SQL, there are five important aggregate functions for data analysts/scientists: COUNT()SUM()AVG()MIN()MAX()A few examples:SELECT COUNT(*) FROM table name WHERE column1 'something';It counts the number of rows in the SQL table in which the value in column1 is'something'.SELECT AVG(column1) FROM table name WHERE column2 1000;It calculates the average (mean) of the values in column1, only including rows inwhich the value in column2 is greater than 1000.CREATED BY TOMI MESTER DATA36.COM!5

SQL CHEAT SHEETSQL GROUP BYThe GROUP BY clause is usually used with an aggregate function (COUNT, SUM,AVG, MIN, MAX). It groups the rows by a given column value (specified afterGROUP BY) then calculates the aggregate for each group and returns that to thescreen.SELECT column1, COUNT(column2) FROM table name GROUP BY column1;This query counts the number of values in column2 - for each group of uniquecolumn1 values.SELECT column1, SUM(column2) FROM table name GROUP BY column1;This query sums the number of values in column2 - for each group of uniquecolumn1 values.SELECT column1, MIN(column2) FROM table name GROUP BY column1;This query finds the minimum value in column2 - for each group of unique column1values.SELECT column1, MAX(column2) FROM table name GROUP BY column1;This query finds the maximum value in column2 - for each group of unique column1values.CREATED BY TOMI MESTER DATA36.COM!6

SQL CHEAT SHEETSQL ALIASESYou can rename columns, tables, subqueries, anything.SELECT column1, COUNT(column2) AS number of values FROM table nameGROUP BY column1;This query counts the number of values in column2 - for each group of uniquecolumn1 values. Then it renames the COUNT(column2) column tonumber of values.SQL JOINYou can JOIN two (or more) SQL tables based on column values.SELECT *FROM table1JOIN table2ON table1.column1 table2.column1;This joins table1 and table2 values - for every row where the value of column1 fromtable1 equals the value of column1 from table2.Detailed explanation here: -ep5/CREATED BY TOMI MESTER DATA36.COM!7

SQL CHEAT SHEETSQL HAVINGThe execution order of the different SQL keywords doesn't allow you to filter withthe WHERE clause on the result of an aggregate function (COUNT, SUM, etc.). Thisis because WHERE is executed before the aggregate functions. But that's whatHAVING is for:SELECT column1, COUNT(column2)FROM table nameGROUP BY column1HAVING COUNT(column2) 100;This query counts the number of values in column2 - for each group of uniquecolumn1 values. It returns only those results where the counted value is greaterthan 100.Detailed explanation and examples here: ial-ep6/CORRECT KEYWORD ORDER AGAINSQL is extremely sensitive to keyword order.So make sure you keep it right:1.2.3.4.5.6.7.8.SELECTFROMJOIN (ON)WHEREGROUP BYHAVINGORDER BYLIMITCREATED BY TOMI MESTER DATA36.COM!8

SQL CHEAT SHEETSUBQUERIESYou can run SQL queries within SQL queries. (Called subqueries.) Even querieswithin queries within queries. The point is to use the result of one query as an inputvalue of another query.Example:SELECT COUNT(*) FROM(SELECT column1, COUNT(column2) AS inner number of valuesFROM table nameGROUP BY column1) AS inner queryWHERE inner number of values 100;The inner query counts the number of values in column2 - for each group of uniquecolumn1 values. Then the outer query uses the inner query's results and counts thenumber of values where inner number of values are greater than 100. (The resultis one number.)Detailed explanation here: ial-ep6/CREATED BY TOMI MESTER DATA36.COM!9

SQL CHEAT SHEETCREATED BYTomi Mester from Data36.comTomi Mester is a data analyst and researcher. He worked for Prezi, iZettle andseveral smaller companies as an analyst/consultant. He’s the author of the Data36blog where he writes posts and tutorials on a weekly basis about data science, ABtesting, online research and data coding. He’s an O’Reilly author and presenter atTEDxYouth, Barcelona E-commerce Summit, Stockholm Analyticsdagarna andmore.WHERE TO GO NEXTFind company workshops, online tutorials and online video courses on my website:https://data36.comSubscribe to my Newsletter list for useful stuff like this:https://data36.com/newsletterOnline SQL tutorial (free): data36.com/sql-tutorialLive SQL workshop: data36.com/sql-workshopPractice SQL - an online SQL course for practicing: data36.com/practice-sqlCREATED BY TOMI MESTER DATA36.COM!10

SQL CHEAT SHEET PROPER FORMATTING You can use line breaks and indentations for nicer formatting. It won't have any effect on your outpu

Related Documents:

Cissp cheat sheet all domains. Cissp cheat sheet 2022 pdf. Cissp cheat sheet 2022. Cissp cheat sheet domain 4. Cissp cheat sheet pdf. Cissp cheat sheet 2021. Cissp cheat sheet domain 1. Cissp cheat sheet reddit. We use cookies to offer you a better browsing experience, analyze site traffic, personalize content, and serve targeted advertisements.

Git-cheat-sheet Optional Reading: Bourbon-cheat-sheet CLI-cheat-sheet Git-for-subversion-cheat-sheet Tower-cheat-sheet (for Mac or Windows depending on your computer) Website_optimization-cheat-sheet Workflow-of-version-control Xcode-cheat-sheet _tower-git-client (

Google Slides Cheat Sheet p. 15-18 Google Sheets Cheat Sheet p. 19-22 Google Drawings Cheat Sheet p. 23-26 Google Drive for iOS Cheat Sheet p. 27-29 Google Chrome Cheat Sheet p. 30-32 ShakeUpLearning.com Google Cheat Sheets - By Kasey Bell 3

The Excel 2010 Ribbon looks and works much the same as the Excel 2007 Ribbon, with one nifty addition: In Excel 2010, you can customize what's on the Ribbon. In this series Word 2010 cheat sheet Excel 2010 cheat sheet Outlook 2010 cheat sheet PowerPoint 2010 cheat sheet The Scrollbar.

SQL Server supports ANSI SQL, which is the standard SQL (Structured Query Language) language. However, SQL Server comes with its own implementation of the SQL language, T-SQL (Transact- SQL). T-SQL is a Microsoft propriety Language known as Transact-SQL. It provides further capab

MS SQL Server: MS SQL Server 2017, MS SQL Server 2016, MS SQL Server 2014, MS SQL Server 2012, MS SQL Server 2008 R2, 2008, 2008 (64 bit), 2008 Express, MS SQL Server 2005, 2005 (64 bit), 2005 Express, MS SQL Server 2000, 2000 (64 bit), 7.0 and mixed formats. To install the software, follow the steps: 1. Double-click Stellar Repair for MS SQL.exe.

Server 2005 , SQL Server 2008 , SQL Server 2008 R2 , SQL Server 2012 , SQL Server 2014 , SQL Server 2005 Express Edition , SQL Server 2008 Express SQL Server 2008 R2 Express , SQL Server 2012 Express , SQL Server 2014 Express .NET Framework 4.0, .NET Framework 2.0,

70 Microsoft SQL Server 2008: A Beginner’s Guide SQL_2008 / Microsoft SQL Server 2008: ABG / Petkovic / 154638-3 / Chapter 4 In Transact-SQL, the use of double quotation marks is defined using the QUOTED_ IDENTIFIER option of the SET statement. If this option is set to ON, which is theFile Size: 387KBPage Count: 26Explore furtherLanguage Elements (Transact-SQL) - SQL Server Microsoft Docsdocs.microsoft.comThe 33 languages of SQL Server Joe Webb Blogweblogs.sqlteam.comThe Language of SQL Pdf - libribooklibribook.comSql And The Standard Language For Relational Database .www.bartleby.comdatabase - What are good alternatives to SQL (the language .stackoverflow.comRecommended to you based on what's popular Feedback