# SQL Basics

# Introduction

Welcome to the SQL for Beginners course! At the end of this course, you will have the tools to fetch, manipulate, and present meaningful data from databases. This course aims to give you the basics of extracting knowledge from large amounts of data. SQL (which stands for "Structured Query Language") is used to manage data from a Relational Database Management System (RDBMS). This means you will learn how to manage data arranged in a tabular format.

[Read More](https://en.wikipedia.org/wiki/SQL)

# What is a database

Databases are like large buckets that store data in an organized manner. A few examples of when we would like to create a database:

<div class="Lesson_content__j6FUx" id="bkmrk-a-database-for-a-uni">- A database for a university to save data about students, courses, and lecturers.
- A database for a car agency to track sales, car storage, and workers.
- A database for a hospital to save information about patient's history to provide good health care
- And many more

</div>Inside a database there are tables, and each table has a name, column names, and rows. For example, this is a `<strong>workers</strong>` table:

<div class="Lesson_content__j6FUx" id="bkmrk-%C2%A0-firstname-lastname"><figure class="table"><table><thead><tr><th> </th><th>firstname</th><th>lastname</th><th>age</th><th>exp\_years</th><th>gender</th></tr></thead><tbody><tr><td>1</td><td>Ghully</td><td>Thuas</td><td>29</td><td>2.3</td><td>Female</td></tr><tr><td>2</td><td>Bostal</td><td>Shkolky</td><td>32</td><td>0.2</td><td>Male</td></tr><tr><td>3</td><td>Qaostu</td><td>Malop</td><td>21</td><td>4</td><td>Female</td></tr></tbody></table>

</figure></div>The `<strong>workers</strong>` table has 5 columns and 3 rows. We don't need any tool to know that we have 3 workers and it is easy to calculate the average age of all of them `(29 + 32 + 21) / 3` but what happens when we have a thousand rows or even a million rows?

For that, we have databases and the SQL language. database stores all of the tables and SQL extracts the data.

# Database concepts

Let's define a few concepts

<div class="Lesson_content__j6FUx" id="bkmrk-rows-are-referred-to">- rows are referred to as **records**
- columns are referred to as **fields**

</div>The number of records in a database is unlimited, but the number of fields is limited to the number of fields that were created for the table. Field naming is important because we use it when writing SQL queries. Field names should be lowercase, without spaces, and singular because the field refers to a single record. For example:

<div class="Lesson_content__j6FUx" id="bkmrk-full_name-instead-of">- full\_name instead of FullNames
- firstname instead of firstName
- salary instead of salaries

</div>It is not allowed to name two different fields with the same name, as this can lead to confusion and difficulty in distinguishing between a table name and a field. To identify different records, it is best to use a unique identifier called a "key". Every table should have a key field, as it allows us to distinguish between two similar records. This key field is usually referred to as the "id".

# Unique values

Let's assume we have the following table

**sales**

<div class="Lesson_content__j6FUx" id="bkmrk-%C2%A0-country-city-amoun"><figure class="table"><table><thead><tr><th> </th><th>country</th><th>city</th><th>amount</th></tr></thead><tbody><tr><td>1</td><td>Poland</td><td>Warsaw</td><td>13</td></tr><tr><td>2</td><td>Germany</td><td>Berlin</td><td>24</td></tr><tr><td>3</td><td>Poland</td><td>Katowice</td><td>56</td></tr></tbody></table>

</figure></div>And we would like to know all of the countries that the product was sold.

If we use the normal query we know: `SELECT country from sales` it will return `Poland Germany Poland`. This is not what we are looking for because Poland is repeated twice.

To solve it we can use the `DISTINCT` keyword:

```sql
SELECT DISTINCT country FROM sales
```

# Conditional statements part 1

Sometimes we would like to fetch records that meet a certain condition.

For example

<div class="Lesson_content__j6FUx" id="bkmrk-fetch-all-of-the-rec">- fetch all of the records that have the family name "Aothly"
- fetch all of the records that the amount is bigger than 5
- fetch all of the records with the country "Mexico"

</div>To add condition we can use the `WHERE` keyword

**sales**

<div class="Lesson_content__j6FUx" id="bkmrk-coin-amount-agk-13-g"><figure class="table"><table><tbody><tr><td>coin</td><td>amount</td></tr><tr><td>AGK</td><td>13</td></tr><tr><td>GOL</td><td>21</td></tr><tr><td>KLA</td><td>15</td></tr><tr><td>AGK</td><td>18</td></tr></tbody></table>

</figure></div>To fetch all of the records with the coin "AGK" we will write:

```sql
SELECT * FROM sales WHERE coin = "AGK"

```

<div class="Lesson_content__j6FUx" id="bkmrk--1"><div><div>  
</div></div></div>To fetch all of the records with amount smaller than 20 we will write:

```sql
SELECT * FROM sales WHERE amount < 20
```

# Conditional statements part 2

Creating a query with only one condition is not sufficient. Sometimes we would like to check something more complicated. For that SQL (and many other programming languages) have the `AND`, `OR`, and `NOT` keywords to increase our ability to fetch the right result we need.

The `AND` and `OR` keywords are used like this:

```sql
SELECT col1, col2 
FROM table1
WHERE condition1 AND condition2 OR condition3 ...

```

<div class="Lesson_content__j6FUx" id="bkmrk--1"><div><div>  
</div></div></div>We can stack as many conditions as we want together.

<div class="Lesson_content__j6FUx" id="bkmrk--2">---

</div>**people**

<div class="Lesson_content__j6FUx" id="bkmrk-name-age-gender-joas"><figure class="table"><table><thead><tr><th>name</th><th>age</th><th>gender</th></tr></thead><tbody><tr><td>Joas</td><td>13</td><td>male</td></tr><tr><td>Holwa</td><td>17</td><td>male</td></tr><tr><td>Nohlas</td><td>24</td><td>female</td></tr><tr><td>Polar</td><td>23</td><td>male</td></tr><tr><td>Loopa</td><td>18</td><td>female</td></tr></tbody></table>

</figure></div>The `AND` keyword means that **both** conditions must be true; if either of them is not, then the condition will not be met.

For example, if we will write

```sql
SELECT * 
FROM people
WHERE gender = "female" AND age < 20

```

<div class="Lesson_content__j6FUx" id="bkmrk--3"><div><div>  
</div></div></div>It means that we are looking for all records that the gender is "female" and the age is less than 20.

This will be the result:

<div class="Lesson_content__j6FUx" id="bkmrk-name-age-gender-loop"><figure class="table"><table><thead><tr><th>name</th><th>age</th><th>gender</th></tr></thead><tbody><tr><td>Loopa</td><td>18</td><td>female</td></tr></tbody></table>

</figure></div>The `OR` keyword means that we want one of the conditions will be true.

For example, if we take the same example from above and change the `AND` keyword to `OR`

```sql
SELECT * 
FROM people
WHERE gender = "female" OR age < 20

```

<div class="Lesson_content__j6FUx" id="bkmrk--4"><div><div>  
</div></div></div>It means that we are looking for all records that either the gender is female or the age is less than 20.

This will be the result:

<div class="Lesson_content__j6FUx" id="bkmrk-name-age-gender-joas-1"><figure class="table"><table><thead><tr><th>name</th><th>age</th><th>gender</th></tr></thead><tbody><tr><td>Joas</td><td>13</td><td>male</td></tr><tr><td>Holwa</td><td>17</td><td>male</td></tr><tr><td>Nohlas</td><td>24</td><td>female</td></tr><tr><td>Loopa</td><td>18</td><td>female</td></tr></tbody></table>

</figure></div>The `NOT` keywords mean that we don't want the condition to be met.

For example, if we write:

```sql
SELECT * 
FROM people
WHERE NOT gender = "male"

```

<div class="Lesson_content__j6FUx" id="bkmrk--5"><div><div>  
</div></div></div>This will be the result:

<div class="Lesson_content__j6FUx" id="bkmrk-name-age-gender-nohl"><figure class="table"><table><thead><tr><th>name</th><th>age</th><th>gender</th></tr></thead><tbody><tr><td>Nohlas</td><td>24</td><td>female</td></tr><tr><td>Loopa</td><td>18</td><td>female</td></tr></tbody></table>

</figure></div><div class="Lesson_challengeContainer___d1_L" id="bkmrk--6"></div><div class="Lesson_challengeContainer___d1_L" id="bkmrk-easy"><div class="Lesson_challengeTop___c7ZK"></div></div>

# Conditional statements part 3

Conditions are booleans. Boolean is a data type with two possible values: `TRUE` or `FALSE`.

For example

<div class="Lesson_content__j6FUx" id="bkmrk-10-%3E-100%C2%A0--false-10-">- `10 > 100` - FALSE
- `10 > 5` - TRUE
- `10 > 5 AND 100 < 5` - FALSE

</div>Boolean columns have only two values - either 1 or 0. `TRUE` indicates 1 and `FALSE` indicates 0

We can replace columns such as employed or unemployed to `1` or `0` to make it easier to filter data. To filter data using booleans we will use the `IS TRUE` or `IS NOT TRUE` keywords.

```sql
SELECT *
FROM table1
WHERE col1 IS NOT FALSE AND col2 IS TRUE
```