Introduction to Databases and the SELECT Statement
Welcome to the Digital Warehouse
The SQL Analogy
Think of a database as a massive Digital Warehouse. To get information out of it, you need to know how the warehouse is organized and how to ask the manager for specific items.
- Tables: The aisles.
- Rows: The individual items on the shelves.
- Columns: Specific details about those items (like price or name).
Welcome to your first step in mastering data. Imagine a database is a massive digital warehouse. Inside, data is organized into tables, much like aisles in a store. Each row represents a single item, while columns store specific details about those items, like their price or description. SQL is the tool we use to talk to the warehouse manager.
- Databases are like warehouses organized into tables.
- Tables consist of rows (records) and columns (attributes).
- SQL is the language used to request data from these tables.
Exploring the Products Table
Our Sample Database
We will use a realistic E-commerce database throughout this course. Let's look at the Products table, which stores everything our store sells.
Before we write code, let's look at our data. This is the Products table. Notice the column names at the top: product_id, product_name, category, price, and stock_quantity. Each row below these headers represents a unique product in our inventory. To get this data, we use the SELECT statement.
- The Products table contains inventory data.
- Each column has a specific name like product_id or price.
- Understanding the schema is the first step to writing a query.
The SELECT Statement
The Basic Syntax
To retrieve data, you need two keywords:
- SELECT: Which columns do you want?
- FROM: Which table is the data in?
Use an asterisk (*) to grab all columns at once.
To retrieve data, we use the SELECT statement. Think of it as a sentence: 'SELECT these columns FROM this table.' If you want to see every detail available, use the asterisk as a wildcard shorthand for 'all columns'. Running this query returns every single row and column in the Products table.
- SELECT identifies columns.
- FROM identifies the table.
- The asterisk (*) is a wildcard for 'all columns'.
Selecting Specific Columns
Reducing Data Clutter
In large databases, tables can have hundreds of columns. To save time and memory, only select the columns you need by listing their names separated by commas.
Often, you don't need every piece of information. Instead of using the asterisk, you can list specific columns. By requesting only the product name and price, we reduce data clutter and make our results much easier to read. Remember to separate your column names with a comma.
- Listing specific columns reduces 'data clutter'.
- Column names must be separated by commas.
- Do not put a comma after the final column name.
Spot the Syntax Errors
SQL is picky! Look at the three queries below and click on the errors to fix them.
Even experts make mistakes. Here are three common pitfalls for beginners. Can you find the spelling error, the missing comma, and the wrong keyword order? Click the errors to fix the queries. Correct. Multiple columns need commas to separate them, but never put one after the last column. Exactly. In SQL, we always say SELECT what we want before we say FROM where it comes. Great catch! SQL won't recognize 'product_nme'—it must match the table definition exactly.
- Spelling must be exact.
- Commas are required between columns.
- Keyword order matters: SELECT always comes before FROM.
Exercise 1: Inventory Overview
Your Turn!
The warehouse manager needs a complete overview of the inventory. Write a query to retrieve all columns from the Products table.
Time for some hands-on practice. Write a query to retrieve all columns from the Products table. Type your code in the editor and click 'Run Query' to see the results.
- Apply the SELECT * syntax.
- Target the Products table.
Exercise 2: Marketing List
Marketing Request
The marketing manager only needs the product_name and category. Write a query to retrieve just these two columns from Products.
Now, let's be more specific. The marketing team doesn't need prices or IDs—just names and categories. Write the query to fetch only those two columns.
- Select specific columns.
- Use commas correctly.
Exercise 3: Finance Audit
Finance Check
The finance team needs to check pricing and stock levels. Write a query to retrieve the product_name, price, and stock_quantity.
Final challenge for this lesson! Finance needs to see names, prices, and stock quantities. Combine everything you've learned to write this three-column query.
- Select three specific columns.
- Maintain correct keyword order.