What is SQL - Part 1
Tags: sql, beginners
Published: 2021-04-02 02:05:18

---
title: What is SQL - Part 1
published: true
description: Introduction to SQL
tags: sql, beginners
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9rnxam9e350k45gvey3.png
---
## Introduction to SQL

**This is part 1 of a four-part article which explains SQL**

- **Part 1**
- [Part 2](https://dev.to/marcegarba/what-is-sql-part-2-1oa1)
- [Part 3](https://dev.to/marcegarba/what-is-sql-part-3-4ica)
- [Part 4](https://dev.to/marcegarba/what-is-sql-part-4-9c1)

What is [SQL][sql]?

In my experience, many developers, although expert in their coding fields, fail to grasp the important fundamentals of the SQL language and the math behind it.

For instance, strict SQL:
* Is a [Logical Programming][logical] language
  * It's **not Procedural**!
* Has strong math behind it
  * It's based on Relational Algebra, which in turn uses [Set Theory][set_theo]
* Does not operate on _files_, _records_ and _fields_, but rather on **tables**, **rows** and **columns**
  * **Quite a different approach** and quite different concepts

## Why _briefly_ touch the theory rather than jump directly to SQL code samples

As part of my teaching younger devs, once I had to explain what [Git][git] is to coders that have used [Subversion][SVN] for years.

I understood that explaining the **foundation features** of git, which are absolutely different from those of SVN, right from the start, would give them the proper understanding and see **what they can do** with such a monster version control product. Concepts such as:

* [Content Addressable File system][CAF]
* Git objects, such as blobs, trees, commits, tags
* Branching and merging in Git

In the same way, this first part starts with a **little** theory.

## SQL, a [Domain Specific Language][DSL]
It's a very old [4th Generation Languages][FGL], widely in use, which expresses Data using [Relational Algebra][REL] and (lately) other mathematical concepts, such as [Recursion][recursion].

### What is it?
Relational Algebra is closely related with [Sets][set].

SQL was first formally defined in a [1974 paper][paper].

#### The name SQL has two origins:
1 Abbreviation of **Sequel**, from the mentioned 1974 paper
2 **S**tructured **Q**uary **L**anguage

#### It has different parts, each of which deals with distinct aspects of manipulating data and metadata in a Database Management System, or [DBMS][DBMS], such as:
* **DDL**: [Data Definition Language][DDL]
  * Defines the _objects_ in a DBMS, such as Tables, Views, Constraints
* **DML**: [Data Manipulation Language][DML]
  * Retrieves, adds, deletes and updates data from Tables in a DBMS
* **DCL**: [Data Control Language][DCL]
  * Gives or revokes specific permissions for DB users

#### A Relation is a Table
* A Table is a matrix that has:
  * Columns (_attributes_), where each of them has a name and is of a _specific data type_, such as number, text, boolean and others
  * Rows (_tuples_), which represent a unique combination of data in the columns
* To consider a Table as a __Set__, in the mathematical sense, it should not allow duplicate rows, _although this is not always the case_
* A few considerations of Sets, which will help to understand the way SQL works:
  * Sets do not allow duplicate elements
  * The order of elements in a set is not important
  * All operations in sets are expressed at once
  * There are operations in Sets, such as [Unions][union], [Intersections][intersect], [Cartesian product][Cartesian]

#### Constraints
* To prevent tables to contain erroneous data, at the Database level the enforcement of **rules** may be established:
  * Keys, such as Primary Keys <sup><a name="cbpk">[1](#pk)</a></sup>, Unique Keys, Foreign Keys <sup><a name="cbfk">[2](#fk)</a></sup>
  * Admission or prohibition of NULL values <sup><a name="cbnull">[3](#null)</a></sup>
  * Check Constraints, where columns might have only selected values

----
<a name="pk">[1](#cbpk) Primary Keys assure that a table can't contain duplicate rows.</a>
<a name="fk">[2](#cbfk) Foreign Keys restrict the values of one or more columns in a table to the Primary Keys of another table, or itself.</a>
<a name="null">[3](#cbnull) The NULL value represents both, unknown data and, when joining tables, missing data.</a>

[sql]: https://en.wikipedia.org/wiki/SQL
[DSL]: https://en.wikipedia.org/wiki/Domain-specific_language
[FGL]: https://en.wikipedia.org/wiki/Fourth-generation_programming_language
[REL]: https://en.wikipedia.org/wiki/Relational_algebra
[set]: https://en.wikipedia.org/wiki/Set_(mathematics)
[paper]: https://researcher.watson.ibm.com/researcher/files/us-dchamber/sequel-1974.pdf
[recursion]: https://en.wikipedia.org/wiki/Recursion_(computer_science)
[DBMS]: https://en.wikipedia.org/wiki/Relational_database#RDBMS
[DDL]: https://en.wikipedia.org/wiki/Data_definition_language
[DML]: https://en.wikipedia.org/wiki/Data_manipulation_language
[DCL]: https://en.wikipedia.org/wiki/Data_control_language
[union]: https://en.wikipedia.org/wiki/Union_(set_theory)
[intersect]: https://en.wikipedia.org/wiki/Intersection_(set_theory)
[Cartesian]: https://en.wikipedia.org/wiki/Cartesian_product
[logical]: https://en.wikipedia.org/wiki/Logic_programming
[set_theo]: https://en.wikipedia.org/wiki/Set_theory
[git]: https://en.wikipedia.org/wiki/Git
[SVN]: https://en.wikipedia.org/wiki/Apache_Subversion
[CAF]: https://en.wikipedia.org/wiki/Content-addressable_storage#Content-addressed_vs._location-addressed