SQL Basics for Beginners
Tags: sql, database, beginners
Published: 2021-10-31 10:56:33

Hello everyone Let's take a brief look at the basics of SQL. Let's find out what SQL is, what commands, methods and features there are.

Let's start!
********
>###Basic terminology

**Database** - is some organized set of information.

**Relational database** - is a database built on a relational data model (according to the mathematical theory of relations).

**Database management system (DBMS)** - is a set of programs that allows you to manage the creation and use of a database.
******
>###SQL Language

**SQL (structured query language)** - is a programming language designed to work with relational databases.

**The peculiarity of SQL** - is a declarative programming language, we describe what we want to get, not how
******
>###Types of SQL statements

#####Groups of SQL statements:
 * Operators for working with database objects.
 * Data manipulation operators.
 * Transaction Management Commands
 * Data protection and management operators.

We will consider only data manipulation operators, because it is with them that you will most often meet in the course of your work.
*****
>###SELECT request. Basic syntax

 * Minimum query:
```sql
SELECT * FROM table WHERE condition;
```

 * "Separate" and "order by" operators:
```sql
SELECT DISTINCT f1, f2 FROM table
WHERE condition ORDER BY f2 DESC;
```
 * Graph operator:
```sql
SELECT QUANTITY (*) FROM table;
```
*****
>###Subqueries

**Example:** print the count of different values in the "f1" field of the table.
```sql
SELECT COUNT(*) FROM (SELECT DISTINCT f1 FROM table WHERE condition);
```
The subquery is written in parentheses, the result of its work is a table.
*****
>###The exists operator

A quick way to check if a query returns at least one value.
```sql
SELECT smth FROM table 
WHERE EXISTS (SELECT * FROM table2 WHERE table2.field = table.field1);

```
Sometimes it is convenient to use aliases:
```sql
SELECT field1 as f1, field2 as f2
FROM table AS first_table
WHERE EXISTS
(SELECT * FROM table2 AS second_table
WHERE second_table.field = first_table.f1);
```
*****
>###Group by operator

```sql
SELECT field, COUNT(*) FROM table 
WHERE condition GROUP BY field HAVING having_condition;
```
In the HAVING operator, unlike where, aggregating functions can be used.

**Some aggregating functions:**

 * SUM
 * COUNT
 * MIN
 * MAX

*****
>###Merging tables

```sql
SELECT column_name(s) FROM table1
UNION (UNION ALL)
SELECT column_name(s) FROM table2
```
*****
>###Keys

**Types of key relationships:**

 * 1 to 1
 * 1 to many
 * many to 1

![Keys](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jurcwhfm9rol4lasui59.png)
*****
>###Many to many relationship

![ManyToMany](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ry1buby71rb4x9s3i0mj.png)
*****
>###JOIN operator

![Join operator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qluyhpjr7b903gonkk1u.png)
 *****
>###Changing the contents of tables

 * Adding an element:
```sql
INSERT INTO table_name values (v1, v2, …);
INSERT INTO table_name (col1, col2) VALUES (v1, v2);
```
 * Updating an element:
```sql
UPDATE table_name SET col1 = val1, col2 = val2 WHERE condition;
```
 * Deleting an element:
```sql
DELETE FROM table_name WHERE condition;
```
*I hope my efforts will help someone to deal with such a topic as SQL.*
####I wish you good luck!

#### FAQ

I am a beginner, how should I learn Python?

Look into the following series:

[Learning Python](https://dev.to/vadimkolobanov/series/15329)
[Step by Step to Junior](https://dev.to/vadimkolobanov/series/15349)
[Ideas](https://dev.to/vadimkolobanov/series/15331)

Would you mentor me?

Of course I am ready to participate in the training. The big difficulty is that English is not my native language and it will be possible to communicate through a translator

Would you like to collaborate on our organization?

If you have interesting ideas, we will be happy to invite your writing from our organization. Write in private messages or in social networks below

Connect to me on

[Write me on Face....oh...Meta](https://www.facebook.com/neo.skarynsky)

[My Twitter](https://twitter.com/decodesperato)

<a href="https://www.patreon.com/bePatron?u=64009903" data-patreon-widget-type="become-patron-button"><img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4kAQAgAW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/db3jtgy2i5l4w8slkps5.png" alt="Become a patron" loading="lazy"></a><script asyncsrc="https://c6.patreon.com/becomePatronButton.bundle.js"></script>