Top 10 Advanced SQL Queries for Data Mastery
1. Recursive CTE (Common Table Expressions)
Use a recursive CTE to traverse hierarchical data, such as employees and their managers.
2. Pivoting Data
Turn row data into columns (e.g., show product categories as separate columns).
3. Window Functions
Calculate a running total of sales based on order date.
4. Ranking with Window Functions
Rank employees’ salaries within each department.
5. Finding Gaps in Sequences
Identify missing values in a sequential dataset (e.g., order numbers).
6. Unpivoting Data
Convert columns into rows to simplify analysis of multiple attributes.
7. Finding Consecutive Events
Check for consecutive days/orders for the same product using
8. Aggregation with the FILTER Clause
Calculate selective averages (e.g., only for the Sales department).
9. JSON Data Extraction
Extract values from JSON columns directly in SQL.
10. Using Temporary Tables
Create a temporary table for intermediate results, then join it with other tables.
Why These Matter
Advanced SQL queries let you handle complex data manipulation and analysis tasks with ease. From traversing hierarchical relationships to reshaping data (pivot/unpivot) and working with JSON, these techniques expand your ability to derive insights from relational databases.
Keep practicing these queries to solidify your SQL expertise and make more data-driven decisions!
#sql #dataanalyst
1. Recursive CTE (Common Table Expressions)
Use a recursive CTE to traverse hierarchical data, such as employees and their managers.
WITH RECURSIVE EmployeeHierarchy AS (
SELECT employee_id, employee_name, manager_id
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT e.employee_id, e.employee_name, e.manager_id
FROM employees e
JOIN EmployeeHierarchy eh ON e.manager_id = eh.employee_id
)
SELECT *
FROM EmployeeHierarchy;
2. Pivoting Data
Turn row data into columns (e.g., show product categories as separate columns).
SELECT *
FROM (
SELECT TO_CHAR(order_date, 'YYYY-MM') AS month, product_category, sales_amount
FROM sales
) AS pivot_data
PIVOT (
SUM(sales_amount)
FOR product_category IN ('Electronics', 'Clothing', 'Books')
) AS pivoted_sales;
3. Window Functions
Calculate a running total of sales based on order date.
SELECT
order_date,
sales_amount,
SUM(sales_amount) OVER (ORDER BY order_date) AS running_total
FROM sales;
4. Ranking with Window Functions
Rank employees’ salaries within each department.
SELECT
department,
employee_name,
salary,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank
FROM employees;
5. Finding Gaps in Sequences
Identify missing values in a sequential dataset (e.g., order numbers).
WITH Sequences AS (
SELECT MIN(order_number) AS start_seq, MAX(order_number) AS end_seq
FROM orders
)
SELECT start_seq + 1 AS missing_sequence
FROM Sequences
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.order_number = Sequences.start_seq + 1
);
6. Unpivoting Data
Convert columns into rows to simplify analysis of multiple attributes.
SELECT
product_id,
attribute_name,
attribute_value
FROM products
UNPIVOT (
attribute_value FOR attribute_name IN (color, size, weight)
) AS unpivoted_data;
7. Finding Consecutive Events
Check for consecutive days/orders for the same product using
LAG().WITH ConsecutiveOrders AS (
SELECT
product_id,
order_date,
LAG(order_date) OVER (PARTITION BY product_id ORDER BY order_date) AS prev_order_date
FROM orders
)
SELECT product_id, order_date, prev_order_date
FROM ConsecutiveOrders
WHERE order_date - prev_order_date = 1;
8. Aggregation with the FILTER Clause
Calculate selective averages (e.g., only for the Sales department).
SELECT
department,
AVG(salary) FILTER (WHERE department = 'Sales') AS avg_salary_sales
FROM employees
GROUP BY department;
9. JSON Data Extraction
Extract values from JSON columns directly in SQL.
SELECT
order_id,
customer_id,
order_details ->> 'product' AS product_name,
CAST(order_details ->> 'quantity' AS INTEGER) AS quantity
FROM orders;
10. Using Temporary Tables
Create a temporary table for intermediate results, then join it with other tables.
-- Create a temporary table
CREATE TEMPORARY TABLE temp_product_sales AS
SELECT product_id, SUM(sales_amount) AS total_sales
FROM sales
GROUP BY product_id;
-- Use the temp table
SELECT p.product_name, t.total_sales
FROM products p
JOIN temp_product_sales t ON p.product_id = t.product_id;
Why These Matter
Advanced SQL queries let you handle complex data manipulation and analysis tasks with ease. From traversing hierarchical relationships to reshaping data (pivot/unpivot) and working with JSON, these techniques expand your ability to derive insights from relational databases.
Keep practicing these queries to solidify your SQL expertise and make more data-driven decisions!
#sql #dataanalyst
👍7
Please open Telegram to view this post
VIEW IN TELEGRAM
👍8
What fundamental axioms and unchangeable principles exist in data engineering and data modeling?
Consider Euclidean geometry as an example. It's an axiomatic system, built on universal "true statements" that define the entire field. For instance, "a line can be drawn between any two points" or "all right angles are equal." From these basic axioms, all other geometric principles can be derived.
So, what are the axioms of data engineering and data modeling?
I asked ChatGPT about that and it gave this list:
▪️ Data exists in multiple forms and formats
▪️ Data can and should be transformed to serve the needs
▪️ Data should be trustworthy
▪️ Data systems should be efficient and scalable
Classic ChatGPT, pretty standard, pretty boring 🥱. Yes, these are universal and fundamental rules, but what can we learn from them?
Here is what I'd call axioms for myself:
🔹 Every table should have a primary key which is unique and not empty (dbt tests for life 🙂)
🔹 Every column should have strong types and constraints (storing data as STRING or JSON is ouch)
🔹 Data pipelines should be idempotent (I don't want to deal with duplicates and inconsistencies)
🔹 Every data transformation has to be defined in code (otherwise what are we doing here)
Now it's your turn: what principles would you defend at all costs? 🤔
Consider Euclidean geometry as an example. It's an axiomatic system, built on universal "true statements" that define the entire field. For instance, "a line can be drawn between any two points" or "all right angles are equal." From these basic axioms, all other geometric principles can be derived.
So, what are the axioms of data engineering and data modeling?
I asked ChatGPT about that and it gave this list:
▪️ Data exists in multiple forms and formats
▪️ Data can and should be transformed to serve the needs
▪️ Data should be trustworthy
▪️ Data systems should be efficient and scalable
Classic ChatGPT, pretty standard, pretty boring 🥱. Yes, these are universal and fundamental rules, but what can we learn from them?
Here is what I'd call axioms for myself:
🔹 Every table should have a primary key which is unique and not empty (dbt tests for life 🙂)
🔹 Every column should have strong types and constraints (storing data as STRING or JSON is ouch)
🔹 Data pipelines should be idempotent (I don't want to deal with duplicates and inconsistencies)
🔹 Every data transformation has to be defined in code (otherwise what are we doing here)
Now it's your turn: what principles would you defend at all costs? 🤔
❤7
🪄 Covering the Entire Data Set with ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING 🪄
When you need every row in your partition to “know” the complete picture, use this powerful window frame:
What’s Happening?
This clause tells SQL to consider all rows in the partition—ignoring the current row’s position entirely. It’s perfect for calculations that need a partition-wide perspective, such as computing overall averages, totals, or even min/max values for each group.
Why Use It?
👉 Comprehensive Analysis: Every row in the partition has access to the entire group's data, which is ideal when you need to compare each record against a complete metric.
👉 Consistency: It ensures that each row in the partition uses the same aggregate value, even if you’re applying an ORDER BY clause elsewhere.
For example, without an ORDER BY clause, functions like COUNT(event_name) OVER (PARTITION BY session_id) will return the full count for that session. However, if you add an ORDER BY clause, SQL might default to a running count (using a frame like RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). Specifying ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ensures you always get the full count regardless of ordering.
When to Use It:
👉 Group-Wide Metrics: When you want to display a group’s total or average alongside each individual record.
👉 Data Normalisation: When you need to calculate percentages or deviations relative to the entire group.
I'll say again that you can use a number instead of UNBOUNDED, you may have a use case when you want to return results for the 5 page views prior and 5 page views after current row for instance.
Have you used partition-wide window frames in your projects? Share your success stories or questions below!
When you need every row in your partition to “know” the complete picture, use this powerful window frame:
AVG(amount) OVER (PARTITION BY category
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS partition_avg
What’s Happening?
This clause tells SQL to consider all rows in the partition—ignoring the current row’s position entirely. It’s perfect for calculations that need a partition-wide perspective, such as computing overall averages, totals, or even min/max values for each group.
Why Use It?
👉 Comprehensive Analysis: Every row in the partition has access to the entire group's data, which is ideal when you need to compare each record against a complete metric.
👉 Consistency: It ensures that each row in the partition uses the same aggregate value, even if you’re applying an ORDER BY clause elsewhere.
For example, without an ORDER BY clause, functions like COUNT(event_name) OVER (PARTITION BY session_id) will return the full count for that session. However, if you add an ORDER BY clause, SQL might default to a running count (using a frame like RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). Specifying ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ensures you always get the full count regardless of ordering.
When to Use It:
👉 Group-Wide Metrics: When you want to display a group’s total or average alongside each individual record.
👉 Data Normalisation: When you need to calculate percentages or deviations relative to the entire group.
I'll say again that you can use a number instead of UNBOUNDED, you may have a use case when you want to return results for the 5 page views prior and 5 page views after current row for instance.
Have you used partition-wide window frames in your projects? Share your success stories or questions below!
👍3
Here's one of the most useful SQL keywords you've never heard of:
GROUP BY CUBE
It's useful when for performing aggregate analysis when you want to group by more than one column.
Think of GROUP BY CUBE as the ultimate grouping keyword. It essentially contains GROUP BY ROLLUP and GROUP BY GROUPING SETS inside of it.
How does it work?
Take our example dataset: the Citibike trips dataset. This data contains Citibike trips taken by riders in NYC.
Let's say you're grouping by three different columns: year, month, and station name. You're doing a COUNT() of trips taken.
What are all the possible groupings we could come up with?
1. Total trips overall
2. Trips from each station
3. Trips in each month
4. Trips in each year
5. Trips in each month + each station
6. Trips in each year + each station
7. Trips in each year + each month
8. Trips in each year + each month + each station
GROUP BY CUBE will perform all of these grouping combinations for you automatically. Without GROUP BY CUBE, you'd need to write multiple queries and UNION the results together!
The image below shows what this looks like in actual code.
Usage is super simple. All you need to do is type GROUP BY CUBE along with the names of the columns you'd like to group by.
#sql #groupby
GROUP BY CUBE
It's useful when for performing aggregate analysis when you want to group by more than one column.
Think of GROUP BY CUBE as the ultimate grouping keyword. It essentially contains GROUP BY ROLLUP and GROUP BY GROUPING SETS inside of it.
How does it work?
Take our example dataset: the Citibike trips dataset. This data contains Citibike trips taken by riders in NYC.
Let's say you're grouping by three different columns: year, month, and station name. You're doing a COUNT() of trips taken.
What are all the possible groupings we could come up with?
1. Total trips overall
2. Trips from each station
3. Trips in each month
4. Trips in each year
5. Trips in each month + each station
6. Trips in each year + each station
7. Trips in each year + each month
8. Trips in each year + each month + each station
GROUP BY CUBE will perform all of these grouping combinations for you automatically. Without GROUP BY CUBE, you'd need to write multiple queries and UNION the results together!
The image below shows what this looks like in actual code.
Usage is super simple. All you need to do is type GROUP BY CUBE along with the names of the columns you'd like to group by.
#sql #groupby
👍10
💫 Monetizing Your Data Analytics Skills: Side Hustles & Passive Income Streams
Once you've mastered data analytics, you can leverage your expertise to generate income beyond your 9-to-5 job. Here’s how:
1️⃣ Freelancing & Consulting 💼
Offer data analytics, visualization, or SQL expertise on platforms like Upwork, Fiverr, and Toptal.
Provide business intelligence solutions, dashboard building, or data cleaning services.
Work with startups, small businesses, and enterprises remotely.
2️⃣ Creating & Selling Online Courses 🎥
Teach SQL, Power BI, Python, or Data Visualization on platforms like Udemy, Coursera, and Teachable.
Offer exclusive workshops or bootcamps via LinkedIn, Gumroad, or your website.
Monetize your expertise once and earn passive income forever.
3️⃣ Blogging & Technical Writing ✍️
Write data-related articles on Medium, Towards Data Science, or Substack.
Start a newsletter focused on analytics trends and career growth.
Earn through Medium Partner Program, sponsored posts, or affiliate marketing.
4️⃣ YouTube & Social Media Monetization 📹
Create a YouTube channel sharing tutorials on SQL, Power BI, Python, and real-world analytics projects.
Monetize through ads, sponsorships, and memberships.
Grow a LinkedIn, Twitter, or Instagram audience and collaborate with brands.
5️⃣ Affiliate Marketing in Data Analytics 🔗
Promote courses, books, tools (Tableau, Power BI, Python IDEs) and earn commissions.
Join Udemy, Coursera, or DataCamp affiliate programs.
Recommend data tools, laptops, or online learning resources through blogs or YouTube.
6️⃣ Selling Templates & Dashboards 📊
Create Power BI or Tableau templates and sell them on Gumroad or Etsy.
Offer SQL query libraries, Excel automation scripts, or data storytelling templates.
Provide customized analytics solutions for different industries.
7️⃣ Writing E-books or Guides 📖
Publish an e-book on SQL, Power BI, or breaking into data analytics.
Sell through Amazon Kindle, Gumroad, or your website.
Provide case studies, real-world datasets, and practice problems.
8️⃣ Building a Subscription-Based Community 🌍
Create a private Slack, Discord, or Telegram group for data professionals.
Charge for premium access, mentorship, and exclusive content.
Offer live Q&A sessions, job referrals, and networking opportunities.
9️⃣ Developing & Selling AI-Powered Tools 🤖
Build Python scripts, automation tools, or AI-powered analytics apps.
Sell on GitHub, Gumroad, or AppSumo.
Offer API-based solutions for businesses needing automated insights.
🔟 Landing Paid Speaking Engagements & Workshops 🎤
Speak at data conferences, webinars, and corporate training events.
Offer paid workshops for businesses or universities.
Become a recognized expert in your niche and command high fees.
Start Small, Scale Fast! 🚀
The data analytics field offers endless opportunities to earn beyond a job. Start with freelancing, content creation, or digital products—then scale it into a business!
Hope it helps :)
#dataanalytics #sql
Once you've mastered data analytics, you can leverage your expertise to generate income beyond your 9-to-5 job. Here’s how:
1️⃣ Freelancing & Consulting 💼
Offer data analytics, visualization, or SQL expertise on platforms like Upwork, Fiverr, and Toptal.
Provide business intelligence solutions, dashboard building, or data cleaning services.
Work with startups, small businesses, and enterprises remotely.
2️⃣ Creating & Selling Online Courses 🎥
Teach SQL, Power BI, Python, or Data Visualization on platforms like Udemy, Coursera, and Teachable.
Offer exclusive workshops or bootcamps via LinkedIn, Gumroad, or your website.
Monetize your expertise once and earn passive income forever.
3️⃣ Blogging & Technical Writing ✍️
Write data-related articles on Medium, Towards Data Science, or Substack.
Start a newsletter focused on analytics trends and career growth.
Earn through Medium Partner Program, sponsored posts, or affiliate marketing.
4️⃣ YouTube & Social Media Monetization 📹
Create a YouTube channel sharing tutorials on SQL, Power BI, Python, and real-world analytics projects.
Monetize through ads, sponsorships, and memberships.
Grow a LinkedIn, Twitter, or Instagram audience and collaborate with brands.
5️⃣ Affiliate Marketing in Data Analytics 🔗
Promote courses, books, tools (Tableau, Power BI, Python IDEs) and earn commissions.
Join Udemy, Coursera, or DataCamp affiliate programs.
Recommend data tools, laptops, or online learning resources through blogs or YouTube.
6️⃣ Selling Templates & Dashboards 📊
Create Power BI or Tableau templates and sell them on Gumroad or Etsy.
Offer SQL query libraries, Excel automation scripts, or data storytelling templates.
Provide customized analytics solutions for different industries.
7️⃣ Writing E-books or Guides 📖
Publish an e-book on SQL, Power BI, or breaking into data analytics.
Sell through Amazon Kindle, Gumroad, or your website.
Provide case studies, real-world datasets, and practice problems.
8️⃣ Building a Subscription-Based Community 🌍
Create a private Slack, Discord, or Telegram group for data professionals.
Charge for premium access, mentorship, and exclusive content.
Offer live Q&A sessions, job referrals, and networking opportunities.
9️⃣ Developing & Selling AI-Powered Tools 🤖
Build Python scripts, automation tools, or AI-powered analytics apps.
Sell on GitHub, Gumroad, or AppSumo.
Offer API-based solutions for businesses needing automated insights.
🔟 Landing Paid Speaking Engagements & Workshops 🎤
Speak at data conferences, webinars, and corporate training events.
Offer paid workshops for businesses or universities.
Become a recognized expert in your niche and command high fees.
Start Small, Scale Fast! 🚀
The data analytics field offers endless opportunities to earn beyond a job. Start with freelancing, content creation, or digital products—then scale it into a business!
Hope it helps :)
#dataanalytics #sql
👍7
📊 Decoding Business Metrics: Median vs. Average! 🧐
Ever wondered whether to use the average (mean) or the median when presenting your business data? 🤔
While the average is a common go-to (add everything up and divide by the count 🔢), it can sometimes paint a misleading picture, especially when your data has some serious outliers! 😬
Think about it: Imagine a company's sales team where a few top performers have HUGE numbers, while the majority have more moderate results. The average might be inflated by those high-flyers, not truly reflecting the typical sales performance. 📈
➡️ That's where the median shines! ✨ The median is the middle value when your data is ordered. It's not affected by extreme highs or lows, giving you a more accurate representation of the "typical" data point. 🎯
When might the median be your MVP? 👇
* 💰 Salary distributions: A few executive salaries can heavily skew the average. The median gives a better sense of the typical employee's earnings.
* 🏘️ Real estate prices: One or two ultra-luxury homes won't drastically impact the median price in an area.
* 🛒 Customer spending: A few very large purchases might inflate the average order value, while the median shows the more common spending amount.
So, next time you're presenting business metrics, take a moment to consider your data's distribution. If you suspect skewness, the median might be your secret weapon for a more truthful and insightful representation! 💡
#dataanalyst
Ever wondered whether to use the average (mean) or the median when presenting your business data? 🤔
While the average is a common go-to (add everything up and divide by the count 🔢), it can sometimes paint a misleading picture, especially when your data has some serious outliers! 😬
Think about it: Imagine a company's sales team where a few top performers have HUGE numbers, while the majority have more moderate results. The average might be inflated by those high-flyers, not truly reflecting the typical sales performance. 📈
➡️ That's where the median shines! ✨ The median is the middle value when your data is ordered. It's not affected by extreme highs or lows, giving you a more accurate representation of the "typical" data point. 🎯
When might the median be your MVP? 👇
* 💰 Salary distributions: A few executive salaries can heavily skew the average. The median gives a better sense of the typical employee's earnings.
* 🏘️ Real estate prices: One or two ultra-luxury homes won't drastically impact the median price in an area.
* 🛒 Customer spending: A few very large purchases might inflate the average order value, while the median shows the more common spending amount.
So, next time you're presenting business metrics, take a moment to consider your data's distribution. If you suspect skewness, the median might be your secret weapon for a more truthful and insightful representation! 💡
#dataanalyst
👍6
🎯 How do you grow from Junior to Senior as an Analytics Engineer?
It’s not just about writing better SQL — it’s about evolving your mindset and ownership at each level.
Here's a 3-stage roadmap I use to break it down:
🔹 Level 1: Junior
Learn foundational tools, build confidence, and write clean code with guidance.
🔸 Level 2: Mid-Level
Own pipelines and metrics, design reliable dbt models, and support business analysts.
🔴 Level 3: Senior
Architect scalable systems, champion data quality, and shape the team’s data strategy.
#sql #dataanalyst #Analytics
It’s not just about writing better SQL — it’s about evolving your mindset and ownership at each level.
Here's a 3-stage roadmap I use to break it down:
🔹 Level 1: Junior
Learn foundational tools, build confidence, and write clean code with guidance.
🔸 Level 2: Mid-Level
Own pipelines and metrics, design reliable dbt models, and support business analysts.
🔴 Level 3: Senior
Architect scalable systems, champion data quality, and shape the team’s data strategy.
#sql #dataanalyst #Analytics
❤4🔥4👍1
SQL Interview Questions
1. How would you find duplicate records in SQL?
2.What are various types of SQL joins?
3.What is a trigger in SQL?
4.What are different DDL,DML commands in SQL?
5.What is difference between Delete, Drop and Truncate?
6.What is difference between Union and Union all?
7.Which command give Unique values?
8. What is the difference between Where and Having Clause?
9.Give the execution of keywords in SQL?
10. What is difference between IN and BETWEEN Operator?
11. What is primary and Foreign key?
12. What is an aggregate Functions?
13. What is the difference between Rank and Dense Rank?
14. List the ACID Properties and explain what they are?
15. What is the difference between % and _ in like operator?
16. What does CTE stands for?
17. What is database?what is DBMS?What is RDMS?
18.What is Alias in SQL?
19. What is Normalisation?Describe various form?
20. How do you sort the results of a query?
21. Explain the types of Window functions?
22. What is limit and offset?
23. What is candidate key?
24. Describe various types of Alter command?
25. What is Cartesian product?
Like this post if you need more content like this ❤️
1. How would you find duplicate records in SQL?
2.What are various types of SQL joins?
3.What is a trigger in SQL?
4.What are different DDL,DML commands in SQL?
5.What is difference between Delete, Drop and Truncate?
6.What is difference between Union and Union all?
7.Which command give Unique values?
8. What is the difference between Where and Having Clause?
9.Give the execution of keywords in SQL?
10. What is difference between IN and BETWEEN Operator?
11. What is primary and Foreign key?
12. What is an aggregate Functions?
13. What is the difference between Rank and Dense Rank?
14. List the ACID Properties and explain what they are?
15. What is the difference between % and _ in like operator?
16. What does CTE stands for?
17. What is database?what is DBMS?What is RDMS?
18.What is Alias in SQL?
19. What is Normalisation?Describe various form?
20. How do you sort the results of a query?
21. Explain the types of Window functions?
22. What is limit and offset?
23. What is candidate key?
24. Describe various types of Alter command?
25. What is Cartesian product?
Like this post if you need more content like this ❤️
❤4👍4🔥2
📌 SQL Cheatsheet — Quick Reference Guide
Whether you’re just starting out with databases or you need a handy reminder while coding, keep this sheet in your pocket and query with confidence!
---
Whether you’re just starting out with databases or you need a handy reminder while coding, keep this sheet in your pocket and query with confidence!
---
-- 1️⃣ Database Basics
CREATE DATABASE db_name;
USE db_name;
-- 2️⃣ Tables
CREATE TABLE table_name (col1 datatype, col2 datatype);
DROP TABLE table_name;
ALTER TABLE table_name ADD column_name datatype;
-- 3️⃣ Insert Data
INSERT INTO table_name (col1, col2) VALUES (val1, val2);
-- 4️⃣ Select Queries
SELECT * FROM table_name;
SELECT col1, col2 FROM table_name;
SELECT * FROM table_name WHERE condition;
-- 5️⃣ Update Data
UPDATE table_name SET col1 = value1 WHERE condition;
-- 6️⃣ Delete Data
DELETE FROM table_name WHERE condition;
-- 7️⃣ Joins
SELECT * FROM table1
INNER JOIN table2 ON table1.col = table2.col;
SELECT * FROM table1
LEFT JOIN table2 ON table1.col = table2.col;
SELECT * FROM table1
RIGHT JOIN table2 ON table1.col = table2.col;
-- 8️⃣ Aggregations
SELECT COUNT(*) FROM table_name;
SELECT SUM(col) FROM table_name;
SELECT col, COUNT(*) FROM table_name GROUP BY col;
-- 9️⃣ Sorting & Limiting
SELECT * FROM table_name ORDER BY col ASC; -- or DESC
SELECT * FROM table_name LIMIT n;
-- 🔟 Indexes
CREATE INDEX idx_name ON table_name (col);
DROP INDEX idx_name;
-- 1️⃣1️⃣ Subqueries
SELECT * FROM table_name
WHERE col IN (SELECT col FROM other_table);
-- 1️⃣2️⃣ Views
CREATE VIEW view_name AS
SELECT * FROM table_name;
DROP VIEW view_name;
👍15❤4🔥1
🚀 Excel vs SQL vs Python (Pandas):
1️⃣ Filtering Data
↳ Excel: =FILTER(A2:D100, B2:B100>50) (Excel 365 users)
↳ SQL: SELECT * FROM table WHERE column > 50;
↳ Python: df_filtered = df[df['column'] > 50]
2️⃣ Sorting Data
↳ Excel: Data → Sort (or =SORT(A2:A100, 1, TRUE))
↳ SQL: SELECT * FROM table ORDER BY column ASC;
↳ Python: df_sorted = df.sort_values(by="column")
3️⃣ Counting Rows
↳ Excel: =COUNTA(A:A)
↳ SQL: SELECT COUNT(*) FROM table;
↳ Python: row_count = len(df)
4️⃣ Removing Duplicates
↳ Excel: Data → Remove Duplicates
↳ SQL: SELECT DISTINCT * FROM table;
↳ Python: df_unique = df.drop_duplicates()
5️⃣ Joining Tables
↳ Excel: Power Query → Merge Queries (or VLOOKUP/XLOOKUP)
↳ SQL: SELECT * FROM table1 JOIN table2 ON table1.id = table2.id;
↳ Python: df_merged = pd.merge(df1, df2, on="id")
6️⃣ Ranking Data
↳ Excel: =RANK.EQ(A2, $A$2:$A$100)
↳ SQL: SELECT column, RANK() OVER (ORDER BY column DESC) AS rank FROM table;
↳ Python: df["rank"] = df["column"].rank(method="min", ascending=False)
7️⃣ Moving Average Calculation
↳ Excel: =AVERAGE(B2:B4) (manually for rolling window)
↳ SQL: SELECT date, AVG(value) OVER (ORDER BY date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_avg FROM table;
↳ Python: df["moving_avg"] = df["value"].rolling(window=3).mean()
8️⃣ Running Total
↳ Excel: =SUM($B$2:B2) (drag down)
↳ SQL: SELECT date, SUM(value) OVER (ORDER BY date) AS running_total FROM table;
↳ Python: df["running_total"] = df["value"].cumsum()
1️⃣ Filtering Data
↳ Excel: =FILTER(A2:D100, B2:B100>50) (Excel 365 users)
↳ SQL: SELECT * FROM table WHERE column > 50;
↳ Python: df_filtered = df[df['column'] > 50]
2️⃣ Sorting Data
↳ Excel: Data → Sort (or =SORT(A2:A100, 1, TRUE))
↳ SQL: SELECT * FROM table ORDER BY column ASC;
↳ Python: df_sorted = df.sort_values(by="column")
3️⃣ Counting Rows
↳ Excel: =COUNTA(A:A)
↳ SQL: SELECT COUNT(*) FROM table;
↳ Python: row_count = len(df)
4️⃣ Removing Duplicates
↳ Excel: Data → Remove Duplicates
↳ SQL: SELECT DISTINCT * FROM table;
↳ Python: df_unique = df.drop_duplicates()
5️⃣ Joining Tables
↳ Excel: Power Query → Merge Queries (or VLOOKUP/XLOOKUP)
↳ SQL: SELECT * FROM table1 JOIN table2 ON table1.id = table2.id;
↳ Python: df_merged = pd.merge(df1, df2, on="id")
6️⃣ Ranking Data
↳ Excel: =RANK.EQ(A2, $A$2:$A$100)
↳ SQL: SELECT column, RANK() OVER (ORDER BY column DESC) AS rank FROM table;
↳ Python: df["rank"] = df["column"].rank(method="min", ascending=False)
7️⃣ Moving Average Calculation
↳ Excel: =AVERAGE(B2:B4) (manually for rolling window)
↳ SQL: SELECT date, AVG(value) OVER (ORDER BY date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_avg FROM table;
↳ Python: df["moving_avg"] = df["value"].rolling(window=3).mean()
8️⃣ Running Total
↳ Excel: =SUM($B$2:B2) (drag down)
↳ SQL: SELECT date, SUM(value) OVER (ORDER BY date) AS running_total FROM table;
↳ Python: df["running_total"] = df["value"].cumsum()
❤14👍6
💻 How Do Analysts Use SQL in a Company?
SQL is every data analyst’s *superpower* 💥
Here’s how it's used in the real world:
🔍 Extract Data
Pull info from multiple tables to answer real business questions.
(P.S. Say NO to `SELECT *`—your future self will thank you!)
🧹 Clean & Transform
Use functions like
📊 Summarize & Analyze
Find trends with
📈 Build Dashboards
Plug your queries into Power BI, Tableau, or Excel to tell visual stories.
🧪 Run A/B Tests
Measure product changes and campaigns with data, not guesses.
🧱 Use Views & CTEs
Simplify complex logic. Clean. Reusable. Boss-approved.
🎯 Drive Decisions
Marketing, Product, Sales, Finance—SQL helps *everyone* answer:
“What’s working?”
💡 Write *smart* queries.
Avoid
SQL is every data analyst’s *superpower* 💥
Here’s how it's used in the real world:
🔍 Extract Data
Pull info from multiple tables to answer real business questions.
SELECT name, revenue FROM sales WHERE region = 'North America';
(P.S. Say NO to `SELECT *`—your future self will thank you!)
🧹 Clean & Transform
Use functions like
TRIM(), COALESCE(), CAST() to tidy up raw data.📊 Summarize & Analyze
Find trends with
GROUP BY, SUM(), AVG() and more.📈 Build Dashboards
Plug your queries into Power BI, Tableau, or Excel to tell visual stories.
🧪 Run A/B Tests
Measure product changes and campaigns with data, not guesses.
🧱 Use Views & CTEs
Simplify complex logic. Clean. Reusable. Boss-approved.
🎯 Drive Decisions
Marketing, Product, Sales, Finance—SQL helps *everyone* answer:
“What’s working?”
💡 Write *smart* queries.
Avoid
SELECT * unless you really, *really* mean it.❤7🔥2
SQL Interview Questions with Answers
1. How to change a table name in SQL?
This is the command to change a table name in SQL:
ALTER TABLE table_name
RENAME TO new_table_name;
We will start off by giving the keywords ALTER TABLE, then we will follow it up by giving the original name of the table, after that, we will give in the keywords RENAME TO and finally, we will give the new table name.
2. How to use LIKE in SQL?
The LIKE operator checks if an attribute value matches a given string pattern. Here is an example of LIKE operator
SELECT * FROM employees WHERE first_name like ‘Steven’;
With this command, we will be able to extract all the records where the first name is like “Steven”.
3. If we drop a table, does it also drop related objects like constraints, indexes, columns, default, views and sorted procedures?
Yes, SQL server drops all related objects, which exists inside a table like constraints, indexes, columns, defaults etc. But dropping a table will not drop views and sorted procedures as they exist outside the table.
4. Explain SQL Constraints.
SQL Constraints are used to specify the rules of data type in a table. They can be specified while creating and altering the table. The following are the constraints in SQL: NOT NULL CHECK DEFAULT UNIQUE PRIMARY KEY FOREIGN KEY
React ❤️ for more
1. How to change a table name in SQL?
This is the command to change a table name in SQL:
ALTER TABLE table_name
RENAME TO new_table_name;
We will start off by giving the keywords ALTER TABLE, then we will follow it up by giving the original name of the table, after that, we will give in the keywords RENAME TO and finally, we will give the new table name.
2. How to use LIKE in SQL?
The LIKE operator checks if an attribute value matches a given string pattern. Here is an example of LIKE operator
SELECT * FROM employees WHERE first_name like ‘Steven’;
With this command, we will be able to extract all the records where the first name is like “Steven”.
3. If we drop a table, does it also drop related objects like constraints, indexes, columns, default, views and sorted procedures?
Yes, SQL server drops all related objects, which exists inside a table like constraints, indexes, columns, defaults etc. But dropping a table will not drop views and sorted procedures as they exist outside the table.
4. Explain SQL Constraints.
SQL Constraints are used to specify the rules of data type in a table. They can be specified while creating and altering the table. The following are the constraints in SQL: NOT NULL CHECK DEFAULT UNIQUE PRIMARY KEY FOREIGN KEY
React ❤️ for more
❤7👏1