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

推荐订阅源

A
About on SuperTechFans
GbyAI
GbyAI
I
InfoQ
C
Cisco Blogs
T
Tor Project blog
NISL@THU
NISL@THU
N
Netflix TechBlog - Medium
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
H
Help Net Security
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
A
Arctic Wolf
Stack Overflow Blog
Stack Overflow Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Palo Alto Networks Blog
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
P
Privacy International News Feed
L
LINUX DO - 热门话题
Know Your Adversary
Know Your Adversary
L
LangChain Blog
AWS News Blog
AWS News Blog
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
U
Unit 42
P
Privacy & Cybersecurity Law Blog
Spread Privacy
Spread Privacy
WordPress大学
WordPress大学
Latest news
Latest news
L
Lohrmann on Cybersecurity
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
雷峰网
雷峰网
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
S
SegmentFault 最新的问题
Recent Announcements
Recent Announcements
C
Cybersecurity and Infrastructure Security Agency CISA
Martin Fowler
Martin Fowler
S
Schneier on Security
C
CERT Recently Published Vulnerability Notes
B
Blog
Project Zero
Project Zero
Recorded Future
Recorded Future

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 Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
SQL Basics: Talking to a Database for the First Time
Akhilesh · 2026-04-29 · via DEV Community

Every company you will ever work for stores its data in a database.

Not CSV files. Not Excel sheets. A database. Tables of users. Tables of transactions. Tables of products, logs, events, sessions. Millions of rows. Billions sometimes.

To get that data out, you write SQL.

Not Python. Not Pandas. SQL. A completely different language designed specifically for asking questions of relational databases. And unlike Python which took months to feel comfortable, SQL has a core that you can learn in one sitting and use productively within hours.

This is that sitting.


What SQL Actually Is

SQL stands for Structured Query Language. It is not a programming language in the traditional sense. You do not write loops or functions. You describe what data you want and the database figures out how to retrieve it.

That difference matters. In Python you tell the computer how to do something step by step. In SQL you tell it what you want and it decides the how.

SELECT name, salary
FROM employees
WHERE department = 'Engineering'
ORDER BY salary DESC;

Enter fullscreen mode Exit fullscreen mode

Read that out loud. "Select name and salary from the employees table where the department is Engineering, ordered by salary descending." That is exactly what it does. SQL reads like English on purpose.


Setting Up SQLite in Python

You do not need to install a database server. Python comes with SQLite, a lightweight database that lives in a single file. Perfect for learning.

import sqlite3
import pandas as pd

conn = sqlite3.connect("company.db")
cursor = conn.cursor()

cursor.executescript("""
    DROP TABLE IF EXISTS employees;
    DROP TABLE IF EXISTS departments;
    DROP TABLE IF EXISTS projects;

    CREATE TABLE departments (
        dept_id   INTEGER PRIMARY KEY,
        dept_name TEXT NOT NULL,
        budget    INTEGER
    );

    CREATE TABLE employees (
        emp_id     INTEGER PRIMARY KEY,
        name       TEXT NOT NULL,
        age        INTEGER,
        salary     INTEGER,
        dept_id    INTEGER,
        hire_date  TEXT,
        FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
    );

    CREATE TABLE projects (
        project_id  INTEGER PRIMARY KEY,
        emp_id      INTEGER,
        project     TEXT,
        hours       INTEGER,
        FOREIGN KEY (emp_id) REFERENCES employees(emp_id)
    );

    INSERT INTO departments VALUES
        (1, 'Engineering', 500000),
        (2, 'Marketing', 300000),
        (3, 'Sales', 400000),
        (4, 'HR', 200000);

    INSERT INTO employees VALUES
        (1, 'Alex',   25, 55000, 1, '2022-01-15'),
        (2, 'Priya',  30, 82000, 2, '2021-03-22'),
        (3, 'Sam',    22, 43000, 1, '2023-06-01'),
        (4, 'Jordan', 35, 95000, 3, '2019-04-20'),
        (5, 'Lisa',   28, 67000, 2, '2022-11-30'),
        (6, 'Ravi',   31, 71000, 1, '2021-07-14'),
        (7, 'Tom',    42, 88000, 3, '2018-09-05'),
        (8, 'Nina',   26, 59000, 4, '2023-02-28');

    INSERT INTO projects VALUES
        (1, 1, 'AI Platform',     120),
        (2, 3, 'Data Pipeline',    80),
        (3, 2, 'Campaign Tool',    95),
        (4, 6, 'Internal API',    110),
        (5, 4, 'CRM Integration',  60),
        (6, 1, 'Model Monitoring', 45);
""")

conn.commit()
print("Database created with three tables.")

Enter fullscreen mode Exit fullscreen mode

A helper function to run queries and see results cleanly:

def query(sql, conn=conn):
    return pd.read_sql_query(sql, conn)

Enter fullscreen mode Exit fullscreen mode

pd.read_sql_query runs the SQL and returns a DataFrame. Best of both worlds: SQL for querying, Pandas for display and further manipulation.


SELECT: Getting Data Out

The most basic query. Get everything from a table.

result = query("SELECT * FROM employees;")
print(result)

Enter fullscreen mode Exit fullscreen mode

Output:

   emp_id   name  age  salary  dept_id   hire_date
0       1   Alex   25   55000        1  2022-01-15
1       2  Priya   30   82000        2  2021-03-22
2       3    Sam   22   43000        1  2023-06-01
...

Enter fullscreen mode Exit fullscreen mode

* means all columns. Never use SELECT * in production code. Always name the columns you need. It is faster and the code is clearer.

result = query("SELECT name, salary, dept_id FROM employees;")
print(result)

Enter fullscreen mode Exit fullscreen mode

Give columns aliases with AS:

result = query("""
    SELECT
        name        AS employee_name,
        salary      AS annual_salary,
        salary / 12 AS monthly_salary
    FROM employees;
""")
print(result)

Enter fullscreen mode Exit fullscreen mode

Output:

  employee_name  annual_salary  monthly_salary
0          Alex          55000        4583.333
1         Priya          82000        6833.333
2           Sam          43000        3583.333
...

Enter fullscreen mode Exit fullscreen mode

SQL can do math directly in the SELECT. Monthly salary calculated inline. No Python needed.


WHERE: Filtering Rows

result = query("""
    SELECT name, salary
    FROM employees
    WHERE salary > 70000;
""")
print(result)

Enter fullscreen mode Exit fullscreen mode

Output:

    name  salary
0  Priya   82000
1 Jordan   95000
2   Ravi   71000
3    Tom   88000

Enter fullscreen mode Exit fullscreen mode

Comparison operators: =, !=, >, <, >=, <=

Note: SQL uses single = for equality comparison, not ==.

Multiple conditions:

result = query("""
    SELECT name, age, salary
    FROM employees
    WHERE salary > 60000
      AND age < 35;
""")
print(result)

Enter fullscreen mode Exit fullscreen mode

Output:

   name  age  salary
0 Priya   30   82000
1  Lisa   28   67000
2  Ravi   31   71000

Enter fullscreen mode Exit fullscreen mode

AND, OR, NOT work exactly as you expect.

result = query("""
    SELECT name, dept_id
    FROM employees
    WHERE dept_id IN (1, 3);
""")
print(result)

Enter fullscreen mode Exit fullscreen mode

IN is cleaner than multiple OR conditions for checking against a list.

result = query("""
    SELECT name, salary
    FROM employees
    WHERE salary BETWEEN 60000 AND 85000;
""")
print(result)

Enter fullscreen mode Exit fullscreen mode

result = query("""
    SELECT name
    FROM employees
    WHERE name LIKE 'J%';
""")
print(result)

Enter fullscreen mode Exit fullscreen mode

LIKE with % as wildcard. 'J%' matches anything starting with J. '%son' matches anything ending with son. '%or%' matches anything containing "or".


ORDER BY and LIMIT

result = query("""
    SELECT name, salary
    FROM employees
    ORDER BY salary DESC
    LIMIT 3;
""")
print(result)

Enter fullscreen mode Exit fullscreen mode

Output:

     name  salary
0  Jordan   95000
1     Tom   88000
2   Priya   82000

Enter fullscreen mode Exit fullscreen mode

Top 3 earners. DESC for descending, ASC for ascending (default).

result = query("""
    SELECT name, age, salary
    FROM employees
    ORDER BY dept_id ASC, salary DESC;
""")
print(result)

Enter fullscreen mode Exit fullscreen mode

Sort by multiple columns. Department first, then salary within each department.


Aggregate Functions

result = query("""
    SELECT
        COUNT(*)          AS total_employees,
        AVG(salary)       AS avg_salary,
        MIN(salary)       AS min_salary,
        MAX(salary)       AS max_salary,
        SUM(salary)       AS total_payroll
    FROM employees;
""")
print(result.round(0))

Enter fullscreen mode Exit fullscreen mode

Output:

   total_employees  avg_salary  min_salary  max_salary  total_payroll
0                8     70000.0       43000       95000         560000

Enter fullscreen mode Exit fullscreen mode

Five aggregate functions. One query. No loops.

COUNT(*) counts all rows. COUNT(column_name) counts non-null values in that column.


GROUP BY: Aggregate by Category

This is the SQL equivalent of Pandas groupby.

result = query("""
    SELECT
        dept_id,
        COUNT(*)     AS headcount,
        AVG(salary)  AS avg_salary,
        MAX(salary)  AS max_salary
    FROM employees
    GROUP BY dept_id
    ORDER BY avg_salary DESC;
""")
print(result.round(0))

Enter fullscreen mode Exit fullscreen mode

Output:

   dept_id  headcount  avg_salary  max_salary
0        3          2     91500.0       95000
1        2          2     74500.0       82000
2        4          1     59000.0       59000
3        1          3     56333.0       71000

Enter fullscreen mode Exit fullscreen mode

Every aggregate function runs within each group. The ORDER BY here sorts the result by average salary.


HAVING: Filter After Grouping

WHERE filters rows before grouping. HAVING filters groups after aggregation.

result = query("""
    SELECT
        dept_id,
        COUNT(*)    AS headcount,
        AVG(salary) AS avg_salary
    FROM employees
    GROUP BY dept_id
    HAVING COUNT(*) >= 2
    ORDER BY avg_salary DESC;
""")
print(result.round(0))

Enter fullscreen mode Exit fullscreen mode

Output:

   dept_id  headcount  avg_salary
0        3          2     91500.0
1        2          2     74500.0
2        1          3     56333.0

Enter fullscreen mode Exit fullscreen mode

Department 4 (HR) only has one person so it was filtered out by HAVING COUNT(*) >= 2.

A very common mistake: using WHERE to filter aggregated values.

-- Wrong: WHERE cannot reference aggregated values
SELECT dept_id, AVG(salary)
FROM employees
WHERE AVG(salary) > 70000
GROUP BY dept_id;

-- Correct: use HAVING
SELECT dept_id, AVG(salary)
FROM employees
GROUP BY dept_id
HAVING AVG(salary) > 70000;

Enter fullscreen mode Exit fullscreen mode


Combining SQL with Pandas

SQL retrieves data. Pandas transforms and analyzes it. Use both together.

df = query("""
    SELECT name, age, salary, dept_id, hire_date
    FROM employees
    WHERE salary > 50000
    ORDER BY salary DESC;
""")

df["hire_date"] = pd.to_datetime(df["hire_date"])
df["years_employed"] = (pd.Timestamp.now() - df["hire_date"]).dt.days / 365
df["salary_per_year"] = df["salary"] / df["years_employed"]

print(df[["name", "salary", "years_employed", "salary_per_year"]].round(1))

Enter fullscreen mode Exit fullscreen mode

Use SQL for what it is good at: filtering large datasets at the database level before loading into Python. Use Pandas for what it is good at: flexible transformations, visualization prep, feature engineering.


SQL Execution Order

This confuses everyone at first.

SQL does not execute in the order you write it.

The actual execution order is:

1. FROM       -- which table
2. WHERE      -- filter rows
3. GROUP BY   -- group the filtered rows
4. HAVING     -- filter the groups
5. SELECT     -- choose columns and compute aggregates
6. ORDER BY   -- sort the result
7. LIMIT      -- restrict output count

Enter fullscreen mode Exit fullscreen mode

This is why you cannot use a SELECT alias in a WHERE clause:

-- Wrong: 'monthly_salary' does not exist yet at WHERE stage
SELECT salary / 12 AS monthly_salary
FROM employees
WHERE monthly_salary > 5000;

-- Correct: use the original expression
SELECT salary / 12 AS monthly_salary
FROM employees
WHERE salary / 12 > 5000;

Enter fullscreen mode Exit fullscreen mode


A Resource Worth Knowing

Mode Analytics SQL Tutorial at mode.com/sql-tutorial is the most well-structured free SQL learning resource online. It uses real datasets, interactive queries you can run in the browser, and covers everything from basics to window functions. More practical than any textbook. Widely recommended by data professionals. Search "Mode Analytics SQL Tutorial" and it comes up immediately.

Towards Data Science published a piece by Terence Shin called "10 SQL Concepts Every Data Scientist Should Know" that maps directly to what practicing data scientists actually use versus what SQL courses spend too long on. Cuts through the noise. Search the title on Towards Data Science.


Try This

Create sql_practice.py. Use the database we built in this post.

Write SQL queries to answer each of these. Do not use Python to filter or transform the results. Do it all in SQL.

Which employees earn more than the average salary of the entire company?

How many employees were hired each year? (Extract the year from hire_date using strftime('%Y', hire_date))

What is the total hours worked on projects per employee? Include employees with no projects (hint: you need a join, which is coming next post, but try it anyway).

Find the department where the average age of employees is the lowest.

List employees who earn more than the average salary in their own department. (This needs a subquery. Try it. The syntax is WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept_id = e.dept_id).)

Close the connection when done: conn.close().


What's Next

You can query one table now. Real databases have dozens or hundreds of tables. The power comes from combining them. Next post is SQL joins: how to connect tables through shared keys and build queries that span your entire database.