SlideShare a Scribd company logo
1: Introduction
2: Practice Dataset – Superstore
3: Data Analytics with SQL
4: Multiple Join Relationships
and Advanced Topics
6: Subqueries
UNIT 2: ORGANIZATION
2 | © 2025 True Digital Academy
In today’s lesson, we will:
1. Perform data analytics using SQL.
2. Learn about database structures and the role of structured query language (SQL).
3. Introduce basic SQL commands.
4. Explore practice data, using the Superstore Database.
DATA ORGANIZATION
LEARNING OBJECTIVES
3 | © 2025 True Digital Academy
FUNDAMENTALS OF DATABASES AND SQL
INTRODUCTION
4 | © 2025 True Digital Academy
BASIC SQL AND DATABASE
SQL (pronounced: “si-kwel”) is short for structured query language. It was
developed at IBM by Donald D. Chamberlin and Raymond Boyce in the early 1970s.
There are a number of different types of SQL database implementations and
platforms, all of which support standard (ANSI) SQL, including:
● PostgreSQL
● MySQL
● Microsoft SQL Server
● Oracle
● IBM DB2
Databases store and organize your information, and the SQL language serves as a
link between you and the information.
5 | © 2025 True Digital Academy
EXCEL/GOOGLE SHEET VS. SQL
Why do we need SQL when we have Excel/Google Sheet?
● Excel is limited by your computer’s available memory and system resources.
● Excel has a fixed upper limit of 1,048,576 rows and 16,384 columns.
● Google Sheet has a fixed limit of 10 million cells (40,000 new rows at a time) or
18,278 columns.
In other words, Excel is a local tool and Google Sheet is a cloud tool that are not able
to capably manage or interact with very large datasets. This is when SQL steps in!
6 | © 2025 True Digital Academy
EXCEL/GOOGLE SHEET VS. SQL
What can SQL do that Excel/Google Sheet can’t?
● SQL can rapidly navigate databases and query, retrieve, and aggregate
millions of records.
● SQL is also more adept than Excel/Google Sheet at creating data flows for
cleaning and preparing data at high volumes.
● SQL is the industry standard for data query and retrieval.
7 | © 2025 True Digital Academy
EXCEL/GOOGLE SHEET VS. SQL
However…
● SQL is not a data visualization tool.
● SQL can query and organize data, but is not typically used to analyze it.
In other words, SQL is not a substitute for Excel/Google Sheet. Instead, SQL is
normally used in conjunction with Excel/Google Sheet and other data visualization
tools when working with large data repositories.
8 | © 2025 True Digital Academy
EXCEL/GOOGLE SHEET VS. SQL
Ok, so let’s summarize:
● Excel/Google Sheet and SQL use similar functions. For example, both tools
offer ways for you to apply methods like: COUNT, SUM, AVG, IF THEN ELSE, etc.
● But compared to spreadsheets with tons of linked tabs, SQL databases provide
users with a much more efficient way to connect related data.
● SQL is also much more scalable than Excel/Google Sheet, allowing users to
remotely interact with large datasets in production environments.
9 | © 2025 True Digital Academy
Select, import,
explore, & clean
data.
Structure, visualize
& complete
analysis.
Make
business decisions
from data.
Develop a
hypothesis- driven
approach to
analysis.
Present data-driven
insights
to audience.
BASIC SQL: ANALYTICS WORKFLOW
Obtaining data often means accessing a SQL server to retrieve the data you need.
We’ll use SQL in the second step of the GA data workflow — preparing your data:
selecting, exploring, and cleaning.
10 | © 2025 True Digital Academy
BASIC SQL: POSTGRESQL
The SQL database tool we’ll use in this class is PostgreSQL. We’ll use this because:
• It’s an object oriented relational database management system (DBMS).
• It’s powerful and standardized.
• It’s free and open source!
Note:
Many of the SQL skills we’ll learn are applicable to other
SQL database implementations. Many of the same principles
and syntax rules apply to other versions, like MS SQL, MySQL, Oracle, etc.
1: Introduction
2: Practice Dataset – Superstore
3: Data Analytics with SQL
4: Multiple Join Relationships
and Advanced Topics
6: Subqueries
UNIT 2: ORGANIZATION
12 | © 2025 True Digital Academy
FUNDAMENTALS OF DATABASES AND SQL
PRACTICE DATA SET:
SUPERSTORE DATABASE
13 | © 2025 True Digital Academy
Instructions
Step 1: Go to url: https://analyticsga-
southeast2.generalassemb.ly/login?next=%2F
Step 2: Login with
Username: analytics_student@generalassemb.ly
Password: analyticsga
Step 3: Click on GA_Servers then enter password
analyticsga again (if asked)
Step 4: From list of databases, click on the database
you want to connect (In our case, it is superstore)
3
4
14 | © 2025 True Digital Academy
customers table stores customer information
- Customer ID
- Customer Name
- Segment
Note: One row represents one customer
Getting Start with the Data
Step 5: Navigate to Schemas > public > Tables
Step 6: Right click “customers” Table and select View Data > First 100
Rows
5
6
15 | © 2025 True Digital Academy
orders table stores order information such as
- Order ID
- Order date
- Ship date
- Ship mode
- Customer ID
- Product ID
- Sales
- Quantity
- Discount
- Profit
- Postal Code
- Region ID
Getting Start with the Data
16 | © 2025 True Digital Academy
Note:
- Each order ID was placed by one customer
- Each row represents an order of a particular product ordered by a particular customer
from any region
Getting Start with the Data
17 | © 2025 True Digital Academy
Getting Start with the Data
products table stores product information
- Product ID
- Category
- Sub category
- Product name
- Product cost to consumer
Each row represents a unique product
18 | © 2025 True Digital Academy
Getting Start with the Data
regions table stores region information including sales representative
- Region ID
- Country Code
- Country
- Region
- Subregion
- Salesperson
Note:
- Each row represents a country
- One salesperson could handle multiple countries
19 | © 2025 True Digital Academy
Getting Start with the Data
returns table stores returned order information
- Order ID
- Return Date
- Return Quantity
- Reason Returned
Note:
- Each row represents one returned order only
20 | © 2025 True Digital Academy
Schema
‣ A database schema is considered the “blueprint” of a database which describes
how the data may relate to other tables or other data models. However, the
schema does not actually contain data. The Entity-Relationship Diagram
(ER Diagram) is a tool to define a schema.
1: Introduction
2: Practice Dataset – Superstore
3: Data Analytics with SQL
4: Multiple Join Relationships
and Advanced Topics
6: Subqueries
UNIT 2: ORGANIZATION
22 | © 2025 True Digital Academy
DATA ANALYTICS WITH SQL
SCENARIO
Superstore is a very large-scale shop that selling broad spectrum of products. The major
customers are in USA. Recently, furniture selling is not making profit. You, as a data
scientist, were asked to find out what went wrong and to recommend what should be the
solutions to solve this problem.
23 | © 2025 True Digital Academy
SMART Objective and Hypothesis Tree
Problem Statement
การขายเฟอร ์นิเจอร ์ไม่ทากาไร
SMART Objective
สร ้างกาไรจากการขาย
เฟอร ์นิเจอร ์อย่างน้อย x%
ภายใน 6 เดือนด้วยการศึกษา
ข้อมูลการซื้อขายในอดีต
เฟอร ์นิเจอร ์กาไรตกทุกชนิด
เลยไหม ถ้าไม่ใช่งั้นอะไรคือ
ปัญหา
Idea: เพิ่มขาย ลดทุน เท่ากับกาไร
ดังนั้นคาถามทั้งหลายจะเกี่ยวข้องกับการ
เพิ่มขายหรือการลดทุนในช่วง 1 ปีที่ผ่านมา
ของสินค้าเฟอร ์นิเจอร ์
Note: เพื่อให้ง่ายต่อการทาความเข้าใจ ใน
ตัวอย่างนี้จะโฟกัสเฉพาะใน USA เท่านัน
ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
24 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 1: Think first, code later. How does the final output look like
year profit
2015 xxx
2016 xxx
2017 xxx
2018 xxx
… …
25 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 2: Think first, code later. What is/are the input
26 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 3: Think first, code later. What is the idea to process input to
the desired output
Extract year
Group By
year
Aggregate by
Summation
Sorted By
year
year profit
2015 xxx
2016 xxx
2017 xxx
2018 xxx
… …
27 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 4: Selecting column
Idea: We cannot select any column without knowing which table stores such columns
If we would like to pick order_date, we need to tell SQL the table name which is orders
The SELECT command:
In case of selecting all columns in a table:
28 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 4: Selecting column (Cont.)
• Selecting order_date from orders
year profit
2015 xxx
2016 xxx
2017 xxx
2018 xxx
… …
Desired Output
The query results are still far from the desired output.
We do not want dates, but we want years.
Let’s extract year from date using EXTRACT (YEAR FROM
order_date).
29 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 4: Selecting column (Cont.)
• Selecting year from orders
year profit
2015 xxx
2016 xxx
2017 xxx
2018 xxx
… …
Desired Output
The query results are getting closer to the desired output
Alias can be used to rename the column name in the query results
Another way: SELECT DATE_PART('year', order_date) AS order_year
FROM orders
30 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 5: Group By Year, Aggregate By Summation
Idea: Group By requires aggregation (always)
year value
2015 50
2015 60
2016 30
2016 20
2016 40
Example:
Group By year without Aggregation
Results: ??? (Computer has no idea group by what aggregation function)
Need to be aggregated into a single value
Need to be aggregated into a single value
year value
2015 110
2016 90
If summation is applied as aggregation function
31 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 5: Group By Year, Aggregate By Summation
SQL
Example: Group By order_date, aggregated By Summation
We are very much closer to the desired output
In our case, we want SUM(profit) as the aggregation function
year profit
2015 xxx
2016 xxx
2017 xxx
2018 xxx
… …
Desired Output
32 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Activity 1: Combine your knowledge obtained from step 4 and step 5
to complete the following processes
Extract year
Group By
year
Aggregate by
Summation
year profit
2015 xxx
2016 xxx
2017 xxx
2018 xxx
… …
33 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Activity 1: Combine your knowledge obtained from step 4 and step 5
to complete the following processes
‣ Solutions
year profit
2015 xxx
2016 xxx
2017 xxx
2018 xxx
… …
Desired Output
Good Jobs!
We get the results we want.
However, we are just lucky that
the order_year was sorted
To ensure that the order_year
was sorted in the way we want,
the ORDER BY command is
required
34 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 6: Sorted by order_date
The default sorting mode is ascending
Use ASC for ascending sort
Use DESC for descending sort
Let’s apply sorting to complete the Q1
Discussions: What’s data telling you?
Answer: The annual profit was increasing continuously except 2020
Be careful: DO NOT too hurry to conclude that we are having problem in 2020 because the data we have may not be
collected from a whole 2020
35 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Let’s check
• When was the latest order in our data
Idea:
Use MAX to get the maximum value in a given column
Use MIN to get the minimum value in a given column
It is cleared that data in 2020 is only from January
36 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Activity 2: Checking whether we have data in a whole 2015 or not
Too small number. That’s why we need to check
37 | © 2025 True Digital Academy
Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Activity 2: Checking whether we have data in a whole 2015 or not
Solutions
It is cleared that we have only data from December in 2015
38 | © 2025 True Digital Academy
SMART Objective and Hypothesis Tree
Problem Statement
การขายเฟอร ์นิเจอร ์ไม่ทากาไร
SMART Objective
สร ้างกาไรจากการขาย
เฟอร ์นิเจอร ์อย่างน้อย x%
ภายใน 6 เดือนด้วยการศึกษา
ข้อมูลการซื้อขายในอดีต
ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
เฟอร ์นิเจอร ์กาไรตกทุกชนิด
เลยไหม ถ้าไม่ใช่งั้นอะไรคือ
ปัญหา
Idea: เพิ่มขาย ลดทุน เท่ากับกาไร
ดังนั้นคาถามทั้งหลายจะเกี่ยวข้องกับการ
เพิ่มขายหรือการลดทุนในช่วง 1 ปีที่ผ่านมา
ของสินค้าเฟอร ์นิเจอร ์
Note: เพื่อให้ง่ายต่อการทาความเข้าใจ ใน
ตัวอย่างนี้จะโฟกัสเฉพาะใน USA เท่านัน
ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
39 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 1: Think first, code later. How does the final output look like
year profit category
2015 xxx Furniture
2016 xxx Furniture
2017 xxx Furniture
2018 xxx Furniture
… … ….
This column is optional to display
because we can query only order related
to furniture
Displaying it, perhaps, useful in the
sense of ensuring that we performed the
right query
40 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 2: Think first, code later. What is/are input
Joining 2 tables (orders and products) via the common field
41 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Get to know SQL JOIN
Location Temperature
Bangkok 35
Rayong 34
Phuket 33
Prae 28
Location Area sq.km
Bangkok 1,569
Chonburi 4,363
Phuket 543
Chiang Rai 11,678
Location
Location Temperature
Bangkok 35
Rayong 34
Phuket 33
Prae 28
Area sq.km
1,569
NULL
543
NULL
Output
Example
42 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Get to know SQL JOIN
Location Temperature
Bangkok 35
Rayong 34
Phuket 33
Prae 28
Location Area sq.km
Bangkok 1,569
Chonburi 4,363
Phuket 543
Chiang Rai 11,678
Location
Output
Location Area sq.km
Bangkok 1,569
Chonburi 4,363
Phuket 543
Chiang Rai 11,678
Temperature
35
NULL
33
NULL
Example
43 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Get to know SQL JOIN
Location Temperature
Bangkok 35
Rayong 34
Phuket 33
Prae 28
Location Area sq.km
Bangkok 1,569
Chonburi 4,363
Phuket 543
Chiang Rai 11,678
Location
Output
Location Temperature
Bangkok 35
Phuket 33
Area sq.km
1,569
543
Example
44 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Get to know SQL JOIN
Location Temperature
Bangkok 35
Rayong 34
Phuket 33
Prae 28
Location Area sq.km
Bangkok 1,569
Chonburi 4,363
Phuket 543
Chiang Rai 11,678
Location
Output
Location Temperature
Bangkok 35
Rayong 34
Phuket 33
Prae 28
Chonburi NULL
Chiang Rai NULL
Area sq.km
1,569
NULL
543
NULL
4,363
11,678
Example
45 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 3: Joining orders and products
• Question: Which JOIN should be applied
• Answer: Depends on the unit of analysis
Original Question:
ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
orders
Each row represents an order of a particular product ordered by a particular customer from any region
products
Each row represents a unique product
The unit of analysis to answer the original question is an order a particular product
The proper joining are one of the following options
1. LEFT join (orders, products)
2. RIGHT JOIN (products, orders)
3. INNER JOIN (orders, products)
4. INNER JOIN (products, orders)
46 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 3: Joining orders and products (cont.)
• Let’s start with the LEFT JOIN
From orders table From products table
47 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Now we have got the input table
‣ Step 4: Think first code later. How to process input to the desired
output
year profit category
2015 xxx Furniture
2016 xxx Furniture
2017 xxx Furniture
2018 xxx Furniture
… … ….
Desired output
Extract year
Group By
year and
category
Aggregate by
Summation
Filter only
Furniture
Sorted by
year
48 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 4: Select columns
• order_date: Extracting year from order
date
• Category
• profit
We have modified the previous query in step 3
This is still not answering the original question.
We need further processes
49 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 5: Group By and Aggregation
• GROUP BY: Year and Category
• Aggregation: Sum of profit
We have modified the previous query in step 4
We only want Furniture category.
Let’s filter others out using HAVING
50 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 6: Filter other categories out. Keep only Furniture
• HAVING is used to filter measures you’ve aggregated
Year Values
2019 100
2019 120
2020 200
2020 180
annual
SUM
SUM
Year Values
2019 220
2020 380 HAVING Year=2019
Year Values
2019 220
Basic Idea
51 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 6: Filter other categories out. Keep only Furniture
We have modified the previous query in step 4
This query results is very close to our desired output
We only need to sort by year
52 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Step 7: Apply sorting Good jobs! We have completed to process data from input to
the desired output. This is the heart of programming which is
the logic to process input to the desired output.
Question: What is the results telling you?
Note: We have already known that 2015 and 2020 are not
full year data
Answer: The cumulative profit of furniture selling is growing
up in each year
Let’s move to the next analytic question
53 | © 2025 True Digital Academy
Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
‣ Recall from step 3, we have 4 possible JOINs for this problem
The proper joining are one of the following options
1. LEFT join (orders, products)
2. RIGHT JOIN (products, orders)
3. INNER JOIN (orders, products)
4. INNER JOIN (products, orders)
You may check the results
1 2
3
4
54 | © 2025 True Digital Academy
SMART Objective and Hypothesis Tree
Problem Statement
การขายเฟอร ์นิเจอร ์ไม่ทากาไร
SMART Objective
สร ้างกาไรจากการขาย
เฟอร ์นิเจอร ์อย่างน้อย x%
ภายใน 6 เดือนด้วยการศึกษา
ข้อมูลการซื้อขายในอดีต
ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
เฟอร ์นิเจอร ์กาไรตกทุกชนิด
เลยไหม ถ้าไม่ใช่งั้นอะไรคือ
ปัญหา
Idea: เพิ่มขาย ลดทุน เท่ากับกาไร
ดังนั้นคาถามทั้งหลายจะเกี่ยวข้องกับการ
เพิ่มขายหรือการลดทุนในช่วง 1 ปีที่ผ่านมา
ของสินค้าเฟอร ์นิเจอร ์
Note: เพื่อให้ง่ายต่อการทาความเข้าใจ ใน
ตัวอย่างนี้จะโฟกัสเฉพาะใน USA เท่านัน
ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ
sub region ที่ USA ในแต่ละเดือนของปี 2019
ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
55 | © 2025 True Digital Academy
Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019
‣ Step 1: Think first, code later. How does the final output look like
year Month profit category subregion country
2019 1 xxx Furniture … United States
2019 2 xxx Furniture … United States
2019 … xxx Furniture … United States
2019 … xxx Furniture … United States
… … … …. … United States
56 | © 2025 True Digital Academy
Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019
‣ Step 2: Think first, code later. What is/are input
Joining 2 tables (orders and products) via the common field
Joining 2 tables (orders and regions) via the common field
We need these columns
We need these columns
We need these columns
57 | © 2025 True Digital Academy
Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019
‣ Activity 3: Use your knowledge from previous questions to merge 3 tables
(orders, products, regions) together and select the following columns
• Orders
• Order_date
• Extract month
• Extract year
• Profit
• Products
• Category
• Regions
• Sub_region
• Country
58 | © 2025 True Digital Academy
Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019
‣ Activity 3: Use your knowledge from previous questions to merge 3 tables
(orders, products, regions) together and select the following columns
Solutions
59 | © 2025 True Digital Academy
Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019
‣ Now we have the input from the activity 3
‣ Step 3: Think first code later. How to process input to the desired
output
year Month profit categor
y
subregi
on
country
2019 1 xxx Furniture … United
States
2019 2 xxx Furniture … United
States
2019 … xxx Furniture … United
States
2019 … xxx Furniture … United
States
… … … …. … United
States
Group By all
except profit
Aggregate by
Summation
Filter by
Furniture, United
States and 2019
Sorting
60 | © 2025 True Digital Academy
Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019
‣ Step 4: Group By and Aggregation
• Group BY all except profit
• Aggregate profit by summation
We have modified the previous query in activity 3
61 | © 2025 True Digital Academy
Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019
‣ Step 5: Filtered by Furniture, United States and 2019
We have modified the previous query in step 4
62 | © 2025 True Digital Academy
Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019
‣ Step 6: Sorting
• Tips:
• We are looking for non-profitable evidences
• Apply sorting directly to cum_profit returns the serious non-profitable evidences, but we
will loss the trend monitoring
• Trade-off between focus to the non-profitable evidences and trend monitoring
63 | © 2025 True Digital Academy
Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019
‣ Step 6: Sorting
• Option 1: Focus on non-profitable evidences Question: What is the results telling you?
Answer: There are several non-profitable
evidences in 2019, but June (East and South),
July (West), and February (Central) seems to be
very seriously non-profitable evidences
We need to further investigate what went wrong in
those evidences
We have modified the previous query in step 5
64 | © 2025 True Digital Academy
Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019
‣ Step 6: Sorting
• Option 2: Focus
on trend
monitoring
We have modified the previous query in step 5
Question: What do the results tell
you?
Answer: The cumulative profit, for
example, in 2019 at Central USA is
fluctuating. There are some non-
profitable evidences in Feb, Apr,
and Oct.
We need to further investigate what
went wrong in those evidences and
other subregions
65 | © 2025 True Digital Academy
SMART Objective and Hypothesis Tree
Problem Statement
การขายเฟอร ์นิเจอร ์ไม่ทากาไร
SMART Objective
สร ้างกาไรจากการขาย
เฟอร ์นิเจอร ์อย่างน้อย x%
ภายใน 6 เดือนด้วยการศึกษา
ข้อมูลการซื้อขายในอดีต
ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
เฟอร ์นิเจอร ์กาไรตกทุกชนิด
เลยไหม ถ้าไม่ใช่งั้นอะไรคือ
ปัญหา
Idea: เพิ่มขาย ลดทุน เท่ากับกาไร
ดังนั้นคาถามทั้งหลายจะเกี่ยวข้องกับการ
เพิ่มขายหรือการลดทุนในช่วง 1 ปีที่ผ่านมา
ของสินค้าเฟอร ์นิเจอร ์
Note: เพื่อให้ง่ายต่อการทาความเข้าใจ ใน
ตัวอย่างนี้จะโฟกัสเฉพาะใน USA เท่านัน
Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม
subregion ใน USA เฉพาะปี 2019
ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ใน
แต่ละเดือนของปี 2019
ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
66 | © 2025 True Digital Academy
Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019
‣ Step 1: Think first, code later. How does the final output look like
year Month profit category subcategory Product name subregion country
2019 1 xxx Furniture … … … United States
2019 2 xxx Furniture … … … United States
2019 … xxx Furniture … … … United States
2019 … xxx Furniture … … … United States
… … … …. … … … United States
Must be negative values
67 | © 2025 True Digital Academy
Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019
‣ Step 2: Think first, code later. What is/are input
Joining 2 tables (orders and products) via the common field
Joining 2 tables (orders and regions) via the common field
We need these columns
We need these columns
We need these columns
68 | © 2025 True Digital Academy
Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019
‣ Activity 4: Use your knowledge from previous questions to merge 3 tables (orders, products, regions)
together and select the following columns
• Orders
• Order_date
• Extract month
• Extract year
• Profit
• Products
• Category
• Subcategory
• Product name
• Regions
• Sub_region
• Country
69 | © 2025 True Digital Academy
Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019
‣ Activity 4: Use your knowledge from previous questions to merge 3 tables (orders, products,
regions) together and select the following columns
Solutions
70 | © 2025 True Digital Academy
Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019
‣ Now we have input
‣ Step 3: Think first code later. How to process input to the desired
output
Group By all
except profit
Aggregate by
Summation
Filter by Furniture,
United States, 2019,
and negative profit
Sorting
year Mon
th
prof
it
cate
gory
sub
cate
gory
Pro
duct
nam
e
subr
egio
n
cou
ntry
2019 1 xxx Furni
ture
… … … Unit
ed
Stat
es
2019 2 xxx Furni
ture
… … … Unit
ed
Stat
es
2019 … xxx Furni
ture
… … … Unit
ed
Stat
es
… … … …. … … … Unit
ed
Stat
es
71 | © 2025 True Digital Academy
Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019
‣ Step 4: Group By and Aggregation
• Group BY all except profit
• Aggregate profit by summation
We have modified the previous query in activity 4
72 | © 2025 True Digital Academy
Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019
‣ Step 5: Filtered the group with Furniture, United States, 2019, and
negative cumulative profit
We have modified the previous query in step 4
73 | © 2025 True Digital Academy
Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019
‣ Step 6: Sorting
• Pinpoint to which products and their non-profitable evidences
• Sorting priority focus on cumulative profit
• Rank top 10 of the non-profitable evidences
‣ The LIMIT command is used for displaying only “nrows” of the query
results (works only in PostgresQL)
74 | © 2025 True Digital Academy
Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019
‣ Step 6: Sorting (Cont.)
Question: What is the results telling us?
Answer: 3 subcategories (table, chair, bookcases) were found
with serious non-profitable evidences
75 | © 2025 True Digital Academy
SMART Objective and Hypothesis Tree
Problem Statement
การขายเฟอร ์นิเจอร ์ไม่ทากาไร
SMART Objective
สร ้างกาไรจากการขาย
เฟอร ์นิเจอร ์อย่างน้อย x%
ภายใน 6 เดือนด้วยการศึกษา
ข้อมูลการซื้อขายในอดีต
ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
เฟอร ์นิเจอร ์กาไรตกทุกชนิด
เลยไหม ถ้าไม่ใช่งั้นอะไรคือ
ปัญหา
Idea: เพิ่มขาย ลดทุน เท่ากับกาไร
ดังนั้นคาถามทั้งหลายจะเกี่ยวข้องกับการ
เพิ่มขายหรือการลดทุนในช่วง 1 ปีที่ผ่านมา
ของสินค้าเฟอร ์นิเจอร ์
Note: เพื่อให้ง่ายต่อการทาความเข้าใจ ใน
ตัวอย่างนี้จะโฟกัสเฉพาะใน USA เท่านัน Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม
subregion ใน USA เฉพาะปี 2019
ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ใน
แต่ละเดือนของปี 2019
ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
Product ที่มี serious non-profitable evidences มีการลด
ราคาอย่างไร เช่น Sauder Classic Bookcases, Metal ที่มี
serious non-profitable evidence หนักที่สุด ใน United
States ในปี 2019
โครงสร ้างของราคาผิดปกติ
หรือไม่
76 | © 2025 True Digital Academy
Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร
‣ Step 1: Think first, code later. How does the final output look like
Year Month Product Name SUM Quantity AVG Sales AVG Discount SUM profit
2019 … … … … … …
2019 … … … … … …
2019 … … … … … …
2019 … … … … … …
2019 … … … … … …
77 | © 2025 True Digital Academy
Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร
‣ Step 2: Think first, code later. What is/are input
Joining 2 tables (orders and products) via the common field
We need these columns
We need these columns Joining 2 tables (orders and regions) via the common field
We need these columns
78 | © 2025 True Digital Academy
Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร
‣ Activity 5: Use your knowledge from previous questions to merge 3 tables (orders, products,
regions) together and select the following columns
• Orders
• Order_date
• Extract Year/ Month
• Quantity
• Sales
• Discount
• Profit
• Products
• Product Name
• Regions
• Country
79 | © 2025 True Digital Academy
Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร
‣ Activity 5: Use your knowledge from previous questions to merge 3 tables (orders, products,
regions) together and select the following columns
80 | © 2025 True Digital Academy
Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร
‣ Now we have input
‣ Step 3: Think first code later. How to process input to the desired
output
Group By all
except profit
Aggregate by
SUM and
Average
Filter by Furniture, United
States, 2019, and
product_name ‘Saunder
Classic Bookcase, Metal’ Sorting
Year Mont
h
Product
Name
SUM Quantity AVG Sales AVG
Discount
SUM profit
2019 … … … … … …
2019 … … … … … …
2019 … … … … … …
2019 … … … … … …
2019 … … … … … …
81 | © 2025 True Digital Academy
Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร
‣ Filtering, WHERE command is used for filtering non-aggregated data
locations Temperature
Bangkok 35
Rayong 34
Phuket 33
Prae 28
locations Temperature
Bangkok 35
82 | © 2025 True Digital Academy
Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร
‣ Step 4: Filtering United States, 2019 and product name using WHERE
We have modified the previous query in step 3
“Sauder Classic Bookcase, Metal” was picked for
investigation because it has the most serious non-
profitable evidences
Try LIKE keyword LIKE “Sauder%” or ILIKE for
PostgresQL
83 | © 2025 True Digital Academy
Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร
‣ Step 5: Group By and Aggregation (remove select country)
• Group By all except quantity, profit, discount, and sales
• Aggregate by average and summation
We have modified the previous query in activity 5
84 | © 2025 True Digital Academy
Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร
‣ Step 6: Sorting
Question: What the results telling you
Answer: Something wrong with the discount. In
common sense, the more purchase quantity the more
discount can be.
However, we observed that order in February has
average discount about half of October, but the
purchasing quantity shows contradiction to the common
sense.
Moreover, cumulative profit in February is obviously
extreme compared to others.
Only average value may not be able to reveal fact.
Perhaps, statistical parameters could help.
85 | © 2025 True Digital Academy
Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร
‣ Step 7: Add some statistical parameters to sales
• Min
• Max
• Standard deviation
Question: What is the results telling you?
Answer: It tells not much for this case. We
observe that the non-profitable evidences
have constant selling price
Be careful, do not hurry to conclude because
statistics could mislead to you.
Further investigation in transaction level on
specific product and time is required
86 | © 2025 True Digital Academy
Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร
‣ Statistical could mislead you
87 | © 2025 True Digital Academy
SMART Objective and Hypothesis Tree
Problem Statement
การขายเฟอร ์นิเจอร ์ไม่ทากาไร
SMART Objective
สร ้างกาไรจากการขาย
เฟอร ์นิเจอร ์อย่างน้อย x%
ภายใน 6 เดือนด้วยการศึกษา
ข้อมูลการซื้อขายในอดีต
ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี
เฟอร ์นิเจอร ์กาไรตกทุกชนิด
เลยไหม ถ้าไม่ใช่งั้นอะไรคือ
ปัญหา
Idea: เพิ่มขาย ลดทุน เท่ากับกาไร
ดังนั้นคาถามทั้งหลายจะเกี่ยวข้องกับการ
เพิ่มขายหรือการลดทุนในช่วง 1 ปีที่ผ่านมา
ของสินค้าเฟอร ์นิเจอร ์
Note: เพื่อให้ง่ายต่อการทาความเข้าใจ ใน
ตัวอย่างนี้จะโฟกัสเฉพาะใน USA เท่านัน Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม
subregion ใน USA เฉพาะปี 2019
ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ใน
แต่ละเดือนของปี 2019
ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
Product ที่มี serious non-profitable evidences มีการ
ลดราคาอย่างไร เช่น Sauder Classic Bookcases, Metal
ที่มี serious non-profitable evidence หนักที่สุด ใน
United States ในปี 2019
โครงสร ้างของราคาผิดปกติ
หรือไม่
Product ที่มี serious non-profitable evidences มี
transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit
margin เท่าใด เช่น Sauder Classic Bookcases, Metal ที่มี
serious non-profitable evidence หนักที่สุดใน United
States ในปี 2019
88 | © 2025 True Digital Academy
Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด
‣ Step 1: Think first, code later. How does the final output look like
year month Order ID Product name quantity sales discount profit Profit margin
89 | © 2025 True Digital Academy
Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด
‣ Step 2: Think first, code later. What is/are input
Joining 2 tables (orders and products) via the common field
We need these columns
We need these columns Joining 2 tables (orders and regions) via the common field
We need these columns
90 | © 2025 True Digital Academy
Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด
‣ Activity 6: Use your knowledge from previous questions to merge 3
tables (orders, products, regions) together and select the following
columns
‣ Select only relevant columns defined in step 2
91 | © 2025 True Digital Academy
Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด
‣ Activity 6: Use your knowledge from previous questions to merge 3 tables (orders, products, regions)
together and select the following columns
‣ Select only relevant columns defined in step 2
Solution
92 | © 2025 True Digital Academy
Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด
‣ Now we have input
‣ Step 3: Think first code later. How to process input to the desired output
Calculate profit
margin
Filter by Furniture,
United States, 2019,
and product_name
Sorting
year month Order ID Product name quantity sales discount profit Profit margin
93 | © 2025 True Digital Academy
Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด
‣ Step 4: Calculate profit margin
We have modified the previous query in activity 6
94 | © 2025 True Digital Academy
Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด
‣ Step 4: Calculate profit margin
• Step 4.1 Apply decimal round to make display results easier to read
We have modified the previous query in the previous step 4
95 | © 2025 True Digital Academy
Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด
‣ Step 5: Filtering United States, 2019, and Sauder Classic Bookcase,
Metal
We have modified the previous query in the previous step 4.1
96 | © 2025 True Digital Academy
Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด
‣ Step 6: Sorting
Question: What is the results telling you?
Answer: For Sauder Classic Bookcase, Metal sold in
2019 at USA, overall transactions look fine except IT-
2019-3150765 that has the same quantity, sales, and
discount as other February transactions, but profit looks
very abnormal, and this transaction is the most serious
non-profitable evidence.
We do not have any further data to investigate what
exactly went wrong about it. We could suspect that might
involve with human error, something outside the regular
purchasing system, etc.
97 | © 2025 True Digital Academy
DATA ORGANIZATION
CONCLUSION
98 | © 2025 True Digital Academy
SQL IN A NUTSHELL
✔ SELECT: Selects the columns.
✔ FROM: Points to the table.
✔ WHERE: Filters on rows.
✔ GROUP BY: Aggregates across values of a variable.
✔ HAVING: Filters groups.
✔ ORDER BY: Sorts or arranges the results.
✔ LIMIT: Limits result to the first n rows.
Also: (INNER/LEFT/RIGHT/FULL)JOIN: Combine columns across tables with
the relationships.
99 | © 2025 True Digital Academy
DATA ORGANIZATION WITH SQL
HOMEWORK 3
(กาหนดส่งก่อนเรียนครั้ง
หน้า 29 มี.ค.)
100 | © 2025 True Digital Academy
DATA ORGANIZATION
DIRECTIONS
‣ ทำตำมสไลด์ที่เหลือ (slide 87 – 96)
‣ ตั้งคำถำมเพิ่ม 1 คำถำมนอกเหนือจำกคำถำมในห้อง พร้อมทั้งตอบคำถำมด้วย SQL โดยต้องมีกำรใช้ GROUP BY +
Aggregation
DELIVERABLES
1. ส่ง SQL statement, พร้อม capture หน้ำจอกำรใช้งำนและตัวอย่ำงผลลัพธ์ที่ได้ อธิบำยสิ่งที่ได้โดยสังเขป(คร่ำวๆ)
2. ส่งเป็น PDF
3. ตั้งชื่อไฟล์ขึ้นต้นด้วย HW3_ชื่อนำมสกุล หย่อนลงใน Google Drive โฟลเดอร์ชื่อนำมสกุล ตนเอง
Exit Ticket (เลือก Week 3: 22-Mar-25)
https://bit.ly/4gKS4x9
Ad

More Related Content

Similar to SQL (Structured Query Language) is a standard language used for accessing and manipulating databases. (20)

บทความ Big Data จากบล็อก thanachart.org
บทความ Big Data จากบล็อก thanachart.orgบทความ Big Data จากบล็อก thanachart.org
บทความ Big Data จากบล็อก thanachart.org
IMC Institute
 
เทคโนโลยี Cloud Computing
เทคโนโลยี Cloud Computingเทคโนโลยี Cloud Computing
เทคโนโลยี Cloud Computing
IMC Institute
 
Agile Enterprise Architecture - Danairat
Agile Enterprise Architecture - DanairatAgile Enterprise Architecture - Danairat
Agile Enterprise Architecture - Danairat
Danairat Thanabodithammachari
 
Cloud Computing กับการใช้งานในองค์กรต่างๆ
Cloud Computing กับการใช้งานในองค์กรต่างๆCloud Computing กับการใช้งานในองค์กรต่างๆ
Cloud Computing กับการใช้งานในองค์กรต่างๆ
Software Park Thailand
 
แปล Design and implementation of e
แปล Design and implementation of eแปล Design and implementation of e
แปล Design and implementation of e
School
 
นิตยสาร IT Trends ของ IMC Institute ฉบับที่ 10
นิตยสาร IT Trends ของ  IMC Institute  ฉบับที่ 10นิตยสาร IT Trends ของ  IMC Institute  ฉบับที่ 10
นิตยสาร IT Trends ของ IMC Institute ฉบับที่ 10
IMC Institute
 
05 power platform series power bi workshop
05 power platform series power bi workshop05 power platform series power bi workshop
05 power platform series power bi workshop
Kumton Suttiraksiri
 
Cloud Computing กับการใช้งานในองค์กรต่างๆ
Cloud Computing กับการใช้งานในองค์กรต่างๆCloud Computing กับการใช้งานในองค์กรต่างๆ
Cloud Computing กับการใช้งานในองค์กรต่างๆ
Thanachart Numnonda
 
การพัฒนาซอฟแวร์
การพัฒนาซอฟแวร์การพัฒนาซอฟแวร์
การพัฒนาซอฟแวร์
karmpu
 
e-Government Cloud Computing
e-Government Cloud Computinge-Government Cloud Computing
e-Government Cloud Computing
Thanakrit Lersmethasakul
 
Introduction to big data and analytic eakasit patcharawongsakda
Introduction to big data and analytic eakasit patcharawongsakdaIntroduction to big data and analytic eakasit patcharawongsakda
Introduction to big data and analytic eakasit patcharawongsakda
BAINIDA
 
CoverSpace solution v. 05
 CoverSpace solution v. 05 CoverSpace solution v. 05
CoverSpace solution v. 05
Damrongsak Kobtakul
 
Project Management (February 22, 2019; Postponed to March 29, 2019)
Project Management (February 22, 2019; Postponed to March 29, 2019)Project Management (February 22, 2019; Postponed to March 29, 2019)
Project Management (February 22, 2019; Postponed to March 29, 2019)
Nawanan Theera-Ampornpunt
 
SA Chapter 8
SA Chapter 8SA Chapter 8
SA Chapter 8
Nuth Otanasap
 
การปรับเปลี่ยนเกณฑ์ Changes in Baldrige framework 2019 2020
การปรับเปลี่ยนเกณฑ์ Changes in  Baldrige framework 2019 2020 การปรับเปลี่ยนเกณฑ์ Changes in  Baldrige framework 2019 2020
การปรับเปลี่ยนเกณฑ์ Changes in Baldrige framework 2019 2020
maruay songtanin
 
การค้นหาและสร้าง Business model
การค้นหาและสร้าง Business modelการค้นหาและสร้าง Business model
การค้นหาและสร้าง Business model
Cheeptham Kumvisate
 
เครื่องมือในการออกแบบบัญชีด้วยคอมพิวเตอร์
เครื่องมือในการออกแบบบัญชีด้วยคอมพิวเตอร์เครื่องมือในการออกแบบบัญชีด้วยคอมพิวเตอร์
เครื่องมือในการออกแบบบัญชีด้วยคอมพิวเตอร์
Witoon Thammatuch-aree
 
บทความ Big Data จากบล็อก thanachart.org
บทความ Big Data จากบล็อก thanachart.orgบทความ Big Data จากบล็อก thanachart.org
บทความ Big Data จากบล็อก thanachart.org
IMC Institute
 
เทคโนโลยี Cloud Computing
เทคโนโลยี Cloud Computingเทคโนโลยี Cloud Computing
เทคโนโลยี Cloud Computing
IMC Institute
 
Cloud Computing กับการใช้งานในองค์กรต่างๆ
Cloud Computing กับการใช้งานในองค์กรต่างๆCloud Computing กับการใช้งานในองค์กรต่างๆ
Cloud Computing กับการใช้งานในองค์กรต่างๆ
Software Park Thailand
 
แปล Design and implementation of e
แปล Design and implementation of eแปล Design and implementation of e
แปล Design and implementation of e
School
 
นิตยสาร IT Trends ของ IMC Institute ฉบับที่ 10
นิตยสาร IT Trends ของ  IMC Institute  ฉบับที่ 10นิตยสาร IT Trends ของ  IMC Institute  ฉบับที่ 10
นิตยสาร IT Trends ของ IMC Institute ฉบับที่ 10
IMC Institute
 
05 power platform series power bi workshop
05 power platform series power bi workshop05 power platform series power bi workshop
05 power platform series power bi workshop
Kumton Suttiraksiri
 
Cloud Computing กับการใช้งานในองค์กรต่างๆ
Cloud Computing กับการใช้งานในองค์กรต่างๆCloud Computing กับการใช้งานในองค์กรต่างๆ
Cloud Computing กับการใช้งานในองค์กรต่างๆ
Thanachart Numnonda
 
การพัฒนาซอฟแวร์
การพัฒนาซอฟแวร์การพัฒนาซอฟแวร์
การพัฒนาซอฟแวร์
karmpu
 
Introduction to big data and analytic eakasit patcharawongsakda
Introduction to big data and analytic eakasit patcharawongsakdaIntroduction to big data and analytic eakasit patcharawongsakda
Introduction to big data and analytic eakasit patcharawongsakda
BAINIDA
 
Project Management (February 22, 2019; Postponed to March 29, 2019)
Project Management (February 22, 2019; Postponed to March 29, 2019)Project Management (February 22, 2019; Postponed to March 29, 2019)
Project Management (February 22, 2019; Postponed to March 29, 2019)
Nawanan Theera-Ampornpunt
 
การปรับเปลี่ยนเกณฑ์ Changes in Baldrige framework 2019 2020
การปรับเปลี่ยนเกณฑ์ Changes in  Baldrige framework 2019 2020 การปรับเปลี่ยนเกณฑ์ Changes in  Baldrige framework 2019 2020
การปรับเปลี่ยนเกณฑ์ Changes in Baldrige framework 2019 2020
maruay songtanin
 
การค้นหาและสร้าง Business model
การค้นหาและสร้าง Business modelการค้นหาและสร้าง Business model
การค้นหาและสร้าง Business model
Cheeptham Kumvisate
 
เครื่องมือในการออกแบบบัญชีด้วยคอมพิวเตอร์
เครื่องมือในการออกแบบบัญชีด้วยคอมพิวเตอร์เครื่องมือในการออกแบบบัญชีด้วยคอมพิวเตอร์
เครื่องมือในการออกแบบบัญชีด้วยคอมพิวเตอร์
Witoon Thammatuch-aree
 

SQL (Structured Query Language) is a standard language used for accessing and manipulating databases.

  • 1. 1: Introduction 2: Practice Dataset – Superstore 3: Data Analytics with SQL 4: Multiple Join Relationships and Advanced Topics 6: Subqueries UNIT 2: ORGANIZATION
  • 2. 2 | © 2025 True Digital Academy In today’s lesson, we will: 1. Perform data analytics using SQL. 2. Learn about database structures and the role of structured query language (SQL). 3. Introduce basic SQL commands. 4. Explore practice data, using the Superstore Database. DATA ORGANIZATION LEARNING OBJECTIVES
  • 3. 3 | © 2025 True Digital Academy FUNDAMENTALS OF DATABASES AND SQL INTRODUCTION
  • 4. 4 | © 2025 True Digital Academy BASIC SQL AND DATABASE SQL (pronounced: “si-kwel”) is short for structured query language. It was developed at IBM by Donald D. Chamberlin and Raymond Boyce in the early 1970s. There are a number of different types of SQL database implementations and platforms, all of which support standard (ANSI) SQL, including: ● PostgreSQL ● MySQL ● Microsoft SQL Server ● Oracle ● IBM DB2 Databases store and organize your information, and the SQL language serves as a link between you and the information.
  • 5. 5 | © 2025 True Digital Academy EXCEL/GOOGLE SHEET VS. SQL Why do we need SQL when we have Excel/Google Sheet? ● Excel is limited by your computer’s available memory and system resources. ● Excel has a fixed upper limit of 1,048,576 rows and 16,384 columns. ● Google Sheet has a fixed limit of 10 million cells (40,000 new rows at a time) or 18,278 columns. In other words, Excel is a local tool and Google Sheet is a cloud tool that are not able to capably manage or interact with very large datasets. This is when SQL steps in!
  • 6. 6 | © 2025 True Digital Academy EXCEL/GOOGLE SHEET VS. SQL What can SQL do that Excel/Google Sheet can’t? ● SQL can rapidly navigate databases and query, retrieve, and aggregate millions of records. ● SQL is also more adept than Excel/Google Sheet at creating data flows for cleaning and preparing data at high volumes. ● SQL is the industry standard for data query and retrieval.
  • 7. 7 | © 2025 True Digital Academy EXCEL/GOOGLE SHEET VS. SQL However… ● SQL is not a data visualization tool. ● SQL can query and organize data, but is not typically used to analyze it. In other words, SQL is not a substitute for Excel/Google Sheet. Instead, SQL is normally used in conjunction with Excel/Google Sheet and other data visualization tools when working with large data repositories.
  • 8. 8 | © 2025 True Digital Academy EXCEL/GOOGLE SHEET VS. SQL Ok, so let’s summarize: ● Excel/Google Sheet and SQL use similar functions. For example, both tools offer ways for you to apply methods like: COUNT, SUM, AVG, IF THEN ELSE, etc. ● But compared to spreadsheets with tons of linked tabs, SQL databases provide users with a much more efficient way to connect related data. ● SQL is also much more scalable than Excel/Google Sheet, allowing users to remotely interact with large datasets in production environments.
  • 9. 9 | © 2025 True Digital Academy Select, import, explore, & clean data. Structure, visualize & complete analysis. Make business decisions from data. Develop a hypothesis- driven approach to analysis. Present data-driven insights to audience. BASIC SQL: ANALYTICS WORKFLOW Obtaining data often means accessing a SQL server to retrieve the data you need. We’ll use SQL in the second step of the GA data workflow — preparing your data: selecting, exploring, and cleaning.
  • 10. 10 | © 2025 True Digital Academy BASIC SQL: POSTGRESQL The SQL database tool we’ll use in this class is PostgreSQL. We’ll use this because: • It’s an object oriented relational database management system (DBMS). • It’s powerful and standardized. • It’s free and open source! Note: Many of the SQL skills we’ll learn are applicable to other SQL database implementations. Many of the same principles and syntax rules apply to other versions, like MS SQL, MySQL, Oracle, etc.
  • 11. 1: Introduction 2: Practice Dataset – Superstore 3: Data Analytics with SQL 4: Multiple Join Relationships and Advanced Topics 6: Subqueries UNIT 2: ORGANIZATION
  • 12. 12 | © 2025 True Digital Academy FUNDAMENTALS OF DATABASES AND SQL PRACTICE DATA SET: SUPERSTORE DATABASE
  • 13. 13 | © 2025 True Digital Academy Instructions Step 1: Go to url: https://analyticsga- southeast2.generalassemb.ly/login?next=%2F Step 2: Login with Username: analytics_student@generalassemb.ly Password: analyticsga Step 3: Click on GA_Servers then enter password analyticsga again (if asked) Step 4: From list of databases, click on the database you want to connect (In our case, it is superstore) 3 4
  • 14. 14 | © 2025 True Digital Academy customers table stores customer information - Customer ID - Customer Name - Segment Note: One row represents one customer Getting Start with the Data Step 5: Navigate to Schemas > public > Tables Step 6: Right click “customers” Table and select View Data > First 100 Rows 5 6
  • 15. 15 | © 2025 True Digital Academy orders table stores order information such as - Order ID - Order date - Ship date - Ship mode - Customer ID - Product ID - Sales - Quantity - Discount - Profit - Postal Code - Region ID Getting Start with the Data
  • 16. 16 | © 2025 True Digital Academy Note: - Each order ID was placed by one customer - Each row represents an order of a particular product ordered by a particular customer from any region Getting Start with the Data
  • 17. 17 | © 2025 True Digital Academy Getting Start with the Data products table stores product information - Product ID - Category - Sub category - Product name - Product cost to consumer Each row represents a unique product
  • 18. 18 | © 2025 True Digital Academy Getting Start with the Data regions table stores region information including sales representative - Region ID - Country Code - Country - Region - Subregion - Salesperson Note: - Each row represents a country - One salesperson could handle multiple countries
  • 19. 19 | © 2025 True Digital Academy Getting Start with the Data returns table stores returned order information - Order ID - Return Date - Return Quantity - Reason Returned Note: - Each row represents one returned order only
  • 20. 20 | © 2025 True Digital Academy Schema ‣ A database schema is considered the “blueprint” of a database which describes how the data may relate to other tables or other data models. However, the schema does not actually contain data. The Entity-Relationship Diagram (ER Diagram) is a tool to define a schema.
  • 21. 1: Introduction 2: Practice Dataset – Superstore 3: Data Analytics with SQL 4: Multiple Join Relationships and Advanced Topics 6: Subqueries UNIT 2: ORGANIZATION
  • 22. 22 | © 2025 True Digital Academy DATA ANALYTICS WITH SQL SCENARIO Superstore is a very large-scale shop that selling broad spectrum of products. The major customers are in USA. Recently, furniture selling is not making profit. You, as a data scientist, were asked to find out what went wrong and to recommend what should be the solutions to solve this problem.
  • 23. 23 | © 2025 True Digital Academy SMART Objective and Hypothesis Tree Problem Statement การขายเฟอร ์นิเจอร ์ไม่ทากาไร SMART Objective สร ้างกาไรจากการขาย เฟอร ์นิเจอร ์อย่างน้อย x% ภายใน 6 เดือนด้วยการศึกษา ข้อมูลการซื้อขายในอดีต เฟอร ์นิเจอร ์กาไรตกทุกชนิด เลยไหม ถ้าไม่ใช่งั้นอะไรคือ ปัญหา Idea: เพิ่มขาย ลดทุน เท่ากับกาไร ดังนั้นคาถามทั้งหลายจะเกี่ยวข้องกับการ เพิ่มขายหรือการลดทุนในช่วง 1 ปีที่ผ่านมา ของสินค้าเฟอร ์นิเจอร ์ Note: เพื่อให้ง่ายต่อการทาความเข้าใจ ใน ตัวอย่างนี้จะโฟกัสเฉพาะใน USA เท่านัน ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
  • 24. 24 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 1: Think first, code later. How does the final output look like year profit 2015 xxx 2016 xxx 2017 xxx 2018 xxx … …
  • 25. 25 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 2: Think first, code later. What is/are the input
  • 26. 26 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 3: Think first, code later. What is the idea to process input to the desired output Extract year Group By year Aggregate by Summation Sorted By year year profit 2015 xxx 2016 xxx 2017 xxx 2018 xxx … …
  • 27. 27 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 4: Selecting column Idea: We cannot select any column without knowing which table stores such columns If we would like to pick order_date, we need to tell SQL the table name which is orders The SELECT command: In case of selecting all columns in a table:
  • 28. 28 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 4: Selecting column (Cont.) • Selecting order_date from orders year profit 2015 xxx 2016 xxx 2017 xxx 2018 xxx … … Desired Output The query results are still far from the desired output. We do not want dates, but we want years. Let’s extract year from date using EXTRACT (YEAR FROM order_date).
  • 29. 29 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 4: Selecting column (Cont.) • Selecting year from orders year profit 2015 xxx 2016 xxx 2017 xxx 2018 xxx … … Desired Output The query results are getting closer to the desired output Alias can be used to rename the column name in the query results Another way: SELECT DATE_PART('year', order_date) AS order_year FROM orders
  • 30. 30 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 5: Group By Year, Aggregate By Summation Idea: Group By requires aggregation (always) year value 2015 50 2015 60 2016 30 2016 20 2016 40 Example: Group By year without Aggregation Results: ??? (Computer has no idea group by what aggregation function) Need to be aggregated into a single value Need to be aggregated into a single value year value 2015 110 2016 90 If summation is applied as aggregation function
  • 31. 31 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 5: Group By Year, Aggregate By Summation SQL Example: Group By order_date, aggregated By Summation We are very much closer to the desired output In our case, we want SUM(profit) as the aggregation function year profit 2015 xxx 2016 xxx 2017 xxx 2018 xxx … … Desired Output
  • 32. 32 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Activity 1: Combine your knowledge obtained from step 4 and step 5 to complete the following processes Extract year Group By year Aggregate by Summation year profit 2015 xxx 2016 xxx 2017 xxx 2018 xxx … …
  • 33. 33 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Activity 1: Combine your knowledge obtained from step 4 and step 5 to complete the following processes ‣ Solutions year profit 2015 xxx 2016 xxx 2017 xxx 2018 xxx … … Desired Output Good Jobs! We get the results we want. However, we are just lucky that the order_year was sorted To ensure that the order_year was sorted in the way we want, the ORDER BY command is required
  • 34. 34 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 6: Sorted by order_date The default sorting mode is ascending Use ASC for ascending sort Use DESC for descending sort Let’s apply sorting to complete the Q1 Discussions: What’s data telling you? Answer: The annual profit was increasing continuously except 2020 Be careful: DO NOT too hurry to conclude that we are having problem in 2020 because the data we have may not be collected from a whole 2020
  • 35. 35 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Let’s check • When was the latest order in our data Idea: Use MAX to get the maximum value in a given column Use MIN to get the minimum value in a given column It is cleared that data in 2020 is only from January
  • 36. 36 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Activity 2: Checking whether we have data in a whole 2015 or not Too small number. That’s why we need to check
  • 37. 37 | © 2025 True Digital Academy Q1:ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Activity 2: Checking whether we have data in a whole 2015 or not Solutions It is cleared that we have only data from December in 2015
  • 38. 38 | © 2025 True Digital Academy SMART Objective and Hypothesis Tree Problem Statement การขายเฟอร ์นิเจอร ์ไม่ทากาไร SMART Objective สร ้างกาไรจากการขาย เฟอร ์นิเจอร ์อย่างน้อย x% ภายใน 6 เดือนด้วยการศึกษา ข้อมูลการซื้อขายในอดีต ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี เฟอร ์นิเจอร ์กาไรตกทุกชนิด เลยไหม ถ้าไม่ใช่งั้นอะไรคือ ปัญหา Idea: เพิ่มขาย ลดทุน เท่ากับกาไร ดังนั้นคาถามทั้งหลายจะเกี่ยวข้องกับการ เพิ่มขายหรือการลดทุนในช่วง 1 ปีที่ผ่านมา ของสินค้าเฟอร ์นิเจอร ์ Note: เพื่อให้ง่ายต่อการทาความเข้าใจ ใน ตัวอย่างนี้จะโฟกัสเฉพาะใน USA เท่านัน ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
  • 39. 39 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 1: Think first, code later. How does the final output look like year profit category 2015 xxx Furniture 2016 xxx Furniture 2017 xxx Furniture 2018 xxx Furniture … … …. This column is optional to display because we can query only order related to furniture Displaying it, perhaps, useful in the sense of ensuring that we performed the right query
  • 40. 40 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 2: Think first, code later. What is/are input Joining 2 tables (orders and products) via the common field
  • 41. 41 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Get to know SQL JOIN Location Temperature Bangkok 35 Rayong 34 Phuket 33 Prae 28 Location Area sq.km Bangkok 1,569 Chonburi 4,363 Phuket 543 Chiang Rai 11,678 Location Location Temperature Bangkok 35 Rayong 34 Phuket 33 Prae 28 Area sq.km 1,569 NULL 543 NULL Output Example
  • 42. 42 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Get to know SQL JOIN Location Temperature Bangkok 35 Rayong 34 Phuket 33 Prae 28 Location Area sq.km Bangkok 1,569 Chonburi 4,363 Phuket 543 Chiang Rai 11,678 Location Output Location Area sq.km Bangkok 1,569 Chonburi 4,363 Phuket 543 Chiang Rai 11,678 Temperature 35 NULL 33 NULL Example
  • 43. 43 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Get to know SQL JOIN Location Temperature Bangkok 35 Rayong 34 Phuket 33 Prae 28 Location Area sq.km Bangkok 1,569 Chonburi 4,363 Phuket 543 Chiang Rai 11,678 Location Output Location Temperature Bangkok 35 Phuket 33 Area sq.km 1,569 543 Example
  • 44. 44 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Get to know SQL JOIN Location Temperature Bangkok 35 Rayong 34 Phuket 33 Prae 28 Location Area sq.km Bangkok 1,569 Chonburi 4,363 Phuket 543 Chiang Rai 11,678 Location Output Location Temperature Bangkok 35 Rayong 34 Phuket 33 Prae 28 Chonburi NULL Chiang Rai NULL Area sq.km 1,569 NULL 543 NULL 4,363 11,678 Example
  • 45. 45 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 3: Joining orders and products • Question: Which JOIN should be applied • Answer: Depends on the unit of analysis Original Question: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี orders Each row represents an order of a particular product ordered by a particular customer from any region products Each row represents a unique product The unit of analysis to answer the original question is an order a particular product The proper joining are one of the following options 1. LEFT join (orders, products) 2. RIGHT JOIN (products, orders) 3. INNER JOIN (orders, products) 4. INNER JOIN (products, orders)
  • 46. 46 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 3: Joining orders and products (cont.) • Let’s start with the LEFT JOIN From orders table From products table
  • 47. 47 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Now we have got the input table ‣ Step 4: Think first code later. How to process input to the desired output year profit category 2015 xxx Furniture 2016 xxx Furniture 2017 xxx Furniture 2018 xxx Furniture … … …. Desired output Extract year Group By year and category Aggregate by Summation Filter only Furniture Sorted by year
  • 48. 48 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 4: Select columns • order_date: Extracting year from order date • Category • profit We have modified the previous query in step 3 This is still not answering the original question. We need further processes
  • 49. 49 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 5: Group By and Aggregation • GROUP BY: Year and Category • Aggregation: Sum of profit We have modified the previous query in step 4 We only want Furniture category. Let’s filter others out using HAVING
  • 50. 50 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 6: Filter other categories out. Keep only Furniture • HAVING is used to filter measures you’ve aggregated Year Values 2019 100 2019 120 2020 200 2020 180 annual SUM SUM Year Values 2019 220 2020 380 HAVING Year=2019 Year Values 2019 220 Basic Idea
  • 51. 51 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 6: Filter other categories out. Keep only Furniture We have modified the previous query in step 4 This query results is very close to our desired output We only need to sort by year
  • 52. 52 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Step 7: Apply sorting Good jobs! We have completed to process data from input to the desired output. This is the heart of programming which is the logic to process input to the desired output. Question: What is the results telling you? Note: We have already known that 2015 and 2020 are not full year data Answer: The cumulative profit of furniture selling is growing up in each year Let’s move to the next analytic question
  • 53. 53 | © 2025 True Digital Academy Q2: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี ‣ Recall from step 3, we have 4 possible JOINs for this problem The proper joining are one of the following options 1. LEFT join (orders, products) 2. RIGHT JOIN (products, orders) 3. INNER JOIN (orders, products) 4. INNER JOIN (products, orders) You may check the results 1 2 3 4
  • 54. 54 | © 2025 True Digital Academy SMART Objective and Hypothesis Tree Problem Statement การขายเฟอร ์นิเจอร ์ไม่ทากาไร SMART Objective สร ้างกาไรจากการขาย เฟอร ์นิเจอร ์อย่างน้อย x% ภายใน 6 เดือนด้วยการศึกษา ข้อมูลการซื้อขายในอดีต ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี เฟอร ์นิเจอร ์กาไรตกทุกชนิด เลยไหม ถ้าไม่ใช่งั้นอะไรคือ ปัญหา Idea: เพิ่มขาย ลดทุน เท่ากับกาไร ดังนั้นคาถามทั้งหลายจะเกี่ยวข้องกับการ เพิ่มขายหรือการลดทุนในช่วง 1 ปีที่ผ่านมา ของสินค้าเฟอร ์นิเจอร ์ Note: เพื่อให้ง่ายต่อการทาความเข้าใจ ใน ตัวอย่างนี้จะโฟกัสเฉพาะใน USA เท่านัน ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019 ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
  • 55. 55 | © 2025 True Digital Academy Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019 ‣ Step 1: Think first, code later. How does the final output look like year Month profit category subregion country 2019 1 xxx Furniture … United States 2019 2 xxx Furniture … United States 2019 … xxx Furniture … United States 2019 … xxx Furniture … United States … … … …. … United States
  • 56. 56 | © 2025 True Digital Academy Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019 ‣ Step 2: Think first, code later. What is/are input Joining 2 tables (orders and products) via the common field Joining 2 tables (orders and regions) via the common field We need these columns We need these columns We need these columns
  • 57. 57 | © 2025 True Digital Academy Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019 ‣ Activity 3: Use your knowledge from previous questions to merge 3 tables (orders, products, regions) together and select the following columns • Orders • Order_date • Extract month • Extract year • Profit • Products • Category • Regions • Sub_region • Country
  • 58. 58 | © 2025 True Digital Academy Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019 ‣ Activity 3: Use your knowledge from previous questions to merge 3 tables (orders, products, regions) together and select the following columns Solutions
  • 59. 59 | © 2025 True Digital Academy Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019 ‣ Now we have the input from the activity 3 ‣ Step 3: Think first code later. How to process input to the desired output year Month profit categor y subregi on country 2019 1 xxx Furniture … United States 2019 2 xxx Furniture … United States 2019 … xxx Furniture … United States 2019 … xxx Furniture … United States … … … …. … United States Group By all except profit Aggregate by Summation Filter by Furniture, United States and 2019 Sorting
  • 60. 60 | © 2025 True Digital Academy Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019 ‣ Step 4: Group By and Aggregation • Group BY all except profit • Aggregate profit by summation We have modified the previous query in activity 3
  • 61. 61 | © 2025 True Digital Academy Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019 ‣ Step 5: Filtered by Furniture, United States and 2019 We have modified the previous query in step 4
  • 62. 62 | © 2025 True Digital Academy Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019 ‣ Step 6: Sorting • Tips: • We are looking for non-profitable evidences • Apply sorting directly to cum_profit returns the serious non-profitable evidences, but we will loss the trend monitoring • Trade-off between focus to the non-profitable evidences and trend monitoring
  • 63. 63 | © 2025 True Digital Academy Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019 ‣ Step 6: Sorting • Option 1: Focus on non-profitable evidences Question: What is the results telling you? Answer: There are several non-profitable evidences in 2019, but June (East and South), July (West), and February (Central) seems to be very seriously non-profitable evidences We need to further investigate what went wrong in those evidences We have modified the previous query in step 5
  • 64. 64 | © 2025 True Digital Academy Q3: ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ในแต่ละเดือนของปี 2019 ‣ Step 6: Sorting • Option 2: Focus on trend monitoring We have modified the previous query in step 5 Question: What do the results tell you? Answer: The cumulative profit, for example, in 2019 at Central USA is fluctuating. There are some non- profitable evidences in Feb, Apr, and Oct. We need to further investigate what went wrong in those evidences and other subregions
  • 65. 65 | © 2025 True Digital Academy SMART Objective and Hypothesis Tree Problem Statement การขายเฟอร ์นิเจอร ์ไม่ทากาไร SMART Objective สร ้างกาไรจากการขาย เฟอร ์นิเจอร ์อย่างน้อย x% ภายใน 6 เดือนด้วยการศึกษา ข้อมูลการซื้อขายในอดีต ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี เฟอร ์นิเจอร ์กาไรตกทุกชนิด เลยไหม ถ้าไม่ใช่งั้นอะไรคือ ปัญหา Idea: เพิ่มขาย ลดทุน เท่ากับกาไร ดังนั้นคาถามทั้งหลายจะเกี่ยวข้องกับการ เพิ่มขายหรือการลดทุนในช่วง 1 ปีที่ผ่านมา ของสินค้าเฟอร ์นิเจอร ์ Note: เพื่อให้ง่ายต่อการทาความเข้าใจ ใน ตัวอย่างนี้จะโฟกัสเฉพาะใน USA เท่านัน Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019 ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ใน แต่ละเดือนของปี 2019 ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี
  • 66. 66 | © 2025 True Digital Academy Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019 ‣ Step 1: Think first, code later. How does the final output look like year Month profit category subcategory Product name subregion country 2019 1 xxx Furniture … … … United States 2019 2 xxx Furniture … … … United States 2019 … xxx Furniture … … … United States 2019 … xxx Furniture … … … United States … … … …. … … … United States Must be negative values
  • 67. 67 | © 2025 True Digital Academy Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019 ‣ Step 2: Think first, code later. What is/are input Joining 2 tables (orders and products) via the common field Joining 2 tables (orders and regions) via the common field We need these columns We need these columns We need these columns
  • 68. 68 | © 2025 True Digital Academy Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019 ‣ Activity 4: Use your knowledge from previous questions to merge 3 tables (orders, products, regions) together and select the following columns • Orders • Order_date • Extract month • Extract year • Profit • Products • Category • Subcategory • Product name • Regions • Sub_region • Country
  • 69. 69 | © 2025 True Digital Academy Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019 ‣ Activity 4: Use your knowledge from previous questions to merge 3 tables (orders, products, regions) together and select the following columns Solutions
  • 70. 70 | © 2025 True Digital Academy Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019 ‣ Now we have input ‣ Step 3: Think first code later. How to process input to the desired output Group By all except profit Aggregate by Summation Filter by Furniture, United States, 2019, and negative profit Sorting year Mon th prof it cate gory sub cate gory Pro duct nam e subr egio n cou ntry 2019 1 xxx Furni ture … … … Unit ed Stat es 2019 2 xxx Furni ture … … … Unit ed Stat es 2019 … xxx Furni ture … … … Unit ed Stat es … … … …. … … … Unit ed Stat es
  • 71. 71 | © 2025 True Digital Academy Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019 ‣ Step 4: Group By and Aggregation • Group BY all except profit • Aggregate profit by summation We have modified the previous query in activity 4
  • 72. 72 | © 2025 True Digital Academy Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019 ‣ Step 5: Filtered the group with Furniture, United States, 2019, and negative cumulative profit We have modified the previous query in step 4
  • 73. 73 | © 2025 True Digital Academy Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019 ‣ Step 6: Sorting • Pinpoint to which products and their non-profitable evidences • Sorting priority focus on cumulative profit • Rank top 10 of the non-profitable evidences ‣ The LIMIT command is used for displaying only “nrows” of the query results (works only in PostgresQL)
  • 74. 74 | © 2025 True Digital Academy Q4: Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019 ‣ Step 6: Sorting (Cont.) Question: What is the results telling us? Answer: 3 subcategories (table, chair, bookcases) were found with serious non-profitable evidences
  • 75. 75 | © 2025 True Digital Academy SMART Objective and Hypothesis Tree Problem Statement การขายเฟอร ์นิเจอร ์ไม่ทากาไร SMART Objective สร ้างกาไรจากการขาย เฟอร ์นิเจอร ์อย่างน้อย x% ภายใน 6 เดือนด้วยการศึกษา ข้อมูลการซื้อขายในอดีต ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี เฟอร ์นิเจอร ์กาไรตกทุกชนิด เลยไหม ถ้าไม่ใช่งั้นอะไรคือ ปัญหา Idea: เพิ่มขาย ลดทุน เท่ากับกาไร ดังนั้นคาถามทั้งหลายจะเกี่ยวข้องกับการ เพิ่มขายหรือการลดทุนในช่วง 1 ปีที่ผ่านมา ของสินค้าเฟอร ์นิเจอร ์ Note: เพื่อให้ง่ายต่อการทาความเข้าใจ ใน ตัวอย่างนี้จะโฟกัสเฉพาะใน USA เท่านัน Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019 ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ใน แต่ละเดือนของปี 2019 ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี Product ที่มี serious non-profitable evidences มีการลด ราคาอย่างไร เช่น Sauder Classic Bookcases, Metal ที่มี serious non-profitable evidence หนักที่สุด ใน United States ในปี 2019 โครงสร ้างของราคาผิดปกติ หรือไม่
  • 76. 76 | © 2025 True Digital Academy Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร ‣ Step 1: Think first, code later. How does the final output look like Year Month Product Name SUM Quantity AVG Sales AVG Discount SUM profit 2019 … … … … … … 2019 … … … … … … 2019 … … … … … … 2019 … … … … … … 2019 … … … … … …
  • 77. 77 | © 2025 True Digital Academy Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร ‣ Step 2: Think first, code later. What is/are input Joining 2 tables (orders and products) via the common field We need these columns We need these columns Joining 2 tables (orders and regions) via the common field We need these columns
  • 78. 78 | © 2025 True Digital Academy Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร ‣ Activity 5: Use your knowledge from previous questions to merge 3 tables (orders, products, regions) together and select the following columns • Orders • Order_date • Extract Year/ Month • Quantity • Sales • Discount • Profit • Products • Product Name • Regions • Country
  • 79. 79 | © 2025 True Digital Academy Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร ‣ Activity 5: Use your knowledge from previous questions to merge 3 tables (orders, products, regions) together and select the following columns
  • 80. 80 | © 2025 True Digital Academy Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร ‣ Now we have input ‣ Step 3: Think first code later. How to process input to the desired output Group By all except profit Aggregate by SUM and Average Filter by Furniture, United States, 2019, and product_name ‘Saunder Classic Bookcase, Metal’ Sorting Year Mont h Product Name SUM Quantity AVG Sales AVG Discount SUM profit 2019 … … … … … … 2019 … … … … … … 2019 … … … … … … 2019 … … … … … … 2019 … … … … … …
  • 81. 81 | © 2025 True Digital Academy Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร ‣ Filtering, WHERE command is used for filtering non-aggregated data locations Temperature Bangkok 35 Rayong 34 Phuket 33 Prae 28 locations Temperature Bangkok 35
  • 82. 82 | © 2025 True Digital Academy Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร ‣ Step 4: Filtering United States, 2019 and product name using WHERE We have modified the previous query in step 3 “Sauder Classic Bookcase, Metal” was picked for investigation because it has the most serious non- profitable evidences Try LIKE keyword LIKE “Sauder%” or ILIKE for PostgresQL
  • 83. 83 | © 2025 True Digital Academy Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร ‣ Step 5: Group By and Aggregation (remove select country) • Group By all except quantity, profit, discount, and sales • Aggregate by average and summation We have modified the previous query in activity 5
  • 84. 84 | © 2025 True Digital Academy Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร ‣ Step 6: Sorting Question: What the results telling you Answer: Something wrong with the discount. In common sense, the more purchase quantity the more discount can be. However, we observed that order in February has average discount about half of October, but the purchasing quantity shows contradiction to the common sense. Moreover, cumulative profit in February is obviously extreme compared to others. Only average value may not be able to reveal fact. Perhaps, statistical parameters could help.
  • 85. 85 | © 2025 True Digital Academy Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร ‣ Step 7: Add some statistical parameters to sales • Min • Max • Standard deviation Question: What is the results telling you? Answer: It tells not much for this case. We observe that the non-profitable evidences have constant selling price Be careful, do not hurry to conclude because statistics could mislead to you. Further investigation in transaction level on specific product and time is required
  • 86. 86 | © 2025 True Digital Academy Q5: Product ที่มี serious non-profitable evidences มีการลดราคาต่างจาก Product อื่นอย่างไร ‣ Statistical could mislead you
  • 87. 87 | © 2025 True Digital Academy SMART Objective and Hypothesis Tree Problem Statement การขายเฟอร ์นิเจอร ์ไม่ทากาไร SMART Objective สร ้างกาไรจากการขาย เฟอร ์นิเจอร ์อย่างน้อย x% ภายใน 6 เดือนด้วยการศึกษา ข้อมูลการซื้อขายในอดีต ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละปี เฟอร ์นิเจอร ์กาไรตกทุกชนิด เลยไหม ถ้าไม่ใช่งั้นอะไรคือ ปัญหา Idea: เพิ่มขาย ลดทุน เท่ากับกาไร ดังนั้นคาถามทั้งหลายจะเกี่ยวข้องกับการ เพิ่มขายหรือการลดทุนในช่วง 1 ปีที่ผ่านมา ของสินค้าเฟอร ์นิเจอร ์ Note: เพื่อให้ง่ายต่อการทาความเข้าใจ ใน ตัวอย่างนี้จะโฟกัสเฉพาะใน USA เท่านัน Product ใดในกลุ่มเฟอร ์นิเจอร ์ที่ขายแล้วขาดทุนจาแนกตาม subregion ใน USA เฉพาะปี 2019 ทิศทางของกาไรจากการขายเฟอร ์นิเจอร ์ที่ผ่านมาเป็ นอย่างไรในแต่ละ sub region ที่ USA ใน แต่ละเดือนของปี 2019 ทิศทางของกาไรที่ผ่านมาเป็ นอย่างไรในแต่ละปี Product ที่มี serious non-profitable evidences มีการ ลดราคาอย่างไร เช่น Sauder Classic Bookcases, Metal ที่มี serious non-profitable evidence หนักที่สุด ใน United States ในปี 2019 โครงสร ้างของราคาผิดปกติ หรือไม่ Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด เช่น Sauder Classic Bookcases, Metal ที่มี serious non-profitable evidence หนักที่สุดใน United States ในปี 2019
  • 88. 88 | © 2025 True Digital Academy Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด ‣ Step 1: Think first, code later. How does the final output look like year month Order ID Product name quantity sales discount profit Profit margin
  • 89. 89 | © 2025 True Digital Academy Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด ‣ Step 2: Think first, code later. What is/are input Joining 2 tables (orders and products) via the common field We need these columns We need these columns Joining 2 tables (orders and regions) via the common field We need these columns
  • 90. 90 | © 2025 True Digital Academy Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด ‣ Activity 6: Use your knowledge from previous questions to merge 3 tables (orders, products, regions) together and select the following columns ‣ Select only relevant columns defined in step 2
  • 91. 91 | © 2025 True Digital Academy Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด ‣ Activity 6: Use your knowledge from previous questions to merge 3 tables (orders, products, regions) together and select the following columns ‣ Select only relevant columns defined in step 2 Solution
  • 92. 92 | © 2025 True Digital Academy Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด ‣ Now we have input ‣ Step 3: Think first code later. How to process input to the desired output Calculate profit margin Filter by Furniture, United States, 2019, and product_name Sorting year month Order ID Product name quantity sales discount profit Profit margin
  • 93. 93 | © 2025 True Digital Academy Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด ‣ Step 4: Calculate profit margin We have modified the previous query in activity 6
  • 94. 94 | © 2025 True Digital Academy Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด ‣ Step 4: Calculate profit margin • Step 4.1 Apply decimal round to make display results easier to read We have modified the previous query in the previous step 4
  • 95. 95 | © 2025 True Digital Academy Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด ‣ Step 5: Filtering United States, 2019, and Sauder Classic Bookcase, Metal We have modified the previous query in the previous step 4.1
  • 96. 96 | © 2025 True Digital Academy Q6: Product ที่มี serious non-profitable evidences มี transaction ผิดปกติหรือไม่ แต่ละ transaction มี profit margin เท่าใด ‣ Step 6: Sorting Question: What is the results telling you? Answer: For Sauder Classic Bookcase, Metal sold in 2019 at USA, overall transactions look fine except IT- 2019-3150765 that has the same quantity, sales, and discount as other February transactions, but profit looks very abnormal, and this transaction is the most serious non-profitable evidence. We do not have any further data to investigate what exactly went wrong about it. We could suspect that might involve with human error, something outside the regular purchasing system, etc.
  • 97. 97 | © 2025 True Digital Academy DATA ORGANIZATION CONCLUSION
  • 98. 98 | © 2025 True Digital Academy SQL IN A NUTSHELL ✔ SELECT: Selects the columns. ✔ FROM: Points to the table. ✔ WHERE: Filters on rows. ✔ GROUP BY: Aggregates across values of a variable. ✔ HAVING: Filters groups. ✔ ORDER BY: Sorts or arranges the results. ✔ LIMIT: Limits result to the first n rows. Also: (INNER/LEFT/RIGHT/FULL)JOIN: Combine columns across tables with the relationships.
  • 99. 99 | © 2025 True Digital Academy DATA ORGANIZATION WITH SQL HOMEWORK 3 (กาหนดส่งก่อนเรียนครั้ง หน้า 29 มี.ค.)
  • 100. 100 | © 2025 True Digital Academy DATA ORGANIZATION DIRECTIONS ‣ ทำตำมสไลด์ที่เหลือ (slide 87 – 96) ‣ ตั้งคำถำมเพิ่ม 1 คำถำมนอกเหนือจำกคำถำมในห้อง พร้อมทั้งตอบคำถำมด้วย SQL โดยต้องมีกำรใช้ GROUP BY + Aggregation DELIVERABLES 1. ส่ง SQL statement, พร้อม capture หน้ำจอกำรใช้งำนและตัวอย่ำงผลลัพธ์ที่ได้ อธิบำยสิ่งที่ได้โดยสังเขป(คร่ำวๆ) 2. ส่งเป็น PDF 3. ตั้งชื่อไฟล์ขึ้นต้นด้วย HW3_ชื่อนำมสกุล หย่อนลงใน Google Drive โฟลเดอร์ชื่อนำมสกุล ตนเอง
  • 101. Exit Ticket (เลือก Week 3: 22-Mar-25) https://bit.ly/4gKS4x9
  翻译: