0% found this document useful (0 votes)
6 views10 pages

SQL Master

The document is a comprehensive SQL guide that includes top free SQL learning websites, a GitHub repository, and a detailed outline of SQL fundamentals, intermediate concepts, and practical applications. It also provides a user guide for setting up a Retail Sales Dataset, including steps for creating a database, importing data, and verifying it, along with SQL queries to solve various analytical questions. Additionally, it promotes a mentorship program for career advancement in Canada.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

SQL Master

The document is a comprehensive SQL guide that includes top free SQL learning websites, a GitHub repository, and a detailed outline of SQL fundamentals, intermediate concepts, and practical applications. It also provides a user guide for setting up a Retail Sales Dataset, including steps for creating a database, importing data, and verifying it, along with SQL queries to solve various analytical questions. Additionally, it promotes a mentorship program for career advancement in Canada.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SQL Guide

Top Free SQL Learning Websites


1.​ https://sqlbolt.com/
2.​ https://sqlzoo.net/wiki/SELECT_basics
3.​ https://mode.com/sql-tutorial
4.​ https://sql-island.informatik.uni-kl.de/#​

Github Repo
https://github.com/SahilGogna/SQL-MyStrivera

Comprehensive Guide to Learning


SQL
1. Database Fundamentals

Before diving into SQL queries, it is essential to understand the foundational concepts of
databases:

●​ What is a Database (DB)? A database is an organized collection of data stored and


accessed electronically. It allows efficient data retrieval and management.​

●​ What is SQL? SQL (Structured Query Language) is a programming language used


to interact with databases. It helps perform operations like retrieving, updating, or
managing data.​

●​ What is a Query? A query is a set of instructions written in SQL to communicate with


a database and perform specific tasks, such as retrieving data.​

●​ What is a Database Management System (DBMS)? DBMS is software that


provides an interface to manage databases, allowing users to store, retrieve, and
manipulate data.​

●​ What is a Relational Database Management System (RDBMS)? RDBMS is a type


of DBMS that organizes data into tables with rows and columns, ensuring data
relationships are maintained.​

●​ What is a SQL Client? A SQL client is a tool or software used to connect to and
interact with databases using SQL.​

●​ What is Normalization? Normalization is the process of organizing data in a


database to reduce redundancy and improve data integrity.​

●​ Key Database Connection Information: To connect to a database, you need:


○​ Hostname: The address of the database server.
○​ Username: Your login ID for the database.
○​ Database name: The specific database to connect to.
○​ Password: The authentication key.
○​ Port: The communication endpoint.​


2. SQL Fundamentals

Understanding and practicing these core concepts is essential for building a strong SQL
foundation:

●​ Basic SQL Commands:


○​ SELECT Statement: Used to retrieve data from a database.
○​ WHERE Clause: Filters records based on conditions.
○​ CRUD Operations:
■​ Create: Add new records.
■​ Read: Retrieve records.
■​ Update: Modify existing records.
■​ Delete: Remove records.
●​ ORDER BY Clause: Allows sorting of data in ascending or descending order.
●​ Joins: Combine data from multiple tables based on a related column:
○​ Inner Join
○​ Left Join
○​ Right Join
○​ Outer Join
○​ Self Join
●​ Aggregation Functions:
○​ SUM: Adds up numeric data.
○​ COUNT: Counts the number of rows.
○​ AVG: Calculates the average.
○​ GROUP BY: Groups rows sharing a property and performs aggregate
functions.
●​ Subqueries: Nested queries used within another SQL query.​
3. Intermediate Concepts

To deepen your SQL knowledge, focus on the following topics:

●​ Window Functions: Perform calculations across a set of table rows related to the
current row.
●​ Common Table Expressions (CTEs): Temporary result sets defined within a SQL
query for better readability and reusability.
●​ Indexes and Optimization: Improve query performance by indexing frequently
accessed columns.

4. Practical Applications

Practical usage of SQL can accelerate learning and provide valuable insights. Examples:

●​ Download your credit card transactions and analyze spending patterns using SQL
queries.
●​ Work on small SQL projects to solve real-world problems, such as sales analysis or
inventory tracking.

5. How Much SQL is Enough?

●​ Less Technical Roles: Focus on beginner concepts like basic queries, CRUD
operations, and aggregation functions.
●​ Data Roles: Build proficiency in intermediate and advanced topics like joins, window
functions, and optimization techniques.
●​ Preparation for Interviews: Regularly practice SQL interview questions to reinforce
concepts and improve problem-solving skills.

Top SQL Youtube Channels

1.​ QAFox -
https://www.youtube.com/playlist?list=PLsjUcU8CQXGFFAhJI6qTA8owv3z9jBbpd
2.​ Kudvenkat- https://www.youtube.com/playlist?list=PL08903FB7ACA1C2FB
3.​ Techtfq Basic Concepts:
https://www.youtube.com/playlist?list=PLavw5C92dz9HQQ_COgGb7kf_1H8UWUBx
O
4.​ Techtfq Intermediate Concepts:
https://www.youtube.com/playlist?list=PLavw5C92dz9FD9XspliRM_HZM_jK7tkii
5.​ Techtfq Advanced Concepts:
https://www.youtube.com/playlist?list=PLavw5C92dz9GbmgiW4TWVnxhjMFOIf0Q7
6.​ ELearning Bridge: https://www.youtube.com/@shashank_mishra/playlists
7.​ Ankit Bansal: https://www.youtube.com/@ankitbansal6/playlists​

Top SQL Learning Websites


1.​ https://sqlbolt.com/
2.​ https://sqlzoo.net/wiki/SELECT_basics
3.​ https://mode.com/sql-tutorial
4.​ https://sql-island.informatik.uni-kl.de/#​

User Guide: Setting Up the Retail Sales


Dataset
This guide provides step-by-step instructions for creating a database table and importing the
Retail Sales Dataset using SQL.

1. Setting Up the Database


1.1 Install PostgreSQL and pgAdmin

●​ Download and install PostgreSQL from postgresql.org.


●​ Install pgAdmin for managing the database via a graphical interface.

1.2 Connect to the Database

1.​ Open pgAdmin and log in with your PostgreSQL credentials.


2.​ Create a new database:
○​ Right-click on "Databases" > "Create" > "Database."
○​ Enter a name (e.g., RetailSalesDB) and save.

2. Creating the Table


2.1 Define the Table Schema

Run the following SQL query in pgAdmin to create the table:


CREATE TABLE RetailSales (​
TransactionID INT PRIMARY KEY,​
Date DATE NOT NULL,​
CustomerID VARCHAR(50) NOT NULL,​
Gender VARCHAR(10) NOT NULL,​
Age INT NOT NULL,​
ProductCategory VARCHAR(100) NOT NULL,​
Quantity INT NOT NULL,​
PricePerUnit INT NOT NULL,​
TotalAmount INT NOT NULL​
);

●​ TransactionID: Unique identifier for each transaction.


●​ Date: The date of the transaction in YYYY-MM-DD format.
●​ CustomerID: Unique identifier for each customer.
●​ Gender: Gender of the customer (e.g., Male, Female).
●​ Age: Age of the customer in years.
●​ ProductCategory: Category of the product purchased.
●​ Quantity: Number of units purchased.
●​ PricePerUnit: Price of one unit of the product.
●​ TotalAmount: Total cost of the transaction.

3. Importing the Dataset


3.1 Prepare the Dataset

1.​ Download the dataset from this link -


https://drive.google.com/file/d/1OyUpe_EPnNvg9RCX7gHuHci2w7dS5chb/view?usp
=sharing and place it in an accessible location (e.g.,
/path/to/retail_sales_dataset.csv).

3.2 Import the Data

Using pgAdmin

1.​ Open the Query Tool in pgAdmin.


2.​ Run the following SQL command to import the dataset:

COPY RetailSales FROM '/path/to/retail_sales_dataset.csv' DELIMITER ','


CSV HEADER;

●​ Replace /path/to/retail_sales_dataset.csv with the actual path to the file.


●​ DELIMITER ',': Specifies that the file is comma-separated.
●​ CSV HEADER: Skips the first row (header).

4. Verifying the Data


Run the following query in the Query Tool to verify the number of rows imported:

SELECT COUNT(*) FROM RetailSales;

Ensure the result matches the expected number of rows in the dataset which is 1000
records.

5. Explore the dataset


After you are finished importing the dataset, don’t stop there. Try to just explore the dataset
and try to understand the business value, the impact it can create and what kind of questions
can be answered using the dataset?

6. Creating a new table to demonstrate joins

-- 1. Create a secondary table for demonstrating joins​


CREATE TABLE customer_details (​
customer_id VARCHAR(10) PRIMARY KEY,​
customer_name VARCHAR(50),​
city VARCHAR(50),​
loyalty_points INT​
);​

-- 2. Insert sample data into the secondary table​
INSERT INTO customer_details (customer_id, customer_name, city,
loyalty_points) VALUES​
('CUST001', 'John Doe', 'Toronto', 500),​
('CUST002', 'Jane Smith', 'Montreal', 1200),​
('CUST003', 'Robert Brown', 'Vancouver', 300),​
('CUST004', 'Michael Davis', 'Calgary', 800),​
('CUST005', 'William Wilson', 'Ottawa', 150),​
('CUST006', 'Emily Johnson', 'Edmonton', 950),​
('CUST007', 'Sophia Martinez', 'Winnipeg', 1100),​
('CUST008', 'Olivia Anderson', 'Quebec City', 600),​
('CUST009', 'James Taylor', 'Halifax', 700),​
('CUST010', 'Emma Moore', 'Victoria', 400);

7. Solve these questions


1. Find the total sales amount for each product category.

2. Which city has the highest total loyalty points?

3. Find the top 3 customers who spent the most.

4. Calculate the average age of customers by gender.

5. Retrieve all transactions where customers spent more than 1000.

6. List customers and their loyalty points for all transactions.

7. What is the average quantity sold per product category?

8. Find the total sales amount for each city.

9. Which product category has the highest average unit price?

10. Show all customers who have more than 500 loyalty points.

11. Rank customers by total spending.

12. Identify the most popular product category based on quantity sold.

13. Find the cumulative sales for each customer.

14. Calculate the difference in loyalty points between the highest and lowest in each city.

15. Retrieve customers who made purchases in more than 1 product category.
8. Solutions

-- Question 1: Find the total sales amount for each product category.​
SELECT product_category, SUM(total_amount) AS total_sales​
FROM retail_sales​
GROUP BY product_category;​

-- Question 2: Which city has the highest total loyalty points?​
SELECT city, SUM(loyalty_points) AS total_loyalty_points​
FROM customer_details​
GROUP BY city​
ORDER BY total_loyalty_points DESC​
LIMIT 1;​

-- Question 3: Find the top 3 customers who spent the most.​
SELECT customer_id, SUM(total_amount) AS total_spent​
FROM retail_sales​
GROUP BY customer_id​
ORDER BY total_spent DESC​
LIMIT 3;​

-- Question 4: Calculate the average age of customers by gender.​
SELECT gender, AVG(age) AS avg_age​
FROM retail_sales​
GROUP BY gender;​

-- Question 5: Retrieve all transactions where customers spent more than
1000.​
SELECT *​
FROM retail_sales​
WHERE total_amount > 1000;​

-- Question 6: List customers and their loyalty points for all
transactions.​
SELECT rs.customer_id, rs.total_amount, cd.loyalty_points​
FROM retail_sales rs​
LEFT JOIN customer_details cd​
ON rs.customer_id = cd.customer_id;​

-- Question 7: What is the average quantity sold per product category?​
SELECT product_category, AVG(quantity) AS avg_quantity​
FROM retail_sales​
GROUP BY product_category;​

-- Question 8: Find the total sales amount for each city.​
SELECT cd.city, SUM(rs.total_amount) AS city_sales​
FROM retail_sales rs​
JOIN customer_details cd​
ON rs.customer_id = cd.customer_id​
GROUP BY cd.city;​

-- Question 9: Which product category has the highest average unit
price?​
SELECT product_category, AVG(price_per_unit) AS avg_unit_price​
FROM retail_sales​
GROUP BY product_category​
ORDER BY avg_unit_price DESC​
LIMIT 1;​

-- Question 10: Show all customers who have more than 500 loyalty
points.​
SELECT *​
FROM customer_details​
WHERE loyalty_points > 500;​

-- Question 11: Rank customers by total spending.​
SELECT customer_id, SUM(total_amount) AS total_spent,​
RANK() OVER (ORDER BY SUM(total_amount) DESC) AS rank​
FROM retail_sales​
GROUP BY customer_id;​

-- Question 12: Identify the most popular product category based on
quantity sold.​
SELECT product_category, SUM(quantity) AS total_quantity​
FROM retail_sales​
GROUP BY product_category​
ORDER BY total_quantity DESC​
LIMIT 1;​

-- Question 13: Find the cumulative sales for each customer.​
SELECT customer_id, total_amount,​
SUM(total_amount) OVER (PARTITION BY customer_id ORDER BY
transaction_id) AS cumulative_sales​
FROM retail_sales;​

-- Question 14: Calculate the difference in loyalty points between the
highest and lowest in each city.​
SELECT city, MAX(loyalty_points) - MIN(loyalty_points) AS points_diff​
FROM customer_details​
GROUP BY city;​

-- Question 15: Retrieve customers who made purchases in more than 1
product category.​
SELECT customer_id​
FROM retail_sales​
GROUP BY customer_id​
HAVING COUNT(DISTINCT product_category) > 1;

🌟 Mentorship Program by Sahil Gogna:


Are you ready to advance your career in Canada? With 4 years of experience in the data industry and 2
years of experience as a senior data engineer in one of the top banks in Canada, I am here to offer
personalized 1:1 consultancy calls tailored to guide your career journey in Canada.

Only limited slots are available per week. Book now to secure your spot! 📈💡
---------

You might also like