Data Science — SQL
Basic queries
-- Select data
SELECT * FROM users;
-- Filter
SELECT * FROM users WHERE age > 25;
-- Order
SELECT * FROM users ORDER BY name;
-- Limit
SELECT * FROM users LIMIT 10;
Aggregations
-- Count
SELECT COUNT(*) FROM users;
-- Group by
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
-- Having
SELECT department, AVG(salary)
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;
Joins
-- Inner join
SELECT * FROM users
INNER JOIN orders ON users.id = orders.user_id;
-- Left join
SELECT * FROM users
LEFT JOIN orders ON users.id = orders.user_id;
Subqueries
SELECT * FROM users
WHERE id IN (SELECT user_id FROM orders);
Window functions
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) as rank
FROM employees;
Python + SQL
import sqlite3
import pandas as pd
conn = sqlite3.connect('database.db')
df = pd.read_sql('SELECT * FROM users', conn)
Mini Practice
- Write basic queries
- Use aggregations
- Join tables
- Use window functions
Up Next
Continue with Python - Programming for data science.
Related Topics
Frequently Asked Questions about SQL
What is SQL in Data Science?
SQL is a fundamental concept in Data Science. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn SQL?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn SQL.
Why is SQL important in Data Science?
SQL is essential for Data Science development. Understanding this concept will help you write better code and solve real-world problems more effectively.