Learn the basics of SQL
Tags: beginners, tutorial, sql, database
Published: 2022-09-26 13:53:00

When you want to become a software developer you’ve got a bajillion things to learn. This makes it scary for the beginner, since there are few things to grab on to to simplify the task. I’ve taken unto me teaching a bunch of [African students how to become software developers](https://aista.com/blog/teaching-you-how-to-fish/). To scale up my efforts, I’ve also started an article series where I teach all the basics they’ll need to understand before they are professionals. In this article I will have my students learn the basics of SQL, and I’ll try to compress it as much as possible, making it dead simple at the same time. There is a video further down in the article in case you prefer to see me demonstrate SQL instead of reading. However, first some basic theory allowing my students to create fruitful associations, simplifying the task.

## CRUD

To simplify things, I’ve created the entire article series around CRUD. CRUD of course is an acronym and it means Create, Read, Update and Delete. CRUD is the axiom around which all data manipulation evolves. Basically, if you understand CRUD, you understand “data”. SQL is also obviously based around CRUD, except it’s using synonyms to the CRUD verbs, and these are as follows.

* __Insert__ (Create)
* __Select__ (Read)
* __Update__ (Update)
* __Delete__ (Delete)

The above are the four basic SQL keywords, and how they map to the CRUD verbs. If you understand how to use the above 4 verbs, you basically understand SQL. In the video below I run through all verbs, with example code, using [Aista Magic Cloud](https://aista.com/) and [SQL Studio](https://aista.com/sql-studio/) to execute my SQL towards the Chinook database. Watch the video and then look at the example code found below.

{% embed https://www.youtube.com/watch?v=517SVcYisto %}

## The select keyword

The select keyword is arguably the most complex. It allows for grouping, aggregating, filtering, paging, joins, and everything required to select data from your database. However, in its most fundamental syntax it looks as follows. This is the first example snippet I use from the above video.

```
select * from Album
  where Title = 'For Those About To Rock We Salute You'
    or Title = 'Balls to the Wall'
```

The above selects all columns from all records in the “Album” table. The Album table is a table in the Chinook database. The asterisk implies “everything”, or all columns. The above “where” keyword allows you to filter your result. If you only execute the first line of code above, the database will return every single record in its album table. The where keyword allows you to filter your result, and only return the parts that meets the condition(s) of your where. If you exchange the above asterisk (*) with a list of columns, you can select a subset of columns. This is called “projections” and even allows you to select columns and “renaming” the name of your columns as the result is being returned from the database.

## Insert, Update and Delete

Below is the second snippet from the video, giving you an example use case of all CRUD verbs in SQL. Notice, to understand these keywords, you want to watch the above YouTube video where I walk you through them.

```
/* CREATE */
insert into Album (Title, ArtistId) values ('Howdy tutorial world', 1);

/* READ */
select * from Album
  where AlbumId = 348
    or Title = 'Balls to the Wall';

/* UPDATE */
update Album set Title = 'Howdy NEW turoaisl world' where AlbumId = 348;

/* DELETE */
delete from Album where AlbumId = 348;
```

## SQL homework

In later articles I will dive deeper into the select keyword, since it allows for grouping, joins, aggregating, filtering and paging – And hence arguably is the most complex of all SQL keywords. However, if you want to start early, the Chinook plugin database installs a bunch of example SQL scripts you can play with, such as for instance the following SQL.

```
/*
 * Script to aggregate records for each artist in
 * chinook database.
 * Make sure you select the chinook database before executing
 * the script.
 */
select ar.Name, count(*) as count
  from Album al, Artist ar where al.ArtistId = ar.ArtistId
  group by al.ArtistId
  order by count desc
  limit 25
```

If you execute the above SQL, you will be given something resembling the following.

![SQL select result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8kx4n11x0454j2bjpys.jpg)

What the above is basically showing you, is how many records each artist have created. Then it’s sorting descending such that the artist with the most records is shown at the top, before it finally limits the result set to the first 25 records. To translate into humanly understandable words this becomes the following result set.

> Give me the name of the 25 artists having released the most albums, and provide me with their album count, and sort such that the artists with the most albums released are returned first

The reason why the above works is because of the _“count(*) as count”_ parts. The select statement allows you to declare which columns you want to return, where “*” means _“all columns”_. However, instead of returning columns, you can also return a list of columns as we do above. You can also return _“aggregate function results”_. The count function is one such aggregate function. The above SQL is also selecting records from multiple tables at the same time. In addition the SQL is using alias for both tables. This is the _“ar”_ and _“al”_ parts. This allows us to use _“ar”_ as an alias for the _“Artist”_ table, and _“al”_ as and alias for the +“Album”_ table.

When you use aggregate functions you must also use the _“group by”_ keywords. This informs the database how the aggregate should be grouped. The above SQL for instance is grouping by ArtistId. Since each artist have a unique ArtistId, and we’re grouping by ArtistId, the result of the count operation basically becomes a count of all albums with the same ArtistId.

In future articles I will go deeper into the subject. However, for now, feel free to look at the example SQL scripts that comes together with the Chinook database and look at this task as homework ...