How to Use SQL to Analyze And Visualize Data?
Tags: programming, tutorial, datascience, database
Published: 2022-08-18 21:39:25

The goal of every business is to perform efficiently, maximize profit and make strategically-guided decisions. In the current business world, the competition is high and every business needs every edge and advantage they can get to improve their odds of success.

Companies that want to stay in business and thrive in the current business world must employ data analysis. Data analysis plays a role in making decisions more scientific and helping businesses operate more effectively.

## What is Data Analysis?

Data analysis can be described as the process of cleaning, transforming, and processing raw data to extract actionable and relevant information that helps businesses make informed decisions. The process of data analysis provides useful insights and statistics that can be presented in tables, charts, and graphs.

In this article, you will learn how to get useful information from a collection of data by running SQL queries. Also, you will learn how to present that information using graphs and charts. I will be using a dataset of different stores of a supermarket company, PostgreSQL, and the Arctype SQL client.

## What is PostgreSQL and Arctype?

PostgreSQL, also known as Postgres, is a free and open-source relational database management system. Arctype is a fast and easy-to-use database management tool for writing SQL queries, building dashboards, and sharing data with your team.

### Setting Up PostgreSQL

To use Postgres in this tutorial, navigate to the [Postgres download page](https://www.postgresql.org/download/) and download it for your operating system. It is available for Linux, macOS, and Windows.

After installing Postgres on your machine, run the command below to confirm the installation.

```
psql --version
```

You should be able to see the version of Postgres installed into your system. Now run the command shown below to open the Postgres prompt.

```
sudo -u postgres psql postgres
```

Once Postgres has opened on the command line, run the command below to set a password for postgres.

```
\password postgres
```

To quit the Postgres prompt, run the command shown below.

```
\q
```

Let us now create a database called *“supermarket”* in PostgreSQL that will be used to store data from the supermarket stores CSV file. We run the command below.

```
CREATE DATABASE supermarket;
```

Then run the command shown below to see if the database you created is on the list of databases in Postgres.

```
\l
```

As you can see from the screenshot below, the *supermarket* database was created in PostgreSQL.

![](https://lh3.googleusercontent.com/at_lleFo7-crECRgrlOD6_I95JgM1T8EOk1lERJGgCvwpHQgbIPEJMVs5JL5rInCd1A7tsmJ7cMwymnu4PbLwpdBt9ecQLQZZUnFJAXWfEdTVeNKErZq91HQr1Lj-p8fpXnYrhrijTlHf1j_)

## The Arctype SQL Client

To use Arctype, navigate to [the Arctype homepage](https://arctype.com) and download the Arctype app on your machine. The application is available on [Windows](https://arctype.com/api/download/windows), [MacOS](https://arctype.com/api/download/mac), and [Linux](https://arctype.com/api/download/linux).

![](https://lh4.googleusercontent.com/g4Wshi5UHGpYNrW6zWhucPuz84XmEY7vduyUVlpqSXtcDw4VpZbeTK1tS8Q0_x8t5DOLs_OZ9DDCuAHErtYhE43z2jpjzvGolMZy5fpxdG2O4ZHeJd-rJktUD7mmn-9sWJ1us_asfGWruxg2)

Once the app has been installed, open it and you will be prompted to create an account as shown below.

![](https://lh3.googleusercontent.com/NuO_Nr0SNYcMk8ym5qgqSwPzGR-sL_QcT96-Xdor6CyZUhNerPXrAko_yu7lF7SJmnlun5HKtIx9Mxx1Qxa2KcTbtimqO-kxjKvPtc2ZgUr2c3tCVKCnql0h-fZYxnqCmyeyo6LfeSV7NfZH)

## Integrating the Arctype SQL Client With Postgres

Once you have created an account, you will be prompted to select your database. In this case, select Postgres.

![](https://lh4.googleusercontent.com/kMJyE5muS8ztJWfSQGbZPRF3FPFzdb-X2WOsHMHixCTwZi4zL8IycNqKrg4Cf8xGreh5IgGeIEgi2kPOm3x-hHReZnJAVxHrKUt5xkET9z_CH3w9z24TbPNdYE9eYZWC9hmg9a6oUg8XCYpR)

The next step is to add Postgres credentials to connect Arctype to your Postgres database.

![](https://lh3.googleusercontent.com/q9DMdEzNb_SOz_CUmJIijcSzVr-Sgi-DUX5RGH85owGc83_B6ALVWXG0MmqYlWWBVqaBG0PbKiHAo4uNupaXrNFJoeT_7AQzaRkPlQjb1IqIby9axr3YRDT3bHiknJKY2MtgVlfDhyyzhWjJ)

Fill in the fields according to the example shown below.

- Name: Supermarkets Dataset
- Host: 127.0.0.1
- Port: 5432
- User: postgres
- Password: *Postgres password*
- Database: supermarket

**Note**: Use the credentials you created when creating a database in the Postgres prompt.

Once you have entered the Postgres credentials, click the “Test Connection” button at the bottom to make sure credentials are correct. If the credentials are correct, click the “save” button to the right of the “Test Connection” button to complete Postgres and Arctype SQL client integration as shown below.

![](https://lh5.googleusercontent.com/JoYLMIXksXqrihbbkzHA_lRgDis9FQk-tkKPBbHucQUVdxzfGnvHcHtCXMKNIA-lGBPAdxOdZobbCGUya4PSOMZvIomYRJUI1fv3io61F3Dw05uYGGltZZt0Lj1M5NVy0d_4ExPwpD53XnBg)

## Importing CSV Files Into Postgres Using Arctype

We will be importing the supermarkets stores dataset CSV file that you can download by navigating to this [Kaggle web page](https://www.kaggle.com/datasets/surajjha101/stores-area-and-sales-data) as shown below.

![](https://lh5.googleusercontent.com/GM1rvwzHNhHwtYYe741Y3Ef3pWdpbHnoJwEkRIVUd9fn1TTJ6cOdyKr5d4D2pvoXdtX_CNV9dw8x1MB011wd_E55cUGqW2oil5Bksrmh4xCTlaA-lwng4FW-Jb4c5p8yzFkVQC0z8_T3OUu4)

To import the CSV file into Postgres using Arctype, navigate to the “Tables” view. Click on the three dots next to the table search field. Then select “Import CSV to Table” as shown below.

![](https://lh4.googleusercontent.com/hiFVTSiZU0ra-IpJ7_Y3YVqiGlyZH4muub3F0ZdU-WPUx42_x0U62wmPdyYPY8j1g-2I1uevRcA85EGiBbFvwMNJz87rTh9cIbIhQgzYbPbQD2f954lhPbQ_EfA6E31EUqbEXOdBnPaKnJXA)

Next use the file selection window that appears to select the CSV file you downloaded. Select your file and click the “Open” button as shown below.

![](https://lh5.googleusercontent.com/gSDlAzXWS27I2o-bLehtkAw8cjP2MLAWedFGrLDWqzFW-eHqQTBt0hr-o2CAhDjLIIv1MbjgesxzcwzLD9tMVSXuPi5laHUblVadzPrrxlfEyPMPURupGk5mLaCcgcrPOlCABpV16GmRbCW-)

Once you have selected and opened the CSV file, Arctype will display the first ten rows of the CSV file data for preview. If everything looks correct, click the “Accept” button as shown below.

![](https://lh6.googleusercontent.com/PTn8S7hUx9H3P83vc2K4MKDlfi1VQrhL46v1aEvev3wX_tHCSWpATm85omqtYG5RtMIPpXoEpizjW6yLOmJqMl3RZWuXvGxzNx4xLDm2j-VSqV-ikgv616RLsLGaBQBBDixitD1kYyJJHUi0)

After clicking the “Accept” button, you will be redirected to a configuration window as shown below. The window allows you to determine where you want the data to be stored and to select the schema.

![](https://lh5.googleusercontent.com/Qk7AXy4QQz4b3KcJMDYuQbkGUWy_vd9eGBoIBcbWNGCV_Wh2bZgQ0CF_1i7MaBfvmAhJFBKKM7DW1kgcqEjq_zg-pLaJ_lLU_twQhAofOTvQP40iEOt8dZtaJJX96Ww2Q9lreY-5vYSVEtMK)

If everything looks good, click the “Import CSV” button to start importing your CSV file. Open the table data was successfully imported,everything should look as shown below.
![](https://lh3.googleusercontent.com/W3h9X2CpGcUe7-YRhUkECYygK_z5Ha29SFI1HaJGQvZNjazfEgY0ivKkVJ9RfseP5M13Pl5XEjhPDb994FPINEs7UZE0Alir5wR7jA6mxqctGxzc7dIM9dTdClOLoiu3VwThpFKOtXGBcfrx)

## Running SQL Queries With Arctype

SQL queries allow you to display or modify data from databases. In this section, I will show you some queries you can run to create tables, charts or graphs to visualize the supermarkets data.

Let us assume you are the owner of the supermarkets. You would want to know interesting stats such as the top 10 performing stores by sales. To get such insights, you can run a SQL query that creates a table of top ten performing stores. Then you can visualize the data by creating a graph or a chart.

To run a SQL query using Arctype, click the “Create query” button on the top right of the Arctype window. A screen will then appear where you can run a query as shown below.

![](https://lh5.googleusercontent.com/flInsQqFcx5nNdEcl7zLRigpj2Q6tSR1KrSdaXa52B-y317ReTRgoSM9Uwx5ya1O3z1n-eiZNPEGNfNZq7c0ASYI3sA6Qxv5iub3-s6sKNSrEogAgraQIZpgxD_AaPP-cKyF6ahsh71uwiaz)

Give your query a descriptive name like “Top 10 Stores By Sales.” Then write the following SQL query to determine the top 10 stores by sales from the stores data.

`SELECT store_area, store_sales AS Sales FROM stores GROUP BY store_id, store_sales ORDER BY store_sales DESC LIMIT 10;`

The query selects data in the column *store_area* and *store_sales* from the *stores* table where *store_sales* is renamed to Sales. The data then gets grouped by the columns named *store_id* and *store_sales* (Sales.) After that, the data is ordered by *store_sales* where sales are arranged from highest to lowest. The query limits the results to the  top 10 selling store areas.

If everything goes well, the query should create a table with two columns named *store_area* and *sales* as shown below.

![](https://lh6.googleusercontent.com/I82UKTCf1HIt7SPPG3sW7dI-zyBlPIA7AA_IQX7MaWUX547uBVUedpoutvOf0mYBUQcxNeB2dMJIJ3sVAyGBhEy99YANqKdn4iXJNgQHYmP_qu0430qcg50Y0w0V0K5ieFZLYULBStXcY8p4)

## Creating Charts Using Arctype

Let us now create a chart from the table created by the SQL query. To create a chart using Arctype, select the “Chart” option at the bottom right of the SQL editor as shown below.

![](https://lh6.googleusercontent.com/Sfa2nf3s5XxX_3rg1NYhkWlC4n5wLI6cwij1eiYgqXEFXZAmD7825DhuWdw4DmezDoGtzvx9oSy-isnw676CSjfKGMs2OgI6aFgLyGnj4y5R-gFWqS4x451gLn5bia7qZN_ND2cyEU8DEm7y)

On the right side of the screen, you can select the type of chart you want. In this case, we select “Bar Chart.” Then below the “Select Chart Type” option, you have the “Specify Columns For The Chart” option. In this section, drag the *store_area* column to the X-axis column and the *sales* column to the Y-axis column. [Arctype](https://arctype.com) should help you  come up with a chart similar to the one shown below.

![](https://lh6.googleusercontent.com/UQ3LJMH6yKA0VvOX22RdQYoGFOMjYu5NOn6YWk9l1a7J_fcNoJV2jlnGIKjH4GZ4V4Hfp0M03HlTHCFB-YaesBOtgTdk7KWnYZx56g_UdRVIQKjOk1oeHpJm881rb_E_Gthx-nqO3ExQiuo-)

## Conclusion

In this article, you have learned how to create a database in PostgreSQL, import a CSV file into the DBMS and how to run SQL queries to analyse data. Now, I suggest you  try to create different SQL queries to come up with some more exciting insights and use [Arctype](https://arctype.com) to create charts.