SQL 101: Introduction to SQL for Data Analysis
Tags: sql, programming, codenewbie
Published: 2023-02-19 15:51:19

Structured Query Language, or SQL for short, is a programming language used to manage and manipulate relational databases. It is widely used in the field of data analysis, where data is often stored in a structured format. SQL provides a powerful set of tools for querying and analyzing data, making it an essential skill for any data analyst.

This article  explores SQL for data analysis, including its syntax, common functions, and how to use it to manipulate data. I've included sample tables and data sets to illustrate the concepts we discuss.

## Getting Started with SQL

Before we dive into the details of SQL, let's review some basic concepts. In SQL, data is stored in tables, which consist of rows and columns. Each column represents a specific data attribute, while each row represents a unique instance of the data.

For example, consider the following table, which represents data about a customer's purchases:


Customer ID	   Product ID	   Purchase Date	Price
1	           100	              2022-01-01	10.99
1	           101	              2022-01-02	5.99
2	           102	              2022-01-03	20.00
3	           100	              2022-01-04	12.99
3	           103	              2022-01-05	7.50

This table has four columns: Customer ID, Product ID, Purchase Date, and Price. Each row represents a unique purchase, with the Customer ID, Product ID, Purchase Date, and Price for that purchase.

## SQL Syntax

SQL uses a specific syntax to perform operations on tables. Here are some common SQL statements and their syntax:

**SELECT statement**: The SELECT statement is used to retrieve data from a table.

```
SELECT column1, column2, ...
FROM table_name;

```
For example, to retrieve all the data from the customer_purchases table, we would use the following SELECT statement:

```
SELECT *
FROM customer_purchases;
```
This would retrieve all the rows and columns in the customer_purchases table.

**WHERE statement**: The WHERE statement is used to filter data based on specific conditions.

```
SELECT column1, column2, ...
FROM table_name
WHERE condition;

```
For example, to retrieve all purchases made by customer ID 1, we would use the following WHERE statement:

```
SELECT *
FROM customer_purchases
WHERE Customer ID = 1;

```
This would retrieve all the rows in the customer_purchases table where the Customer ID is equal to 1.

**GROUP BY statement**: The GROUP BY statement is used to group data based on specific columns.

```
SELECT column1, column2, ..., aggregate_function(column)
FROM table_name
GROUP BY column1, column2, ...;

```
For example, to group the customer_purchases table by Customer ID and calculate the total amount spent by each customer, we would use the following GROUP BY statement:

```
SELECT Customer ID, SUM(Price) as Total
FROM customer_purchases
GROUP BY Customer ID;

```
This would retrieve the total amount spent by each customer in the customer_purchases table.

**JOIN statement**: The JOIN statement is used to combine data from multiple tables

```
SELECT column1, column2, ...
FROM table1
JOIN table2
ON table1.column = table2.column;

```
For example, consider the following two tables:

customer_table:

     Customer ID	Name
       1	        Aggie
       2               Annicia
       3               Albright

customer_purchases table:

Customer ID	  Product ID	   Purchase Date	Price
1	          100	              2022-01-01	10.99
1	          101	              2022-01-02	5.99
2	          102	              2022-01-03	20.00
3	          100	              2022-01-04	12.99
3	          103	              2022-01-05	7.50
To retrieve the customer name and purchase data for each purchase, we would use the following JOIN statement:

```
SELECT customer_table.Name, customer_purchases.*
FROM customer_purchases
JOIN customer_table
ON customer_purchases.Customer ID = customer_table.Customer ID;

```
This would retrieve the customer name and all the columns in the customer_purchases table for each purchase.

## SQL Functions

SQL provides a variety of functions for manipulating and aggregating data. Here are some common SQL functions:

**COUNT function**: The COUNT function is used to count the number of rows in a table or a group of rows based on a specific condition.

```
SELECT COUNT(*)
FROM table_name;

```
For example, to count the number of purchases in the customer_purchases table, we would use the following COUNT function:

```
SELECT COUNT(*)
FROM customer_purchases;

```
This would retrieve the total number of rows in the customer_purchases table.

**SUM function**: The SUM function is used to calculate the sum of a column or a group of rows based on a specific condition.

```
SELECT SUM(column)
FROM table_name
WHERE condition;

```
For example, to calculate the total amount spent on purchases made by customer ID 1, we would use the following SUM function:

```
SELECT SUM(Price)
FROM customer_purchases
WHERE Customer ID = 1;

```
This would retrieve the total amount spent on purchases made by customer ID 1 in the customer_purchases table.

**AVG function**: The AVG function is used to calculate the average of a column or a group of rows based on a specific condition.

```
SELECT AVG(column)
FROM table_name
WHERE condition;

```
For example, to calculate the average price of purchases made by customer ID 1, we would use the following AVG function:

```
SELECT AVG(Price)
FROM customer_purchases
WHERE Customer ID = 1;

```
This would retrieve the average price of purchases made by customer ID 1 in the customer_purchases table.

**MAX and MIN functions**: The MAX and MIN functions are used to retrieve the maximum and minimum values of a column or a group of rows based on a specific condition.

```
SELECT MAX(column)
FROM table_name
WHERE condition;

```

```
SELECT MIN(column)
FROM table_name
WHERE condition;

```
For example, to retrieve the highest and lowest prices of purchases made by customer ID 1, we would use the following MAX and MIN functions:

```
SELECT MAX(Price), MIN(Price)
FROM customer_purchases
WHERE Customer ID = 1;

```
This would retrieve the highest and lowest prices of purchases made by customer ID 1 in the customer_purchases table.

## SQL Data Manipulation

SQL provides a variety of tools for manipulating data in tables. Here are some common SQL data manipulation statements:

**INSERT statement**: The INSERT statement is used to insert new data into a table.

```
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

```
For example, to insert a new purchase into the customer_purchases table, we would use the following INSERT statement:

```
INSERT INTO customer_purchases (Customer ID, Product ID, Purchase Date, Price)
VALUES (4, 102, '2022-01-06', 15.00);

```
This would insert a new row into the customer_purchases table with a customer ID of 4, a product ID of 102, a purchase date of January 6th, 2022, and a price of 15.00.

**UPDATE statement**: The UPDATE statement is used to update existing data in a table.

```
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

```
For example, to update the price of a purchase made by customer ID 1 on January 1st, 2022, we would use the following UPDATE statement:

```
UPDATE customer_purchases
SET Price = 12.99
WHERE Customer ID = 1 AND Purchase Date = '2022-01-01';

```
This would update the price of the purchase made by customer ID 1 on January 1st, 2022 to 12.99.

**DELETE statement**: The DELETE statement is used to delete existing data from a table.

```
DELETE FROM table_name
WHERE condition;

```
For example, to delete all purchases made by customer ID 3, we would use the following DELETE statement:

```
DELETE FROM customer_purchases
WHERE Customer ID = 3;

```
This would delete all rows from the customer_purchases table where the customer ID is 3.

## Conclusion

SQL is an essential tool for data analysis, as it provides a powerful and efficient way to manipulate and analyze data stored in relational databases. In this article, we have covered the basics of SQL, including creating and querying tables, filtering data with the WHERE clause, and using SQL functions to aggregate data. We have also discussed SQL data manipulation statements, including INSERT, UPDATE, and DELETE. By mastering these SQL concepts, you will be well on your way to becoming a proficient data analyst.