Lesson 1 - Exploring Databases and Schemas

Welcome to SQL

This is the very beginning of your SQL journey. SQL (Structured Query Language, pronounced “S-Q-L” or “sequel”) is a programming language designed for working with data stored in a database — software that stores, organizes, and retrieves data efficiently. If you have ever opened a spreadsheet to look something up, SQL is the professional, large-scale version of that idea.

Throughout this module you will play the role of a data analyst at a fictional superstore. Your supervisor keeps asking questions — “Which orders lost us money?”, “What did this customer buy?” — and your job is to pull clear answers out of the company’s database. In this first lesson you will get comfortable looking at the data and writing your first queries.

By the end of this lesson, you will be able to:

  • Explain what SQL and a relational database are
  • Write a SELECT statement to read rows and columns from a table
  • Limit how many rows a query returns with LIMIT
  • Recognize that SQL comes in different dialects that share a common standard
  • Document your queries with single-line and multi-line comments
  • Rename columns in your results using aliases and the AS keyword

No prior database experience is needed. Let’s begin.

Data for this lesson

Database: superstore.db — retail orders from a Superstore (SQLite).

Tables used: orders, returns

Open it in any SQLite client — DB Browser for SQLite, the sqlite3 CLI, or Python’s sqlite3 — to run every query yourself.


Reading Data from a Table

The operations a database can perform are often summarized as CRUD: Create, Read, Update, and Delete. As a data analyst, you will spend almost all of your time on the read part — asking the database to show you data — and that is exactly what this module focuses on.

Your First SELECT Statement

The superstore database contains a table called returns that records which orders were sent back. The simplest possible query reads every column from a table. Here it is, retrieving the first five rows:

SELECT *
  FROM returns
 LIMIT 5;

Let’s unpack each line:

  • SELECT *SELECT chooses which columns to show. The * is a special wildcard meaning “all columns.”
  • FROM returnsFROM tells SQL which table to read from.
  • LIMIT 5LIMIT caps the number of rows returned to five. A real table might hold millions of rows, so trying to display them all would be overwhelming and slow.

Every query you write will start with this SELECT ... FROM ... skeleton. Notice the formatting: each clause is on its own line, capitalized, and lined up so the keywords form a tidy column on the left. SQL does not require this, but it makes queries much easier to read — a habit worth building from day one.

Previewing the Orders Table

The table you will use most in this module is orders, which records every purchase. Let’s preview it the same way:

SELECT *
  FROM orders
 LIMIT 5;

This returns the first five complete order records. Because orders has many columns — order IDs, dates, customer details, product info, sales, profit, and more — the output is wide. Previewing a handful of rows like this is always a smart first move when you meet a new table: it shows you what columns exist and what the values look like before you write anything more specific.


SQL Dialects and the ANSI Standard

There are many versions of SQL, often called dialects or “flavors.” In this course you are writing SQLite (pronounced “sequel light”), a lightweight, popular dialect. Other common ones include PostgreSQL, MySQL, and Microsoft’s T-SQL.

One Language, Many Accents

The good news: these dialects are far more alike than different, much like accents of the same language. Almost everything you learn here transfers to other databases thanks to the ANSI SQL standard — a shared specification that keeps the core of SQL consistent everywhere.

There are still small differences in syntax. A classic example is how each dialect limits rows. In SQLite you write:

SELECT *
  FROM orders
 LIMIT 5;

In T-SQL (Microsoft’s flavor), the same idea uses a different word in a different place:

SELECT TOP 5 *
  FROM orders;

That T-SQL query will not run in SQLite — it is shown only to illustrate the difference. If you ever inherit a query from another system that uses TOP, you now know to translate it to LIMIT to make it run here. The big lesson: learn the concepts well, and adapting to a new dialect later is a matter of looking up a few keywords, not relearning the language.


Selecting Specific Columns

Most databases, like superstore, are made of several tables of data. A table is laid out in rows and columns, which in database language are called records (rows) and fields (columns).

Choosing the Fields You Want

You already know how to limit the number of records with LIMIT. But what about limiting the number of fields? Instead of SELECT * (all columns), you can list exactly the columns you want, separated by commas:

SELECT order_date, order_id, product_name, sales, quantity
  FROM orders
 LIMIT 5;

This returns just the five fields you asked for, for the first five orders:

order_dateorder_idproduct_namesalesquantity
2023-01-03 00:00:00US-2023-103800Message Book, Wirebound, Four 5 1/2" X 4" Forms/Pg., 200 Dupl. Sets/Book16.4482
2023-01-04 00:00:00US-2023-112326GBC Standard Plastic Binding Systems Combs3.542
2023-01-04 00:00:00US-2023-112326Avery 50811.7843
2023-01-04 00:00:00US-2023-112326SAFCO Boltless Steel Shelving272.7363
2023-01-05 00:00:00US-2023-141817Avery Hi-Liter EverBold Pen Style Fluorescent Highlighters, 4/Pack19.5363

Naming the columns you need has two benefits: the query runs faster, and the output is far easier to read because it only shows what matters.

Relational Databases

The superstore database has three tables: orders, returns, and people. Tables in a database are connected to one another through shared fields — for example, an order ID that appears in both orders and returns. Because these relationships between tables matter, this kind of database is called a relational database.

For now, every query you write will look at a single table; joining tables together to combine their data comes in a later module. As an example of selecting from a single table, here is how you would pull location info for a sample of orders:

SELECT city, state_province, region
  FROM orders
 LIMIT 10;

This returns the city, state_province, and region of the first ten orders — exactly the kind of list a manager might use to confirm which areas they cover:

citystate_provinceregion
HoustonTexasCentral
NapervilleIllinoisCentral
NapervilleIllinoisCentral
NapervilleIllinoisCentral
PhiladelphiaPennsylvaniaEast
HendersonKentuckySouth
HendersonKentuckySouth
AthensGeorgiaSouth
HendersonKentuckySouth
HendersonKentuckySouth

Renaming Columns with Aliases

Field names in a database are built for typing, not for presentation. Names like order_id and ship_date are convenient in code but look awkward in a report you hand to your supervisor.

Using AS to Alias

You can rename a column in your results using the AS keyword. This is called an alias. Here is how to give three columns friendlier, human-readable headers:

SELECT order_id   AS 'Order ID',
       order_date AS 'Order Date',
       ship_date  AS 'Ship Date'
  FROM orders
 LIMIT 3;
Order IDOrder DateShip Date
US-2023-1038002023-01-03 00:00:002023-01-07 00:00:00
US-2023-1123262023-01-04 00:00:002023-01-08 00:00:00
US-2023-1123262023-01-04 00:00:002023-01-08 00:00:00

An alias does not change the column name in the database — it only exists for the duration of the query. The next time you query orders, the field is still called order_id.

When to Use Quotes

For simple, single-word aliases without spaces, quotes are optional. They are recommended (or required) when the alias:

  • Contains a space, like order_id AS 'Order ID'
  • Contains special characters, like usd AS 'Dollars ($)'
  • Needs specific capitalization preserved

Most databases accept both single (') and double (") quotes, though some have rules — PostgreSQL, for instance, wants double quotes for case-sensitive aliases. Using quotes consistently keeps your queries portable and readable.

Is AS Optional?

Technically, you can drop AS and place the alias right after the column name:

SELECT order_id   'Order ID',
       order_date 'Order Date'
  FROM orders
 LIMIT 3;

Both versions produce identical results, but you should keep AS for three reasons: it makes the intent obvious to anyone reading the query, it works consistently across dialects, and it avoids confusing situations where a column name looks like a keyword. Writing AS is a small habit that prevents a lot of head-scratching later.


Documenting Queries with Comments

Just like other programming languages, SQL lets you leave comments — notes that explain your reasoning but are ignored by the database when the query runs.

Single-Line and Multi-Line Comments

A single-line comment starts with two dashes (--). Everything after the dashes on that line is ignored:

-- This query previews the first five orders.
SELECT *
  FROM orders
 LIMIT 5;

You can also put a single-line comment at the end of a line of code to clarify it:

SELECT *
  FROM orders   -- read from the orders table
 LIMIT 5;

For longer notes, or to temporarily disable a whole query without deleting it, use a multi-line comment wrapped in /* and */:

/*
This query is paused for now. SQL will not run anything
inside this block:

SELECT order_id, product_name
  FROM orders
 LIMIT 10;
*/

This “commenting out” trick is incredibly handy when you are testing two versions of a query and want to switch between them without losing either one. Well-placed comments turn a clever query into a query your future self — and your teammates — can actually understand.


Practice Exercises

Try these on your own before peeking at the hints. Use the orders table.

Exercise 1: Preview a Table

Write a query that returns all columns from the orders table, showing only the first 4 rows.

Hint

Use SELECT * to grab every column, FROM orders to choose the table, and LIMIT 4 on the last line to cap the rows.

Exercise 2: Select and Alias

Write a query that returns city, state_province, and postal_code from orders, renaming them to City, State, and ZIP Code respectively. Limit the results to 10 rows.

Hint

Use AS after each column name, and wrap aliases that contain a space (like 'ZIP Code') in quotes. The pattern is SELECT city AS 'City', ....

Exercise 3: Comment Your Query

You have two queries that show address fields, but only one lists them in the correct mailing order: city, state_province, postal_code, country_region. Write a single-line comment above the correctly ordered query explaining what it does, and use a multi-line comment to disable the other one.

Hint

Start your single-line note with --. Wrap the query you want to disable in /* and */ so SQL skips it entirely.


Summary

You have explored a relational database and written your first SQL queries. You learned how to read records and fields from a table, control how many rows come back, recognize different SQL dialects, document your work with comments, and rename columns with aliases.

Key Concepts

  • SQL — a language for reading and managing data in relational databases.
  • Relational database — a collection of tables connected through shared fields; superstore has orders, returns, and people.
  • SELECT / FROM — choose which columns to show and which table to read from.
  • LIMIT — restrict how many rows a query returns.
  • Records and fields — database words for rows and columns.
  • ANSI SQL standard — the shared core that keeps SQL dialects mostly compatible.
  • Alias (AS) — a temporary, presentation-friendly name for a column in your results.
  • Comments-- for a single line, /* ... */ for multiple lines; ignored when the query runs.

Why This Matters

Reading data is the foundation of every analysis you will ever do. Before you can summarize sales, find your best customers, or build a dashboard, you have to be able to ask the database for exactly the right slice of data. The skills in this lesson — selecting columns, limiting rows, and aliasing for readability — are the verbs you will use in nearly every query for the rest of your career. Adding comments and clean formatting is what separates a query that works today from one your whole team can trust and reuse next year.


Next Steps

Continue to Lesson 2 - Columns, Data Types, and Functions

Inspect data types and transform text and numbers with functions like ROUND, UPPER, and LOWER

Back to Module Overview

Return to the Getting Started with SQL module overview


Continue Building Your Skills

You just wrote your first real SQL queries against a real database — that is a genuine milestone. Every expert started exactly where you are now, with a simple SELECT statement. Keep previewing tables, naming your columns, and commenting your work, and the language will quickly start to feel like a conversation with your data.