Beginner

Introduction to SQL for Data Science

Understand what SQL is, how relational databases work, and why SQL is the most important language for anyone working with data.

What is SQL?

SQL (Structured Query Language) is the standard language for managing and querying relational databases. Pronounced "sequel" or "S-Q-L," it allows you to create, read, update, and delete data stored in tables.

For data scientists, SQL is often the first tool used to access and explore data. Whether you are pulling data from a data warehouse, running analytics queries, or feeding machine learning pipelines, SQL is essential.

Relational Databases

A relational database organizes data into tables (also called relations) with rows and columns. Each table represents an entity, and relationships between tables are defined through keys.

Example: Users Table
+----+----------+-------------------+------------+
| id | name     | email             | created_at |
+----+----------+-------------------+------------+
|  1 | Alice    | alice@example.com | 2024-01-15 |
|  2 | Bob      | bob@example.com   | 2024-02-20 |
|  3 | Charlie  | charlie@ex.com    | 2024-03-10 |
+----+----------+-------------------+------------+

Key Concepts

ConceptDescription
TableA collection of related data organized in rows and columns.
Row (Record)A single entry in a table representing one item.
Column (Field)A specific attribute of the data (e.g., name, email).
Primary KeyA unique identifier for each row (e.g., id).
Foreign KeyA column that references a primary key in another table.
SchemaThe structure definition of a database (tables, columns, types).

Popular Database Systems

🐘

PostgreSQL

Open-source, feature-rich, excellent for analytics. The gold standard for data science.

📊

MySQL

Most popular open-source database. Widely used in web applications and startups.

BigQuery / Snowflake

Cloud data warehouses designed for massive-scale analytics and data science workloads.

📦

SQLite

Lightweight, file-based database. Perfect for learning, prototyping, and embedded apps.

Why SQL for Data Science?

Key reasons: SQL is the universal data language. Every company stores data in relational databases or data warehouses. Knowing SQL lets you access data directly instead of waiting for someone else to export it for you.
  1. Data Access

    Query millions of rows from production databases, data warehouses, and data lakes directly.

  2. Data Exploration

    Quickly profile datasets, check distributions, find missing values, and identify patterns.

  3. Feature Engineering

    Create features for machine learning models using aggregations, window functions, and joins.

  4. Reporting & Dashboards

    Power BI, Tableau, Looker, and other tools all rely on SQL queries under the hood.

Your First SQL Query

SQL
SELECT name, email
FROM users
WHERE created_at >= '2024-02-01'
ORDER BY name;

This query selects the name and email columns from the users table, filters for users created after February 1, 2024, and sorts the results alphabetically by name.

💡
Getting started: Install DB Browser for SQLite or use an online SQL playground like DB Fiddle to practice queries without any setup.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.