#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
MySQL Tutorial · Data Analytics

MySQL for Data Analysis: Why Analysts Need SQL

Picture a typical first week as a junior analyst. A manager asks which region grew fastest last quarter. The data is not in a spreadsheet; it sits in the company's MySQL database, written there by the billing app every time a customer pays. You can wait for someone to send you an export, or you can write five lines of SQL and answer the question yourself. That difference is why analysts learn SQL.

What SQL gives an analyst that a spreadsheet does not

None of this replaces Excel, Power BI or Python. SQL is usually the first step: get the right data out in the right shape, then chart or model it elsewhere.

A small dataset to work with

CREATE TABLE sales (
  sale_id   INT PRIMARY KEY,
  sale_date DATE,
  region    VARCHAR(10),
  channel   VARCHAR(10),
  amount    DECIMAL(10,2)
);
INSERT INTO sales VALUES
(1,'2026-07-04','North','Online',12000),(2,'2026-07-18','South','Store',8500),
(3,'2026-07-25','North','Store',4000),(4,'2026-08-02','South','Online',15500),
(5,'2026-08-16','North','Online',9800),(6,'2026-08-29','West','Store',7200),
(7,'2026-09-05','South','Online',11000),(8,'2026-09-12','West','Online',13400),
(9,'2026-09-21','North','Store',6100),(10,'2026-09-28','South','Store',5900);

Question 1: is the data what I think it is?

Before any analysis, profile the table. This catches duplicates, gaps and surprise NULLs early:

SELECT COUNT(*) AS total_rows,
       COUNT(DISTINCT sale_id) AS distinct_ids,
       SUM(amount IS NULL) AS null_amounts,
       MIN(sale_date), MAX(sale_date)
FROM sales;
total_rowsdistinct_idsnull_amountsMIN(sale_date)MAX(sale_date)
101002026-07-042026-09-28

SUM(amount IS NULL) works because a comparison returns 1 or 0 in MySQL. If total_rows and distinct_ids differed, you would have duplicates to explain before reporting anything. The guide to NULL values in MySQL covers what NULLs do to averages and counts.

Question 2: which region brings in the most?

SELECT region,
       COUNT(*) AS orders,
       SUM(amount) AS revenue,
       ROUND(AVG(amount), 0) AS avg_order
FROM sales
GROUP BY region
ORDER BY revenue DESC;
regionordersrevenueavg_order
South440900.0010225
North431900.007975
West220600.0010300

South leads on revenue, but West has the highest average order on only two orders. Showing the count next to the average stops anyone from over-reading a small sample. This is the kind of context SQL makes cheap to add.

Question 3: how did the month change versus last month?

WITH m AS (
  SELECT DATE_FORMAT(sale_date, '%Y-%m') AS mon, SUM(amount) AS revenue
  FROM sales
  GROUP BY mon
)
SELECT mon, revenue,
       LAG(revenue) OVER (ORDER BY mon) AS prev_month,
       ROUND(100 * (revenue - LAG(revenue) OVER (ORDER BY mon))
             / LAG(revenue) OVER (ORDER BY mon), 1) AS pct_change
FROM m
ORDER BY mon;
monrevenueprev_monthpct_change
2026-0724500.00NULLNULL
2026-0832500.0024500.0032.7
2026-0936400.0032500.0012.0

The CTE builds monthly totals; LAG() reads the previous row so each month can be compared with the one before. Window functions like this need MySQL 8.0 or later. They are covered properly in the MySQL window functions tutorial.

Question 4: what share comes from each channel?

SELECT channel,
       SUM(amount) AS revenue,
       ROUND(100 * SUM(amount) / SUM(SUM(amount)) OVER (), 1) AS share_pct
FROM sales
GROUP BY channel;
channelrevenueshare_pct
Online61700.0066.1
Store31700.0033.9

SUM(SUM(amount)) OVER () is the grand total across all groups, so the share is computed in the same query. You do not need a second query for the total.

Pitfalls analysts run into

If you are starting from zero, follow a step-by-step MySQL learning path, then practise with SQL interview questions for data analysts.

Become a Job-Ready Data Analyst

Master MySQL, Advanced Excel, Power BI, Tableau and Python with Uncodemy's Data Analytics Training in Noida — live projects, expert mentors and Dedicated Placement Support.

Enroll in Data Analytics Course →