## SQL Cheat Sheet for Beginners

Learn SQL with this beginner-friendly SQL cheat sheet. Whether you’re exploring data in Metabase, practicing SQL queries, or brushing up on the basics, this cheat sheet gives you all the essential SQL commands.

For the data team, it’s a great resource to share with business users so they realize SQL is just asking the database questions and start pulling their own data without waiting for your team.

## Example Data

We’ll use two tables throughout this cheat sheet.

**`customers`**  
| customer\_id | customer\_name | country |  
| --- | --- | --- |  
| 1 | Alice | USA |  
| 2 | Carlos | Mexico |  
| 3 | Bob | Canada |  
| 4 | Diana | France |

**`orders`**  
| order\_id | customer\_id | amount | order\_date |  
| --- | --- | --- | --- |  
| 101 | 1 | 250 | 2025-08-05 |  
| 102 | 2 | 75 | 2025-08-06 |  
| 103 | 3 | 120 | 2025-08-07 |  
| 104 | 4 | 45 | 2025-08-08 |

## Getting data

Use `SELECT` and `FROM` to choose data from a table.

### Get all columns from a table

```sql
SELECT *
FROM customers;
```

### Get specific columns

```sql
SELECT customer_name
FROM orders;
```

### Get unique values from a column

```sql
SELECT DISTINCT country
FROM customers;
```

## Filtering data

Use `WHERE` to filter rows by conditions.

### Filter rows above a certain amount

```sql
SELECT *
FROM orders
WHERE amount > 100;
```

### Filter rows matching multiple conditions

```sql
SELECT *
FROM orders
WHERE order_date > '2025-08-06'
  AND amount > 100;
```

### Filter rows matching either condition

```sql
SELECT *
FROM customers
WHERE country = 'USA'
   OR country = 'Canada';
```

### Filter rows by a list of values

```sql
SELECT *
FROM customers
WHERE country IN ('USA', 'Canada');
```

### Exclude rows with specific values

```sql
SELECT *
FROM customers
WHERE country NOT IN ('USA', 'Canada');
```

### Filter rows within a range

```sql
SELECT *
FROM orders
WHERE amount BETWEEN 50 AND 200;
```

### Filter rows by text patterns

```sql
SELECT *
FROM customers
WHERE customer_name LIKE 'A%';
```

### Filter rows by date

```sql
SELECT *
FROM orders
WHERE order_date >= '2025-08-06';
```

## Aggregating data

Use `COUNT`, `SUM`, `AVG`, and `GROUP BY` to summarize data.

### Count all rows

```sql
SELECT COUNT(*)
FROM orders;
```

### Count distinct values

```sql
SELECT COUNT(DISTINCT customer_id)
FROM orders;
```

### Calculate sum

```sql
SELECT SUM(amount)
FROM orders;
```

### Calculate average value

```sql
SELECT AVG(amount)
FROM orders;
```

### Group rows by a column

```sql
SELECT country, COUNT(*)
FROM customers
GROUP BY country;
```

### Group rows and calculate sums

```sql
SELECT customer_id, SUM(amount)
FROM orders
GROUP BY customer_id;
```

### Filter grouped results

```sql
SELECT customer_id, SUM(amount)
FROM orders
GROUP BY customer_id
HAVING SUM(amount) > 100;
```

### Use CASE for conditional logic

```sql
SELECT order_id, amount,
  CASE
    WHEN amount > 100 THEN 'High'
    ELSE 'Low'
  END AS order_size
FROM orders;
```

## Organizing Results

Sort results with `ORDER BY`, restrict them with `LIMIT`.

### Sort rows in ascending order

```sql
SELECT *
FROM orders
ORDER BY order_date;
```

### Sort rows in descending order

```sql
SELECT *
FROM orders
ORDER BY amount DESC;
```

### Restrict the number of rows returned

```sql
SELECT *
FROM customers
LIMIT 3;
```

### Sort and return only the top results

```sql
SELECT order_id, amount
FROM orders
ORDER BY amount DESC
LIMIT 3;
```

### Sort rows by multiple columns

```sql
SELECT customer_name, country
FROM customers
ORDER BY country ASC, customer_name ASC;
```

### Skip rows with OFFSET

```sql
SELECT *
FROM orders
ORDER BY order_date
LIMIT 3 OFFSET 2;
```

## More SQL resources

This cheat sheet is a great way to dip your toes into SQL, but once you’re comfortable with the basics, you may want to explore more advanced techniques and dive deeper into data analysis. We’ve got you covered there with our Learn SQL path, which includes articles like:

- [SQL filtering by text](/content/learn/sql/filtering/by-text/index.html)
- [SQL filtering by date](/content/learn/sql/filtering/by-date/index.html)
- [Combining tables with SQL UNION](/content/learn/sql/combining-tables/sql-union/index.html)
- [Best practices for writing SQL queries](/content/learn/sql/working-with-sql/sql-best-practices/index.html)
- [Debugging SQL syntax errors](/content/learn/sql/debugging-sql/sql-syntax/index.html)
- And much more: [Learn SQL](/content/learn/sql/index.html)
