Primer on SQL using a test customer dataset

Feb 20, 2023
SQL MariaDB

SQL stands for Structured Query Language. It is a standard programming language used for managing and manipulating relational databases.

Relational databases are a type of database that organizes data into one or more tables, where each table consists of a set of columns and rows. SQL allows users to perform various operations on these tables, such as querying the data to retrieve information, inserting new data into the tables, updating existing data, and deleting data.

SQL is used by a wide variety of organizations and applications that rely on databases to store and manage their data. It is an essential tool for data analysts, software developers, and other professionals who work with large sets of structured data. SQL is a powerful language that can handle complex queries and data manipulation tasks, making it a valuable skill for anyone who works with data.

In this workbook we will use MariaDB to explore a test dataset.

Installing MariaDB on a Linux machine vai terminal:


Hit the below command to start installing MariaDB:
$ sudo apt install mariadb-server mariadb-client

Testing if the installation is successful:
$ mysqladmin --version
>> mysqladmin Ver 9.1 Distrib 10.3.37-MariaDB, for debian-linux-gnu on x86_64

For uninstalling MariaDB:
sudo apt-get purge mariadb-*

Start the MariaDB server:
sudo systemctl start mariadb

Login as root in the MariaDB server:
sudo mysql -u root

Voila! now you can log in to the database:
mariadb

Display all the databases:
MariaDB [(none)]> SHOW DATABASE;


+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)

Create and Import a database:


Create a blank database:
MariaDB [(none)]> CREATE DATABASE LearningDB;


+--------------------+
| Database           |
+--------------------+
| LearningDB         |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)

Enter into the database LearingDB:
MariaDB [(none)]> USE LearningDB;

Use the path of the database file (learning_dB.sql) to load the data into the new database (LearningDB):
MariaDB [LearningDB]> source /{your_path}/learning_dB.sql

Visualize the LearningDB database:


Show the tables withing the database:
MariaDB [LearningDB]> SHOW TABLES;


+----------------------+
| Tables_in_LearningDB |
+----------------------+
| customers            |
| employees            |
| offices              |
| orderdetails         |
| orders               |
| payments             |
| productlines         |
| products             |
+----------------------+
8 rows in set (0.000 sec)

Let's visualize what are inside the customer table:
MariaDB [LearningDB]> DESCRIBE LearningDB.customers;


+------------------------+---------------+------+-----+---------+-------+
| Field                  | Type          | Null | Key | Default | Extra |
+------------------------+---------------+------+-----+---------+-------+
| customerNumber         | int(11)       | NO   | PRI | NULL    |       |
| customerName           | varchar(50)   | NO   |     | NULL    |       |
| contactLastName        | varchar(50)   | NO   |     | NULL    |       |
| contactFirstName       | varchar(50)   | NO   |     | NULL    |       |
| phone                  | varchar(50)   | NO   |     | NULL    |       |
| addressLine1           | varchar(50)   | NO   |     | NULL    |       |
| addressLine2           | varchar(50)   | YES  |     | NULL    |       |
| city                   | varchar(50)   | NO   |     | NULL    |       |
| state                  | varchar(50)   | YES  |     | NULL    |       |
| postalCode             | varchar(15)   | YES  |     | NULL    |       |
| country                | varchar(50)   | NO   |     | NULL    |       |
| salesRepEmployeeNumber | int(11)       | YES  | MUL | NULL    |       |
| creditLimit            | decimal(10,2) | YES  |     | NULL    |       |
+------------------------+---------------+------+-----+---------+-------+
13 rows in set (0.001 sec)