SQL Basics Cheat Sheet
With this SQL cheat sheet, you'll have a handy reference guide to basic querying tables, filtering data, and aggregating data
SQL, also known as Structured Query Language, is a powerful tool to search through large amounts of data and return specific information for analysis. Learning SQL is crucial for anyone aspiring to be a data analyst, data engineer, or data scientist, and helpful in many other fields such as web development or marketing.
In this cheat sheet, you'll find a handy list of functions covering querying data, filtering data, aggregation, and more—all collected from our SQL Fundamentals Skill Track.
To learn more about SQL basics, watch this video from our course. To easily run all the example code in this tutorial yourself, you can create a DataLab workbook for free that has the dataset, SQL, and the code samples preconfigured.
PDF resource SQL for Data Science
The Different Dialects of SQL
Although SQL languages all share a basic structure, some of the specific commands and styles can differ slightly. Popular dialects include MySQL, SQLite, SQL Server, Oracle SQL, and more. PostgreSQL is a good place to start —since it’s close to standard SQL syntax and is easily adapted to other dialects.
Sample Data
Throughout this cheat sheet, we'll be using the sample data airbnb_listings—denoting rental apartments on Airbnb.
id city country number_of_rooms year_listed
1 Paris France 5 2018
2 Tokyo Japan 2 2017
3 New York USA 2 2022
Querying tables
Get all the columns from a table
SELECT *
FROM airbnb_listings.csv;Return the city column from the table
city column from the tableSELECT city
FROM airbnb_listings.csv;Get the city and year_listed columns from the table
city and year_listed columns from the tableSELECT city, year_listed
FROM airbnb_listings.csv;Get the listing id, city, ordered by the number_of_rooms in ascending order
id, city, ordered by the number_of_rooms in ascending orderSELECT city, year_listed
FROM airbnb_listings.csv
ORDER BY number_of_rooms ASC;Get the listing id, city, ordered by the number_of_rooms in descending order
id, city, ordered by the number_of_rooms in descending orderSELECT city, year_listed
FROM airbnb_listings.csv
ORDER BY number_of_rooms DESC;Get the first 5 rows from airbnb_listings
airbnb_listingsSELECT *
FROM airbnb_listings.csv
LIMIT 5;Get a unique list of cities where there are listings