SQL Cheat Sheet — SQL Reference Guide for Data Analysis – Dataquest (2024)

Whether you’re learning SQL through one of our interactive SQL courses or by some other means, it can be really helpful to have a SQL cheat sheet.

Bookmark this article, or download and print the PDF, and keep it handy for quick reference the next time you’re writing an SQL query!

Our SQL cheat sheet goes a bit more in-depth than this handwritten one!

Need to brush up on your SQL before you're ready for the cheat sheet? Check out our interactive online SQL Fundamentals course, read about why you should learn SQL, or do some research about SQL certifications and whether you'll need one.

SQL Basics

SQL stands for Structured Query Language. It is a system for querying — requesting, filtering, and outputting — data from relational databases.

Developed in the 1970s, SQL was originally called SEQUEL. For this reason, today it is sometimes pronounced “Sequel” and sometimes pronounced “S.Q.L.” Either pronunciation is acceptable

Although there are many “flavors” of SQL, SQL in some form can be used for querying data from most relational database systems, including MySQL, SQLite, Oracle, Microsoft SQL Server, PostgreSQL, IBM DB2, Microsoft Azure SQL Database, Apache Hive, etc. databases.

SQL Cheat Sheet: Fundamentals

Performing calculations with SQL

Performing a single calculation:
SELECT 1320+17;

Performing multiple calculations:
SELECT 1320+17, 1340-3, 7*191, 8022/6;

Performing calculations with multiple numbers:
SELECT 1*2*3, 1+2+3;

Renaming results:
SELECT 2*3 AS mult, 1+2+3 AS nice_sum;

Selecting tables, columns, and rows:

Remember: The order of clauses matters in SQL. SQL uses the following order of precedence: FROM, SELECT, LIMIT.

Display the whole table:

SELECT * FROM table_name;

Select specific columns from a table:

SELECT column_name_1, column_name_2 FROM table_name;

Display the first 10 rows on a table:

SELECT * FROM table_name LIMIT 10;

Adding comments to your SQL queries

Adding single-line comments:

-- First commentSELECT column_1, column_2, column_3 -- Second comment FROM table_name; -- Third comment

Adding block comments:

/*This commentspans overmultiple lines */SELECT column_1, column_2, column_3 FROM table_name;

SQL Intermediate: Joins & Complex Queries

Many of these examples use table and column names from the real SQL databases that learners work with in our interactive SQL courses. For more information, sign up for a free account and try one out!

Joining data in SQL:

Joining tables with INNER JOIN:

SELECT column_name_1, column_name_2 FROM table_name_1INNER JOIN table_name_2 ON table_name_1.column_name_1 = table_name_2.column_name_1;

Joining tables using a LEFT JOIN:

SELECT * FROM factsLEFT JOIN cities ON cities.facts_id = facts.id;

Joining tables using a RIGHT JOIN:

SELECT f.name country, c.name cityFROM cities cRIGHT JOIN facts f ON f.id = c.facts;

Joining tables using a FULL OUTER JOIN:

SELECT f.name country, c.name cityFROM cities cFULL OUTER JOIN facts f ON f.id = c.facts_id;

Sorting a column without specifying a column name:

SELECT name, migration_rate FROM FACTSORDER BY 2 desc; -- 2 refers to migration_rate column

Using a join within a subquery, with a limit:

SELECT c.name capital_city, f.name countryFROM facts fINNER JOIN ( SELECT * FROM cities WHERE capital = 1 ) c ON c.facts_id = f.idLIMIT 10;

Joining data from more than two tables:

SELECT [column_names] FROM [table_name_one] [join_type] JOIN [table_name_two] ON [join_constraint] [join_type] JOIN [table_name_three] ON [join_constraint] ... ... ... [join_type] JOIN [table_name_three] ON [join_constraint]

Other common SQL operations:

Combining columns into a single column:

SELECT album_id, artist_id, "album id is " || album_id col_1, "artist id is " || artist_id col2, album_id || artist_id col3FROM album LIMIT 3;

Matching part of a string:

SELECT first_name, last_name, phoneFROM customerWHERE first_name LIKE "%Jen%";

Using if/then logic in SQL with CASE:

CASE WHEN [comparison_1] THEN [value_1] WHEN [comparison_2] THEN [value_2] ELSE [value_3] ENDAS [new_column_name]

Using the WITH clause:

WITH track_info AS( SELECT t.name, ar.name artist, al.title album_name, FROM track t INNER JOIN album al ON al.album_id = t.album_id INNER JOIN artist ar ON ar.artist_id = al.artist_id)SELECT * FROM track_infoWHERE album_name = "Jagged Little Pill";

Creating a view:

CREATE VIEW chinook.customer_2 ASSELECT * FROM chinook.customer;

Dropping a view:

DROP VIEW chinook.customer_2;

Selecting rows that occur in one or more SELECT statements:

[select_statement_one]UNION[select_statement_two];

Selecting rows that occur in both SELECT statements:

SELECT * from customer_usaINTERSECTSELECT * from customer_gt_90_dollars;

Selecting rows that occur in the first SELECT statement but not the second SELECT statement:

SELECT * from customer_usaEXCEPTSELECT * from customer_gt_90_dollars;

Chaining WITH statements:

WITHusa AS ( SELECT * FROM customer WHERE country = "USA" ),last_name_g AS ( SELECT * FROM usa WHERE last_name LIKE " ),state_ca AS ( SELECT * FROM last_name_g WHERE state = "CA" )SELECT first_name, last_name, country, stateFROM state_ca

Important Concepts and Resources:

Reserved words

Reserved words are words that cannot be used as identifiers (such as variable names or function names) in a programming language, because they have a specific meaning in the language itself. Here is a list of reserved words in SQL.

https://youtu.be/JFlukJudHrk

Download the SQL Cheat Sheet PDF

Click on the button below to download the cheat sheet (PDF, 3 MB, color).

Download the SQL Cheat Sheet

Looking for more than just a quick reference? Dataquest's interactive SQL courses will help you get hands-on with SQL as you learn to build the complex queries you'll need to write for real-world data work.

Click the button below to sign up for a free account and start learning right now!

Cheat Sheets

SQL Cheat Sheet — SQL Reference Guide for Data Analysis – Dataquest (2024)

FAQs

How to practice SQL for data analysis? ›

  1. 10 Best Practices for Writing SQL Queries for Data Analysis. Rajanikant Vellaturi. ...
  2. Use Meaningful Alias Names. ...
  3. Avoid Using SELECT * ...
  4. Use Proper Indentation and Formatting. ...
  5. Avoid Subqueries When Possible. ...
  6. Use Indexes, Partitioning/Bucketing for Performance. ...
  7. Use Aggregate Functions. ...
  8. Handle NULL Values.
Dec 11, 2023

How long does it take to learn SQL for data analysis? ›

Because SQL is a relatively simple language, learners can expect to become familiar with the basics within two to three weeks. That said, if you're planning on using SQL skills at work, you'll probably need a higher level of fluency. How quickly you achieve mastery will depend on your method of learning.

Which SQL to learn for data analysis? ›

PostgreSQL, Microsoft SQL Server, MySQL, SQLite, and IBM Db2 are some of the top SQL databases used in data science. They each offer unique features and are compatible with various programming languages.

What is the basic of SQL? ›

SQL is used to create a database, define its structure, implement it, and perform various functions on the database. SQL is also used for accessing, maintaining, and manipulating already created databases. SQL is a well built language for entering data, modifying data, and extracting data in a database.

How can I practice SQL by myself? ›

Best Platforms to Practice SQL Online
  1. HackerRank. HackerRank offers a wide range of coding challenges, including SQL. ...
  2. SQLPad. ...
  3. StrataScratch. ...
  4. LeetCode. ...
  5. Mode. ...
  6. SQLZoo. ...
  7. W3Schools. ...
  8. DataCamp.
Jan 18, 2024

Is Python and SQL enough for data analysis? ›

In today's tech-driven world, SQL and Python are two of the most widely used data analysis and manipulation languages. As companies rely more and more on data for their decisions, it is essential to recognise the distinctions between SQL and Python as well as which language will best suit your requirements.

Is SQL harder than Python? ›

Is SQL easier than Python? SQL features a simpler, more beginner-friendly syntax centered exclusively around databases, which proves easier to learn over Python's general-purpose nature spanning complex programming concepts.

How much SQL knowledge is required for a data analyst? ›

The first 70% of SQL is pretty straightforward, the remaining 30% can be pretty tricky. Data analyst and data scientist interview questions at technology companies often pull from that 30%.

What is the best SQL course for beginners? ›

The Best 6 SQL Courses as of 2024
RankCourse TitleRating
1PostgreSQL for Everybody4.8
2SQL Fundamentals4.8
3The Ultimate MySQL Bootcamp: Go from SQL Beginner to Expert4.5
4Complete SQL Mastery4.9
2 more rows

Which SQL version is best for data analysis? ›

PostgreSQL is best suited for advanced analytics and business intelligence workloads, MySQL is best for small to medium-sized web-based applications, and MS SQL is best for enterprise environments and applications that require high levels of availability and scalability.

Is SQL and Excel enough for data analyst? ›

While Excel skills can be valuable for data analysts, especially for tasks involving data manipulation, visualization, and basic analysis, it is possible to be a good data analyst without extensive knowledge of Excel.

Should I learn SQL or MySQL for data analyst? ›

Data Analysts also need SQL knowledge to understand data available in Relational Databases like Oracle, Microsoft SQL, and MySQL. It is essential to learn SQL for Data Preparation and Wrangling. For instance, if Analysts need to use Big Data Tools for analysis, then SQL is the language they must know.

How difficult is SQL for beginners? ›

Learning SQL is easy

However, there are more advanced SQL commands you'll need to learn to reach a high level of mastery. The trick is to learn how to arrange your SQL statements to perform the actions you want. To learn SQL faster and develop your skills, consistently practice using SQL commands to manipulate data.

Which SQL to use for beginners? ›

If you are just starting to learn SQL and want to get some hands-on experience, MySQL or PostgreSQL might be good choices since they are free and easy to set up. If you are interested in pursuing a career in a specific industry, it might be worth researching which RDBMS are commonly used in that field.

How to write SQL queries for beginners? ›

How to Create a SQL Statement
  1. Start your query with the select statement. select [all | distinct] ...
  2. Add field names you want to display. field1 [,field2, 3, 4, etc.] ...
  3. Add your statement clause(s) or selection criteria. Required: ...
  4. Review your select statement. Here's a sample statement:
Oct 13, 2022

How is SQL used for data analysis? ›

It excels in data integration, seamlessly combining data from various sources or tables to enable comprehensive analysis. Additionally, SQL empowers analysts with a wide range of mathematical and statistical functions, such as SUM, AVG, MAX, and MIN, to derive insights and compute essential metrics within datasets.

How to prepare SQL for data science? ›

We have studied industry trends and listed the most important skills you need to learn in SQL for Data Science.
  1. Relational Database Model.
  2. SQL Query Commands.
  3. Handling Null Values.
  4. Joins.
  5. Key Constraints.
  6. Working with SubQuery.
  7. Creating Tables and Databases.
May 26, 2024

How to prepare data for SQL? ›

Data Cleaning and Preparation With SQL
  1. Checking for duplicate data and removing them.
  2. Removing extra spaces and blanks.
  3. Separating or combining values in a cell when needed.
  4. Checking that the values in certain columns are of an appropriate range.
  5. Checking for outliers.
  6. Correcting wrongly spelt or inputted data.
Aug 2, 2022

How can I learn SQL effectively? ›

Here are some steps that can help you learn SQL quickly and effectively.
  1. Learn basic SQL syntax. Learning any language starts with the basics—common vocabulary and phrases. ...
  2. Practice with real data. To practice SQL, you'll need to download a database management system like MySQL or PostgreSQL. ...
  3. Learn joins.
Dec 19, 2022

Top Articles
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 6228

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.