SQL | Data Analytics
1.85K subscribers
91 photos
8 files
61 links
SQL, Big Query, Looker and DBT for Data Analytics.

https://medium.com/@khavanski
Download Telegram
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
👍10
📖 Learn SQL
👍62
Useful tips
👍92
💫 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
👍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
👍6
📖 Visualizing a SQL query
👍85
📖 Starter Kit 2025 - Data Analyst
👍52🔥1
🎯 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
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 ❤️
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!

---

-- 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;
👍153🔥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()
13👍5
💻 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.

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.
6🔥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
6
Little tip to handle data quality issues

#sql #bigquery
👍6🔥1
📖 SQL For Everything
2
Must-know Pandas Functions for Data Analysis
4👍4
Here is a powerful 𝗜𝗡𝗧𝗘𝗥𝗩𝗜𝗘𝗪 𝗧𝗜𝗣 to help you land a job!

Most people who are skilled enough would be able to clear technical rounds with ease.

But when it comes to 𝗯𝗲𝗵𝗮𝘃𝗶𝗼𝗿𝗮𝗹/𝗰𝘂𝗹𝘁𝘂𝗿𝗲 𝗳𝗶𝘁 rounds, some folks may falter and lose the potential offer.

Many companies schedule a behavioral round with a top-level manager in the organization to understand the culture fit (except for freshers).

One needs to clear this round to reach the salary negotiation round.

Here are some tips to clear such rounds:

1️⃣ Once the HR schedules the interview, try to find the LinkedIn profile of the interviewer using the name in their email ID.

2️⃣ Learn more about his/her past experiences and try to strike up a conversation on that during the interview.

3️⃣ This shows that you have done good research and also helps strike a personal connection.

4️⃣ Also, this is the round not just to evaluate if you're a fit for the company, but also to assess if the company is a right fit for you.

5️⃣ Hence, feel free to ask many questions about your role and company to get a clear understanding before taking the offer. This shows that you really care about the role you're getting into.
4
SQL ⇆ Python ⇆ Excel
8👍2👏1🤯1
SQL Checklist for Data Analysts 📀🧠
1. SQL Basics⦁ SELECT, WHERE, ORDER BY
⦁ DISTINCT, LIMIT, BETWEEN, IN⦁ Aliasing (AS)
2. Filtering & Aggregation
⦁ GROUP BY & HAVING⦁ COUNT(), SUM(), AVG(), MIN(), MAX()
⦁ NULL handling with COALESCE, IS NULL

3. Joins
⦁ INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN
⦁ Joining multiple tables
⦁ Self Joins

4. Subqueries & CTEs
⦁ Subqueries in SELECT, WHERE, FROM
⦁ WITH clause (Common Table Expressions)
⦁ Nested subqueries

5. Window Functions
⦁ ROW_NUMBER(), RANK(), DENSE_RANK()
⦁ LEAD(), LAG()
⦁ PARTITION BY & ORDER BY within OVER()

6. Data Manipulation
⦁ INSERT, UPDATE, DELETE
⦁ CREATE TABLE, ALTER TABLE
⦁ Constraints: PRIMARY KEY, FOREIGN KEY, NOT NULL

7. Optimization Techniques
⦁ Indexes
⦁ Query performance tips
⦁ EXPLAIN plans

8. Real-World Scenarios
⦁ Writing complex queries for reports
⦁ Customer, sales, and product data
⦁ Time-based analysis (e.g., monthly trends)

9. Tools & Practice Platforms
⦁ MySQL, PostgreSQL, SQL Server
⦁ DB Fiddle, Mode Analytics, LeetCode (SQL), StrataScratch

10. Portfolio & Projects
⦁ Showcase queries on GitHub
⦁ Analyze public datasets (e.g., ecommerce, finance)
⦁ Document business insights
4
Why Do Investors Care So Much About LTV:CAC?


https://a16z.com/why-do-investors-care-so-much-about-ltvcac
3