SQL Basics: Ask Questions of Data
What Is SQL? Your First Queries
In this lesson you will
- Explain what a database table, row and column are
- Read data with SELECT and FROM
- Choose specific columns or all columns with *
Introduction
Almost every app you use keeps its information in a database: your orders, your messages, your scores. SQL (said "S-Q-L" or "sequel") is the language you use to ask a database questions, such as "which products cost less than 10?" or "how many orders did we get in March?".
The simple explanation
A database is a collection of tables. SQL is how you read and change them.
A table looks like a spreadsheet:
- A column is one kind of information, such as
nameorprice. - A row is one item, such as one product.
Every example in this course runs on a small shop database that loads automatically. It has four tables: products, customers, orders and order_items. Here is how products is laid out:
| id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Notebook | Stationery | 3.50 | 120 |
| 2 | Pen Set | Stationery | 5.00 | 200 |
| 3 | Backpack | Bags | 29.90 | 35 |
There are eight products in total. Each time you press Run, you get a fresh copy of the database, so you can experiment without breaking anything.
Quick quiz
In a table of products, what is one row?
Your first query
SELECT chooses the columns you want. FROM says which table to read.
The result appears as a table below the editor. Notice how you only get the two columns you asked for.
To get every column, use the star *, which means "all columns":
Rules to remember
- SQL keywords such as
SELECTandFROMare not case sensitive, but writing them in capitals makes queries easier to read. - A query can sit on one line or many. Splitting it across lines is a good habit.
- Each statement ends with a semicolon
;. - Text values go in single quotes, like
'Bags'. You will use this in the next lesson.
Naming a column with AS
You can give a column a friendlier label in the result with AS. This is called an alias.
You can also calculate while you read. This shows the price with 20 percent added:
Quick quiz
Which query returns only the name and city of each customer?
A note on the SQL dialect
There are several versions ("dialects") of SQL. This course runs on SQLite, a small and very popular database that is built into phones, browsers and many apps. The core of SQL you learn here works almost the same in MySQL, PostgreSQL and SQL Server. The few differences are pointed out where they matter.
Try it yourself
Show the name and city of every customer. Then change the query to show the name and joined date, and give joined the alias member_since.
Summary
- A database holds tables. A table has columns (kinds of information) and rows (items).
SELECTpicks columns andFROMpicks the table.*means all columns.ASrenames a column in the result, and you can do arithmetic in a query.- Text goes in single quotes, and statements end with a semicolon.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading