Our biggest-ever sale. 60% off AI & data skills
Buy Now
Berlatih dan lacak kemajuan Anda.
Lanjutkan dengan GoogleDengan melanjutkan, Anda menyetujui Ketentuan Penggunaan, Kebijakan Privasi dan bahwa data Anda disimpan di AS.Simpan lembar contekan ini di ujung jari Anda
Unduh PDFJalankan dan edit kode dari cheat sheet ini secara online
Jalankan kodeAlthough 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.
Analytics engines such as DuckDB, BigQuery, Snowflake, and Databricks SQL use the same core syntax shown here.
Throughout this cheat sheet, we’ll use the columns listed in this sample table of airbnb_listings
| id | city | country | number_of_rooms | year_listed |
| 1 | Paris | France | 5 | 2018 |
| 2 | Tokyo | Japan | 2 | 2017 |
| 3 | New York | USA | 2 | 2022 |
SELECT *
FROM airbnb_listings;
city column from the tableSELECT city
FROM airbnb_listings;
city and year_listed columns from the tableSELECT city, year_listed
FROM airbnb_listings;
id, city, ordered by the number_of_rooms in ascending orderSELECT id, city
FROM airbnb_listings
ORDER BY number_of_rooms ASC;
id, city, ordered by the number_of_rooms in descending orderSELECT id, city
FROM airbnb_listings
ORDER BY number_of_rooms DESC;
airbnb_listings tableSELECT *
FROM airbnb_listings
LIMIT 5;
SELECT DISTINCT city
FROM airbnb_listings;
id, city, ordered by country, then by number_of_rooms in descending orderSELECT id, city
FROM airbnb_listings
ORDER BY country ASC, number_of_rooms DESC;
SELECT *
FROM airbnb_listings
LIMIT 10 OFFSET 5;
city column to location using an aliasSELECT city AS location
FROM airbnb_listings;
'Large' or 'Small' based on its number_of_roomsSELECT city,
CASE WHEN number_of_rooms >= 3 THEN 'Large'
ELSE 'Small'
END AS size
FROM airbnb_listings;
-- A single-line comment
/* A comment that spans
several lines */
SELECT *
FROM airbnb_listings;
number_of_rooms is more or equal to 3SELECT *
FROM airbnb_listings
WHERE number_of_rooms >= 3;
number_of_rooms is more than 3SELECT *
FROM airbnb_listings
WHERE number_of_rooms > 3;
number_of_rooms is exactly equal to 3SELECT *
FROM airbnb_listings
WHERE number_of_rooms = 3;
number_of_rooms is lower or equal to 3SELECT *
FROM airbnb_listings
WHERE number_of_rooms <= 3;
number_of_rooms is lower than 3SELECT *
FROM airbnb_listings
WHERE number_of_rooms < 3;
SELECT *
FROM airbnb_listings
WHERE number_of_rooms BETWEEN 3 AND 6;
'Paris'SELECT *
FROM airbnb_listings
WHERE city = 'Paris';
'USA' and in 'France'SELECT *
FROM airbnb_listings
WHERE country IN ('USA', 'France');
'j' and where the city does not end in 't'SELECT *
FROM airbnb_listings
WHERE city LIKE 'j%' AND city NOT LIKE '%t';
'P', any single character, then 'ris'SELECT *
FROM airbnb_listings
WHERE city LIKE 'P_ris';
'Paris' where number_of_rooms is bigger than 3SELECT *
FROM airbnb_listings
WHERE city = 'Paris' AND number_of_rooms > 3;
'Paris' OR the ones that were listed after 2012SELECT *
FROM airbnb_listings
WHERE city = 'Paris' OR year_listed > 2012;
Mastering SQL is crucial for effective data management and analysis. To leverage the power of data, your team needs to build strong SQL skills alongside other essential data tools. With DataCamp for Business, you can access comprehensive SQL training resources to upskill your team, ensuring they are well-prepared to navigate the complexities of database management and data analysis.
With our SQL training solutions, your team benefits from:
Investing in SQL training for your team empowers them to make informed decisions, optimize database performance, and efficiently manage your organization’s data assets. Request a demo today to explore how our SQL training solutions can help your team excel.
number_of_rooms is missingSELECT *
FROM airbnb_listings
WHERE number_of_rooms IS NULL;
number_of_rooms is not missingSELECT *
FROM airbnb_listings
WHERE number_of_rooms IS NOT NULL;
SELECT SUM(number_of_rooms)
FROM airbnb_listings;
SELECT ROUND(AVG(number_of_rooms), 1)
FROM airbnb_listings;
SELECT MAX(number_of_rooms)
FROM airbnb_listings;
SELECT MIN(number_of_rooms)
FROM airbnb_listings;
SELECT COUNT(*)
FROM airbnb_listings;
number_of_rooms is not missingSELECT COUNT(number_of_rooms)
FROM airbnb_listings;
SELECT COUNT(DISTINCT city)
FROM airbnb_listings;
SELECT country, SUM(number_of_rooms)
FROM airbnb_listings
GROUP BY country;
SELECT country, ROUND(AVG(number_of_rooms), 1)
FROM airbnb_listings
GROUP BY country;
SELECT country, MAX(number_of_rooms)
FROM airbnb_listings
GROUP BY country;
SELECT country, MIN(number_of_rooms)
FROM airbnb_listings
GROUP BY country;
SELECT country, ROUND(AVG(number_of_rooms), 1) AS avg_rooms
FROM airbnb_listings
GROUP BY country
ORDER BY avg_rooms ASC;
SELECT country, ROUND(AVG(number_of_rooms), 1)
FROM airbnb_listings
WHERE country IN ('USA', 'Japan')
GROUP BY country;
SELECT country, COUNT(id) AS number_of_listings
FROM airbnb_listings
GROUP BY country;
SELECT year_listed
FROM airbnb_listings
GROUP BY year_listed
HAVING COUNT(id) > 100;
WHERE filters rows before grouping; HAVING filters groups afterSELECT columns
FROM table
WHERE row_condition
GROUP BY columns
HAVING group_condition
ORDER BY columns
LIMIT n;
cheat sheets

cheat sheets

cheat sheets
SQL (Structured Query Language) is the standard language for working with data in relational databases. You use it to select, filter, sort, and summarize the rows stored in tables, which makes it a core skill for data analysts, data engineers, and data scientists.
This SQL basics cheat sheet covers querying tables, filtering numeric, text, and missing values, aggregating and grouping data, and the order SQL clauses must follow. Every example runs on one sample table,
airbnb_listings, and uses PostgreSQL syntax, which carries over to other dialects with small changes.To learn these queries step by step, take our SQL Fundamentals skill track. When you're ready to combine tables, Joining Data in SQL and the SQL Joins Cheat Sheet are the next step.
To run the examples yourself, create a DataLab workbook for free. It comes with the
airbnb_listingsdataset already loaded.