惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
WordPress大学
WordPress大学
D
DataBreaches.Net
腾讯CDC
小众软件
小众软件
B
Blog RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Blog of Author Tim Ferriss
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Y
Y Combinator Blog
V
V2EX
I
InfoQ
D
Docker
量子位
N
Netflix TechBlog - Medium
Recent Announcements
Recent Announcements
A
About on SuperTechFans
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog
阮一峰的网络日志
阮一峰的网络日志
MyScale Blog
MyScale Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Understanding SQL Joins and SQL Functions, CTEs and Subqu...
Joseous Ng'a · 2026-05-06 · via DEV Community

As my journey in becoming a competent data analytics, my SQL knowledge continues to deepen and as a result, I also publish few things pick up through the process.
couple of weeks back I published about SQL fundamentals, covering that is DDL, DML and Data Manipulation. Read more about SQL fundamentals from this link Click here to visit dev.to.

Building on SQL skills and data analysis, I have come to know you can work on different tables at the same time through the help of SQL joins and SQL Functions.

What is Join?

A JOIN in SQL is used to link or combine rows from two or more tables based on related column between them and it is usually a Primary Key and Foreign Key.
Think of it like, one table has students and another has scores, JOIN will help you see which student took which course.

Types of Joins

  • LEFT JOIN (LEFT OUTER JOIN) This type of returns all records from Left table and matching records from the right table

Example:

SELECT s.name, c.course_name
FROM students s
LEFT JOIN courses c
ON s.course_id = c.id;

Enter fullscreen mode Exit fullscreen mode

All students appear, even if they are not assigned a course.

INNER JOIN
This type of JOIN returns only matching records from both tables.

Example:

SELECT s.name, c.course_name
FROM students s
INNER JOIN courses c
ON s.course_id = c.id;

Enter fullscreen mode Exit fullscreen mode

RIGHT JOIN (RIGHT OUTER JOIN)
The JOIN returns all records from the right table and matching ones from the left.

Example

SELECT s.name, c.course_name
FROM students s
RIGHT JOIN courses c
ON s.course_id = c.id;

Enter fullscreen mode Exit fullscreen mode

All courses appear, even if no student is enrolled.

FULL JOIN(FULL OUTER JOIN)
This type of JOIN returns all records when there is a match in either table.

Example:

SELECT s.name, c.course_name
FROM students s
FULL JOIN courses c
ON s.course_id = c.id;

Enter fullscreen mode Exit fullscreen mode

  • JOINs are about relationships between tables
  • Without JOINs, databases would be much less powerful

SQL Window Functions

What are Window Functions?
Window functions are used to calculate across rows without collapsing them.

Example:

SELECT name, department, salary,
       AVG(salary) OVER (PARTITION BY department) AS avg_salary
FROM employees;

Enter fullscreen mode Exit fullscreen mode

The output from this query, every employee still appears, which also includes department average.

SQL functions Every Beginner Should Know

COUNT():

The function counts rows present in a given table.

Example:

SELECT COUNT(*) FROM students;

Enter fullscreen mode Exit fullscreen mode

Counts total number of students

SUM():

This function is used to add numeric values.

Example:

SELECT SUM(salary) FROM employees;

Enter fullscreen mode Exit fullscreen mode

This will get the total salary.

AVERAGE():

The function is used to get Average values like school exam result performance.

Example:

SELECT AVG(marks) FROM exams;

Enter fullscreen mode Exit fullscreen mode

The output will give the average marks.

UPPER()/LOWER():

The function is used to get or change text case,
UPPER() is used to change text into upper case while
LOWER() is used to change text into lower case.

Example:

SELECT 
   UPPER(first_name),
   LOWER(last_name) 
FROM students;

Enter fullscreen mode Exit fullscreen mode

The student's first name will be in upper case and second name will be in lower case.

NOW()/CURRENT_DATE():
This function is used to get current date or time. It is useful when filtering recent records.

Example:

SELECT CURRENT_DATE;

Enter fullscreen mode Exit fullscreen mode

In SQL functions are many, the highlighted functions are most common and every beginner should know and understand how they work in order to ease the work as data analysts, data engineer or scientist.

SQL Subqueries

What is Subquery in SQL:
A Subquery is a query written inside another SQL Query, It executes first and its result is used by the outer query.

Example:

SELECT name, salary
FROM employees
WHERE salary > (
    SELECT AVG(salary)
    FROM employees
);

Enter fullscreen mode Exit fullscreen mode

  • The inner query calculates average salary and the outer query compares the employee's salary against average

Types of Subquery
We have different types of subqueries, they include;

Scalar Subquery: It is used to return single value.

Example:

SELECT *
FROM products
WHERE price > (
    SELECT AVG(price)
    FROM products
);

Enter fullscreen mode Exit fullscreen mode

Multiple-row Subquery: This subquery returns multiple rows as its name suggests.

Example:

SELECT name
FROM employees
WHERE department_id IN (
    SELECT id
    FROM departments
);

Enter fullscreen mode Exit fullscreen mode

Correlated Subquery: This query references one or more columns from the outer(main) query, it depends on the outer query.

Example:

SELECT e1.name, e1.salary
FROM employees e1
WHERE salary > (
    SELECT AVG(e2.salary)
    FROM employees e2
    WHERE e1.department = e2.department
);

Enter fullscreen mode Exit fullscreen mode

The query compares each employee to the average salary in their department.

Common Table Expression (CTE)

What is CTE?
A CTE (Common Table expression) is a temporary named result set created using the With clause.
CTE makes query easier to organize and read.

Example: same query using a CTE

WITH avg_salary AS (
    SELECT AVG(salary) AS avg_sal
    FROM employees
)

SELECT name, salary
FROM employees
WHERE salary > (
    SELECT avg_sal
    FROM avg_salary
);

Enter fullscreen mode Exit fullscreen mode

Reasons for using CTEs

  • The need for better readability
  • Simply complex queries
  • The need for organized structure
  • If you need to reuse intermediate results

CTE with Multiple Steps
One advantage of CTEs is chaining logic

Example: Monthly sales analysis

WITH monthly_sales AS (
    SELECT
        DATE_TRUNC('month', sale_date) AS month,
        SUM(amount) AS revenue
    FROM sales
    GROUP BY month
),

ranked_sales AS (
    SELECT *,
           RANK() OVER (ORDER BY revenue DESC) AS sales_rank
    FROM monthly_sales
)

SELECT *
FROM ranked_sales;

Enter fullscreen mode Exit fullscreen mode

This will calculate monthly revenue, rank months by revenue and return final results.

SQL Learning Roadmap for a Beginner

SQL Fundamentals

  • JOINs
  • Group BY
  • Window Functions
  • SQL Functions
  • Subqueries
  • CTEs

These will be core concepts needed for:

  • Preparing for SQL technical interview
  • Writing quality SQL scripts
  • Dashboard preparation for tools like Ms Power BI
  • Data analysis

Conclusion

Since now as a data scientist, data manipulation and analysis is easier now that I have learnt and understood SQL Functions, Window Function and also Joins.

Also the use of CTEs and Subquery makes SQL query to be easier to read and organization.
Note: If a subquery starts to become difficult to read, convert it into a CTE