SQL for Data Analysis


SQL for Data Analysis

Anna
Alright, Michael, let's talk about SQL. And I mean really break it down from scratch, because honestly, it's one of those skills that can totally change how you work with data.

Michael
Totally agree. But let's start with the obvious: why would someone who's not a programmer even want to learn SQL? I mean, isn't that for database admins?

Anna
Not at all! Think of any business—they store customer info, orders, products—all in databases. As a product manager at Microsoft, I used SQL every day to pull insights. Like, how many people clicked a button? That data helped decide where to put things on the page. Without SQL, you're guessing.

Michael
So it's about getting answers from raw data. But what even is a database? I've heard the term, but it sounds abstract.

Anna
A database is just a collection of tables that are related to each other. Like, imagine a cookie company. You'd have a Customers table, an Orders table, a Products table. They connect through IDs. That's why they call it a relational database.

Michael
Okay, so tables are like spreadsheets, but split up to avoid repeating info. That makes sense. Now, what do we actually use to talk to the database? That's where SQL comes in, right?

Anna
Exactly. SQL stands for Structured Query Language, sometimes pronounced 'sequel'. It's the language you use to retrieve, filter, sort, add, update, or delete data. And the best part? You don't need a CS degree to learn it. Anyone can pick it up.

Michael
But we need a tool to actually run SQL. Like, some kind of software?

Anna
Yes. We'll use a Relational Database Management System, or RDBMS. Popular ones are Oracle, PostgreSQL, MySQL, and Microsoft SQL Server. We'll use SQL Server because it's free for learning and widely used. Once you learn one, switching is easy—just minor syntax differences.

Michael
So first we install SQL Server, then we need something to write queries in, right?

Anna
Exactly. That's SQL Server Management Studio, or SSMS. It's a graphical interface that connects to your server and lets you write and run queries. After installing both, you connect to your local server using Windows Authentication, and you're in.

Michael
And we need a database to practice on. In the tutorial, we restored a sample database called KCC for the Kevin Cookie Company. That gave us four tables: Customers, Orders, OrderProduct, and Product.

Anna
Right. And when you look inside a table, you see columns—those are fields like CustomerName or OrderDate—and rows, which are individual records. The columns are designed to be granular, like splitting address into street, city, state, so you can query specific parts easily.

Michael
And each table has a primary key, right? That's a column that uniquely identifies each row. For Customers, it's CustomerID. For Orders, it's OrderID. That's how you link tables together.

Anna
Exactly. The primary key is the minimum set of columns that makes each row unique. And then you have foreign keys—like CustomerID in the Orders table—that reference the primary key in Customers. That's how relationships work.

Michael
And there's a neat way to visualize those relationships: database diagrams. In SSMS, you can see the tables and the connections—like one-to-many between Customers and Orders. Super helpful when you're writing joins.

Anna
Okay, let's write our first query. It's simple: SELECT CustomerName FROM dbo.Customers. That retrieves just that column. You can add more columns with commas: SELECT CustomerName, Notes FROM dbo.Customers. And you can alias a column with 'AS' to rename it in the results.

Michael
And if you want all columns, use the asterisk: SELECT *. Or just the top N rows: SELECT TOP 3 * FROM dbo.Customers. That's handy for a quick glance.

Anna
Now, filtering. The WHERE clause is your friend. Want customers in Washington? WHERE State = 'Washington'. You can use =, !=, <>, >, <. And for multiple conditions, use AND and OR. Parentheses help keep it clear.

Michael
What about finding patterns? LIKE is great for partial matches. For example, WHERE CustomerName LIKE 'A%' gives all names starting with 'A'. Or use NOT LIKE to exclude them.

Anna
And for numeric filters, BETWEEN is nice: WHERE OrderTotal BETWEEN 1000 AND 2000. That includes both endpoints. Also, IN lets you check against a list: WHERE State IN ('WA', 'NY', 'UT').

Michael
Now, sorting. ORDER BY OrderTotal DESC gives highest first. ASC is default. You can order by multiple columns too.

Anna
Great. But the real power comes with joins. When you need data from multiple tables, you use JOIN. For example, to get order details plus customer name, you'd join Orders and Customers on CustomerID.

Michael
And there are different types. INNER JOIN gives only rows that match in both tables. LEFT JOIN gives all rows from the left table even if there's no match—then the right side shows NULL. RIGHT JOIN does the opposite.

Anna
Exactly. And you can alias tables to make your query shorter: FROM dbo.Orders O JOIN dbo.Customers C ON O.CustomerID = C.CustomerID. Then you refer to columns as O.OrderID, C.CustomerName, etc.

Michael
Now, aggregate functions. COUNT(*) gives the number of rows. SUM(OrderTotal) adds them up. AVG, MIN, MAX. And if you want totals per group, use GROUP BY. Like, sum of orders per customer: SELECT CustomerID, SUM(OrderTotal) FROM Orders GROUP BY CustomerID.

Anna
And you can filter groups with HAVING, like HAVING SUM(OrderTotal) > 5000. But remember, WHERE filters rows before grouping, HAVING filters after.

Michael
Functions are also handy for dates. DATEADD(month, -1, GETDATE()) gives one month ago. So you can always get last month's data without hardcoding dates.

Anna
And there's a cool trick: the Query Designer in SSMS. You right-click in a query window, select 'Design Query in Editor', and you get a visual interface. You can add tables, select columns, set filters, and it writes the SQL for you. It's perfect for complex queries.

Michael
That's a game-changer for beginners. So overall, SQL is not hard to start with. You learn SELECT, FROM, WHERE, JOIN, ORDER BY, GROUP BY, and some functions—that covers like 90% of what you need for reporting.

Anna
Exactly. And once you know the concepts, you can apply them to any RDBMS. The value is in unlocking insights from your data. So whether you're a business analyst, product manager, or just curious, SQL is a superpower.

Ask anything
We've read this whole study set, so questions about it get the best answers.