SQL Course Introduction


SQL Course Introduction

Anna
Simon, we've got a packed episode today. We're diving into SQL—or should I say SQUEL? There's a whole debate about how to pronounce it.

Simon
Oh, definitely. Mosh, the instructor from the course we're using, explains that IBM originally called it SQUEL—short for Structured English Query Language—but they changed it to SQL because SQUEL was trademarked by an airplane company. So people in non-English speaking countries often say SQL, while he prefers SQUEL. Both are fine, honestly.

Anna
Right. And what matters is the language itself. SQL is how we talk to relational databases. You store data in tables that are linked by relationships—like customers and orders. That's the relational model. There are also NoSQL databases, but they don't speak SQL.

Simon
Exactly. So to manage a relational database, you use a Database Management System or DBMS. Popular ones are MySQL, SQL Server, Oracle. Mosh uses MySQL in the course because it's the most popular open-source one. And he shows how to install it on both Mac and Windows.

Anna
Right. On a Mac, you download the DMG from MySQL.com, run the installer, set a root password, and then install MySQL Workbench—the graphical tool. On Windows, you use the MySQL Installer, which is a wizard that handles everything. After that, you open Workbench and create a connection to your local server.

Simon
Once MySQL is running, you need to set up the databases for the course. Mosh provides a SQL script called 'create databases.sql'. You open it in Workbench and execute it. That creates several databases, like sql_store, sql_invoicing, sql_hr, and more. Each database contains tables, views, stored procedures, and so on.

Anna
Let's explore the sql_store database. It has a customers table and an orders table. If you look at customers, you see columns like customer_id, first_name, last_name, points, etc. Each row is a record. The customer_id is the primary key—it uniquely identifies each customer.

Simon
And orders has a column called customer_id. That's a foreign key linking each order to the customer who placed it. That's the relationship. You don't repeat the customer's full info in orders; you just store the ID. That way, if a customer changes address, you update it in one place.

Anna
Now, the first thing you do in SQL is write a SELECT query. You start by selecting a database with the USE keyword. For example, 'USE sql_store;'. Then you write 'SELECT * FROM customers;' to get all columns and rows. Or you can list specific columns: 'SELECT first_name, last_name FROM customers;'.

Simon
And you can add arithmetic expressions. For instance, 'SELECT points, points * 10 + 100 AS discount_factor FROM customers;'. The AS keyword gives that calculated column a name—an alias. If you want a space in the alias, you use quotes: 'AS "discount factor"'.

Anna
Also, you can remove duplicates with DISTINCT. Like 'SELECT DISTINCT state FROM customers;' gives you each state only once. And in the SELECT clause, you can use the ORDER BY clause to sort results: 'ORDER BY first_name DESC' or 'ORDER BY state, first_name' to sort by multiple columns.

Simon
The WHERE clause filters rows. You can use comparison operators: >, <, >=, <=, =, !=. For strings, use single quotes: 'WHERE state = "VA"'. For dates: 'WHERE birth_date > "1990-01-01"'. And you can combine conditions with AND, OR, and NOT. Remember: AND has higher precedence than OR, so use parentheses to clarify.

Anna
Right. There are also special operators: IN checks if a value is in a list—'WHERE state IN ("VA", "FL", "GA")'. BETWEEN is for ranges: 'WHERE points BETWEEN 1000 AND 3000'. That's inclusive. And for pattern matching, we have LIKE and REGEXP.

Simon
LIKE uses % for any number of characters and _ for a single character. For example, 'WHERE last_name LIKE "b%"' gets names starting with b. 'WHERE last_name LIKE "%y"' ends with y. REGEXP is more powerful: you can use ^ for start, $ for end, | for OR, and square brackets for character sets. Like 'WHERE last_name REGEXP "field|mac"'.

Anna
Another useful thing is IS NULL. 'WHERE phone IS NULL' finds customers missing a phone number. And you can combine it with NOT: 'WHERE phone IS NOT NULL'. Similarly, you can check for missing shipped orders: 'WHERE shipper_id IS NULL'.

Simon
Now, after filtering, you often want to limit results. The LIMIT clause does that: 'LIMIT 3' gives the first three rows. You can also add an offset for pagination: 'LIMIT 6, 3' skips the first six and returns the next three. And remember the order of clauses: SELECT, FROM, WHERE, ORDER BY, LIMIT.

Anna
The real power of SQL comes with JOINs. When you need data from multiple tables, you join them. The most common is INNER JOIN. For example, 'SELECT * FROM orders JOIN customers ON orders.customer_id = customers.customer_id;' This returns only rows where the join condition matches. You can also use aliases for tables to shorten the code: 'FROM orders o JOIN customers c ON o.customer_id = c.customer_id'.

Simon
When joining, if column names are the same in both tables, you can use the USING clause instead of ON. 'FROM orders JOIN customers USING (customer_id)' is cleaner. But it only works if the column name is identical. For composite primary keys, you can list multiple columns: 'USING (order_id, product_id)'.

Anna
There are also OUTER JOINs: LEFT JOIN and RIGHT JOIN. A LEFT JOIN returns all rows from the left table, even if there's no match in the right table. For example, 'SELECT c.customer_id, c.first_name, o.order_id FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id;' gives all customers, with NULL for those without orders. RIGHT JOIN works similarly but with the right table as the base.

Simon
You can join more than two tables. Mosh shows a query joining orders, customers, and order_statuses. Each join adds another table. And be careful: if you use an INNER JOIN after a LEFT JOIN, you might lose rows. So match the join type to your needs. A self join is when you join a table to itself, like employees with managers: 'FROM employees e JOIN employees m ON e.reports_to = m.employee_id'.

Anna
There's also the CROSS JOIN, which matches every row from the first table with every row from the second. For example, 'SELECT * FROM customers CROSS JOIN products;' gives all combinations. That's useful for generating all possible pairs, like sizes and colors. And natural joins automatically join on common column names, but Mosh warns against them because they can produce unexpected results.

Simon
Another way to combine rows is with UNION. It stacks results from multiple SELECT queries on top of each other. The number of columns must match, and the column names come from the first query. A practical example is labeling orders: 'SELECT order_id, "Active" FROM orders WHERE order_date >= "2019-01-01" UNION SELECT order_id, "Archived" FROM orders WHERE order_date < "2019-01-01"'.

Anna
Now let's talk about modifying data. INSERT adds new rows. For a single row: 'INSERT INTO customers (first_name, last_name, address, city, state) VALUES ("John", "Smith", "123 Main St", "New York", "NY");' Since customer_id is auto-increment, you skip it or use DEFAULT. For multiple rows, separate with commas. And for hierarchical data like an order with items, you first insert the order, then use LAST_INSERT_ID() to get the new order ID for the child rows.

Simon
UPDATE modifies existing rows. Always use a WHERE clause to specify which rows. You can set columns to literal values or expressions: 'SET payment_total = invoice_total * 0.5, payment_date = due_date'. You can also use subqueries: 'SET payment_total = (SELECT ...)'. It's a good practice to test your WHERE condition with a SELECT before running the UPDATE.

Anna
DELETE works similarly: 'DELETE FROM invoices WHERE invoice_id = 1;' Without a WHERE clause, it deletes all rows. You can also use subqueries to identify rows, like deleting all invoices for a client based on name. And after all that practice, you can restore the databases to their original state by rerunning the 'create databases.sql' script.

Simon
That's a lot of ground covered, Anna. From basic SELECTs to joins, subqueries, and data modification—all essential for any developer or data scientist. And Mosh's course goes even deeper into advanced topics like database design, transactions, and complex queries.

Anna
Absolutely. If you want to master SQL, this is a fantastic foundation. Thanks for joining us, and happy querying!

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