SQL for Data Analysis

SQL (Structured Query Language) retrieves and manipulates data in relational databases, a key tool for data analysts. This article covers basic commands, joins, an example, and its role in data science.

Basic Commands

  • SELECT: Retrieve data (e.g., SELECT name FROM customers).
  • WHERE: Filter (e.g., WHERE age > 30).
  • ORDER BY: Sort (e.g., ORDER BY sales DESC).

Joins and Aggregations

  • INNER JOIN: Combine matching rows (e.g., FROM orders JOIN customers ON orders.cust_id = customers.id).
  • COUNT, AVG: Aggregate (e.g., SELECT AVG(sales)).

Links and summarizes data.

Example Query

Table: Sales {id, amount, date}:

SELECT date, SUM(amount) 
FROM sales 
WHERE amount > 100 
GROUP BY date 
ORDER BY date;

Daily totals for big sales.

Applications

Used in:

  • Business: Sales reports.
  • Analytics: Customer segmentation.
  • Research: Data extraction.

Core to data workflows.