# How to MySQL

### Select Statement

With the SELECT statement, we can query data from a table.

If there is a table with the name "shows" and with the columns id and name, we can query it by calling:

```sql
-- This shows as every column which exists inside the shows table.
SELECT * FROM shows;

-- This would only show us the name of the show inside the shows table.
SELECT name FROM shows;

-- We can of course also show multiple columns.
SELECT name, genre FROM shows;
```

### Distinct

If we want to see the unique values inside a table, we can use the `DISTINCT` keyword.

As an example, you have a database with 100+ shows. Many of the shows share the same genre, so if you would make a query for the genre, you would see the same multiple times.

```sql
-- Add DISTINCT after the SELECT keyword to query only unqiues.
SELECT DISTINCT genre FROM shows;
```

### Where

We can filter information based on conditions. For that, we will need to use the WHERE clause.

Imagine that inside the show's table is a column called ratings. There are all ratings stored with a value between 1 and 100. Now we want to query all the shows which have at least a rating of 60.

```sql
SELECT *
FROM shows
WHERE ratings < 60;
```

### Like

Let's continue with the show's table. We want to look for shows which begin with the name S. We can easily do this by using the `LIKE` keyword:

```sql
SELECT *
FROM shows
WHERE name LIKE 'S%';
```

The %-sign is a placeholder and means everything after S. Also, did you notice how we combined `WHERE` and `LIKE` together?

### Between

We can use the `BETWEEN` operator inside a `WHERE` clause to filter a result within a certain range. The range must be separated with an `AND` keyword.

```sql
-- Search for all shows between 2018 and 2022
SELECT *
FROM shows
WHERE year
BETWEEN 2018 AND 2022;
```

When the provided values are text, `BETWEEN` filters within the alphabetical range.

```sql
-- Display every show where name begins with A, B, C, D.
SELECT *
FROM shows
WHERE name
BETWEEN 'A' AND 'D';
```

### Order By

With the `ORDER BY` statement, we can sort the rows of the table in ascending or descending order. By default, it sorts in ascending order.

Let's say we want to see all our shows we have in our database by its release year, ordered descending:

```sql
SELECT name, genre, stream, year
FROM shows
ORDER BY year DESC;
-- We can sort it in the ascending order by removing the DESC from our statement.
```

### Aggregate functions

#### Count

With the Count function, we can return how many rows are inside a table.

```sql
-- The asteriks inside COUNT means that we are counting every row.
SELECT COUNT(*)
FROM movies;
```

#### Min and Max

The Min function returns the smallest value inside a column. For example, let's see in a Movie Database, which is the lowest year of a movie?

```sql
SELECT MIN(year)
FROM movies;
```

And with the Max function we would see the highest value, obviously.

```sql
SELECT MAX(year)
FROM movies;
```

#### Sum

The sum function returns us the total value of all numbers count in a column. Here I have some movies with the years between 2021-2024, that's a bad example but imagine it would calculate the sum of all product prices.

```sql
SELECT SUM(year)
FROM movies;
```

#### Avg

With the avg function, we can get the average of the column. For example, the average price.

```sql
SELECT AVG(year)
FROM movies;
```

#### Group by

The `GROUP BY` statement *groups* rows of data with the same values into buckets.

We can combine it with our other Aggregate functions, like for example with Count. Want to see how much movies were released in those years?

```sql
SELECT year, COUNT(*)
FROM movies
GROUP BY year;
```

<figure><img src="https://1852081883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4fsBZk2ITHETrmsaPNE1%2Fuploads%2FMS6oXKU9lsPfZ6iidvHX%2Fimage.png?alt=media&#x26;token=7b682f60-39df-45dd-b8b5-f615f7871a4b" alt=""><figcaption><p>Output of our previous code.</p></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mysql.beko.dev/how-to-mysql.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
