Не попадитесь на накрученные каналы! Узнайте, не накручивает ли канал просмотры или
подписчиков
Проверить канал на накрутку
Телеграм канал «Data Science & Machine Learning»
Data Science & Machine Learning
4.0K
253
1.5K
344
7.2K
Join this channel to learn data science, artificial intelligence and machine learning with funny quizzes, interesting projects and amazing resources for free
SELECT p.product_name,
SUM(o.quantity * o.price) AS revenue
FROM orders o
INNER JOIN products p
ON o.product_id = p.product_id
GROUP BY p.product_name
ORDER BY revenue DESC;
This combines:
Orders
↓
Products
↓
GROUP BY
↓
Revenue calculation
↓
ORDER BY
↓
Highest revenue first
This type of SQL is directly useful for preparing datasets for Data Science and business analysis.
🔹 Common JOIN Mistakes
Mistake 1 — Missing the JOIN condition
Incorrect:
SELECT *
FROM customers
JOIN orders;
Unless intentionally using a CROSS JOIN, you normally need a relationship condition.
Mistake 2 — Joining on the wrong column
Always identify the actual relationship between the tables.
For example:
customers.customer_id
↕
orders.customer_id
Mistake 3 — Unexpected duplicate rows
Suppose one customer has 10 orders.
After joining:
Customer A
Customer A
Customer A
...
may appear 10 times.
This is not necessarily an error.
The JOIN is representing the one-to-many relationship.
Mistake 4 — Accidentally turning a LEFT JOIN into an INNER JOIN
Consider:
SELECT c.name, o.amount
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.amount > 1000;
The WHERE condition removes rows where o.amount is NULL, so customers without orders disappear.
Understanding how filtering interacts with joins is very important.
🔹 JOIN Cheat Sheet
INNER JOIN
→ Matching rows from both tables
LEFT JOIN
→ All left rows + matching right rows
RIGHT JOIN
→ All right rows + matching left rows
FULL OUTER JOIN
→ All rows from both tables
CROSS JOIN
→ Every possible combination
SELF JOIN
→ A table joined with itself
🔹 Interview Questions
1. What is a JOIN?
A JOIN combines data from multiple tables using a related column or condition.
2. What does INNER JOIN return?
Only rows with matching values in both tables.
3. What does LEFT JOIN return?
All rows from the left table and matching rows from the right table.
4. How do you find customers who have never placed an order?
Use a LEFT JOIN and check for NULL on the right table.
SELECT c.name
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL;
5. Why can a JOIN produce duplicate-looking rows?
Because of one-to-many or many-to-many relationships between tables.
🎯 Practice Questions
1. Write an INNER JOIN to display customer names and their order IDs.
2. Write a LEFT JOIN to display all customers, including customers who have no orders.
3. Find customers who have never placed an order.
4. Calculate total spending for each customer using JOIN and SUM().
5. Join orders and products and calculate total revenue for each product.
🎯 Key Takeaways
• JOIN combines data from multiple tables.
• INNER JOIN returns matching records.
• LEFT JOIN keeps every record from the left table.
• RIGHT JOIN keeps every record from the right table.
• FULL OUTER JOIN keeps records from both sides.
• CROSS JOIN creates every possible combination.
• SELF JOIN joins a table with itself.
• ON defines the relationship between tables.
• JOINs are fundamental for real-world Data Science and Analytics.
If an order has no matching customer, the customer columns can contain NULL.
Simple rule:
RIGHT JOIN → Everything from right + matching data from left
In practice, many analysts prefer rewriting a RIGHT JOIN as a LEFT JOIN by switching the table order because it can make queries easier to read.
🔹 4. FULL OUTER JOIN
FULL OUTER JOIN returns:
• Matching rows
• Unmatched rows from the left table
• Unmatched rows from the right table
Example:
SELECT c.customer_id,
c.name,
o.order_id
FROM customers c
FULL OUTER JOIN orders o
ON c.customer_id = o.customer_id;
Conceptually:
Customers only
+
Matching records
+
Orders only
Not every SQL database supports FULL OUTER JOIN in the same way, so database-specific syntax matters.
🔹 5. CROSS JOIN
A CROSS JOIN produces every possible combination of rows between two tables.
Suppose:
colors
Red
Blue
sizes
Small
Large
Query:
SELECT color, size
FROM colors
CROSS JOIN sizes;
Result:
Red Small
Red Large
Blue Small
Blue Large
2 colors × 2 sizes = 4 combinations.
This is called a Cartesian product.
Use it carefully because the number of rows can become very large.
🔹 6. SELF JOIN
A SELF JOIN joins a table to itself.
This is useful when rows in the same table have relationships with other rows.
For example, an employee table:
employee_id employee_name manager_id
101 Alice NULL
102 Bob 101
103 Carol 101
You can use a self join to display each employee and their manager:
SELECT e.employee_name AS employee,
m.employee_name AS manager
FROM employees e
LEFT JOIN employees m
ON e.manager_id = m.employee_id;
Result:
employee manager
Alice NULL
Bob Alice
Carol Alice
🔹 JOIN Conditions with ON
The ON clause specifies how the tables should be connected.
ON customers.customer_id = orders.customer_id
This means:
customers.customer_id
=
orders.customer_id
The two columns don't necessarily need to have the same name.
For example:
ON customers.id = orders.customer_id
can also be valid if those columns represent the same relationship.
🔹 Using Table Aliases
Aliases make JOIN queries much easier to read.
Instead of:
SELECT customers.customer_name,
orders.amount
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id;
You can write:
SELECT c.customer_name,
o.amount
FROM customers c
INNER JOIN orders o
ON c.customer_id = o.customer_id;
Here:
c → customers
o → orders
🔹 JOIN with WHERE
You can filter the joined result.
SELECT c.name,
o.amount
FROM customers c
INNER JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.amount > 1000;
This returns matching customers whose orders exceed 1000.
🔹 JOIN with GROUP BY
You can also aggregate after joining.
Suppose you want total spending by customer:
SELECT c.customer_id,
c.name,
SUM(o.amount) AS total_spent
FROM customers c
INNER JOIN orders o
ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.name;
This is extremely common in analytics.
🔹 Real-World Data Science Example
Imagine you have three tables:
customers
orders
products
You want to calculate how much revenue each product generated.
🚀 Data Science: Important Concepts You Should Know
Data Science combines statistics, programming, data analysis, machine learning, and business understanding to extract useful insights and make better decisions from data.
1. What is Data Science?
Data Science is the process of collecting, cleaning, analyzing, and modeling data to answer questions, discover patterns, and support decisions.
Example:
A company can use customer data to understand why customers leave, predict future sales, and identify high-value customers.
2. Data
Data is the raw information we work with.
Examples:
• Customer records
• Sales transactions
• Website activity
• Financial data
• Sensor readings
Data can be structured, semi-structured, or unstructured.
3. Dataset
A dataset is a collection of related data, usually organized into rows and columns.
Example:
Customer | Age | Income | Purchase
A | 25 | 40K | Yes
B | 34 | 70K | No
4. Variables
Variables are characteristics or attributes measured in the data.
Examples:
• Age
• Salary
• Department
• Sales
• Customer status
They can be numerical or categorical.
5. Data Collection
Data can come from:
• Databases
• APIs
• Surveys
• Excel files
• Websites
• Applications
• Sensors
• Business systems
Good analysis starts with reliable and relevant data.
6. Data Cleaning
Real-world data is rarely perfect.
Data cleaning involves handling:
• Missing values
• Duplicates
• Incorrect formats
• Invalid values
• Inconsistent categories
• Outliers
Garbage in → garbage out.
7. Missing Values
Missing values occur when information isn't available.