SQL Master
SQL Master
Github Repo
https://github.com/SahilGogna/SQL-MyStrivera
Before diving into SQL queries, it is essential to understand the foundational concepts of
databases:
● What is a SQL Client? A SQL client is a tool or software used to connect to and
interact with databases using SQL.
2. SQL Fundamentals
Understanding and practicing these core concepts is essential for building a strong SQL
foundation:
● 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.
● 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.
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
Using pgAdmin
Ensure the result matches the expected number of rows in the dataset which is 1000
records.
10. Show all customers who have more than 500 loyalty points.
12. Identify the most popular product category based on quantity sold.
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;
Only limited slots are available per week. Book now to secure your spot! 📈💡
---------