## Introduction 
Welcome to Lesson 2.2: Basic SQL Commands (SELECT, FROM, WHERE). This lesson is part of Module 2: Core Skills - Data Analysis Techniques in the Data Analytics for Product Leaders course. In this lesson, we'll explore writing basic SQL queries, the role of the SELECT, FROM, and WHERE commands, and practical examples of queries. By the end of this lesson, you will be able to write and execute basic SQL commands to retrieve data effectively.
## Main Content 
### Writing Basic SQL Queries 
Structured Query Language, or SQL, is the standard language used to communicate with databases. It allows you to create, read, update, and delete data, making it an essential tool for data analysis. At its core, a SQL query is a request for information from a database. The most fundamental SQL commands include SELECT, FROM, and WHERE. 
The structure of a basic SQL query can be summarized in the following syntax: 
```sql
SELECT column_name(s) 
FROM table_name 
WHERE condition;
```
In this syntax: 
- **SELECT** specifies the columns you want to retrieve. 
- **FROM** indicates the table from which to retrieve the data. 
- **WHERE** filters records based on specified conditions.
### The Role of SELECT, FROM, and WHERE 
Let’s break down the roles of the SELECT, FROM, and WHERE clauses.
- **SELECT**: This command is used to specify which columns of data you want to view. For instance, if you have a table containing customer data, you might want to select just the names and email addresses. The command would look like this: 
```sql
SELECT name, email 
FROM customers;
```
This command retrieves only the "name" and "email" columns from the "customers" table. If you want to retrieve all columns from a table, you can use the asterisk (*) symbol: 
```sql
SELECT * 
FROM customers;
```
- **FROM**: This clause is crucial as it tells SQL which table to pull the data from. In our previous example, the table was "customers". If you have multiple tables, you can join them to retrieve related data, but for now, we’ll focus on queries involving a single table.
- **WHERE**: This clause allows you to filter results based on specific criteria. For example, if you only want to see customers from a specific city, you can use a WHERE clause like this: 
```sql
SELECT name, email 
FROM customers 
WHERE city = 'New York';
```
This command retrieves the names and emails of customers who reside in New York. The WHERE clause is powerful as it enables you to narrow down your results and focus on the data that matters. You can use various operators (like =, >, <, !=, etc.) to refine your queries even further.
### Practical Examples of Queries 
Now that we've covered the basics, let’s look at some practical examples relevant to the No Name business context.
1. **Retrieving Customer Information**: Suppose you want to gather information on customers who have made purchases recently. You can use the following query: 
```sql
SELECT name, purchase_date 
FROM customers 
WHERE purchase_date >= '2023-01-01';
```
This query retrieves the names and purchase dates of customers who made a purchase on or after January 1, 2023.
2. **Finding Products in a Specific Category**: If your company sells products and you want to know which items belong to a specific category, you could execute: 
```sql
SELECT product_name, price 
FROM products 
WHERE category = 'Electronics';
```
This retrieves the names and prices of products in the "Electronics" category.
3. **Count of Customers by City**: To get insights into your customer base, you might want to count how many customers are located in each city. You can use the following SQL query: 
```sql
SELECT city, COUNT(*) AS customer_count 
FROM customers 
GROUP BY city;
```
This command counts the number of customers in each city and groups the results accordingly.
4. **Filtering by Multiple Conditions**: You can also combine conditions in the WHERE clause. For example, if you want to find customers who live in New York and have made a purchase in the last month, you could write: 
```sql
SELECT name, email 
FROM customers 
WHERE city = 'New York' AND purchase_date >= '2023-09-01';
```
This retrieves names and emails of customers who satisfy both conditions.
These examples illustrate how to use basic SQL commands to extract meaningful data from your database, enabling you to make informed business decisions.
## Conclusion 
In this lesson, we discussed the fundamental SQL commands of SELECT, FROM, and WHERE. You learned how to write basic SQL queries to retrieve data from a database, understand the roles of each command, and apply practical examples relevant to your business context. Mastering these commands will empower you to analyze data effectively and extract valuable insights.
## Reflection Questions 
1. How would you modify a SQL query to retrieve data on customers who made purchases in the last month? 
2. What additional filters could you apply using the WHERE clause to refine your data extraction further? 
3. Can you think of a scenario in your work where using SQL queries could enhance your data analysis capabilities?