Introduction to SQL
SQL (Structured Query Language, often said "sequel") is the language for asking questions of a relational database. Banks, shops, hospitals, apps and websites keep their data in databases, and SQL is how they read and change it.
Tables, rows and columns
A relational database stores data in tables. Each table looks like a spreadsheet:
- each column holds one kind of information (a name, a price, a date),
- each row is one record (one customer, one product, one order).
One language, many databases
SQL is a standard, so what you learn here works, with small differences, in every major database:
| Database | Where you'll meet it |
|---|---|
| inside phones, browsers and apps; this course's examples run on it | |
| websites and web apps | |
| apps and analytics; it follows the standard closely | |
| companies built on Microsoft tools, and Azure SQL Database in the cloud | |
| large enterprises such as banks |
Your practice database
Every SQL example on CodeMIRAI runs against a small online shop database, right in your browser. It has five tables:
| Table | What it holds |
|---|---|
customers | people who buy from the shop, with their city and country |
products | what the shop sells, with category, price and stock |
orders | each order: who placed it, when, and its status |
order_items | which products are in each order, and how many |
employees | the shop's staff, departments and salaries |
Run this query to see every customer:
SELECT * FROM customers;SELECT * means "all columns" and FROM customers says which table. The semicolon ends the statement.
Asking a more specific question
SQL reads almost like a sentence. This asks for the name and price of every product that costs under 50, cheapest first:
SELECT name, price
FROM products
WHERE price < 50
ORDER BY price;Press Edit, change 50 to 100, and run it again.
SQL keywords
Words like SELECT, FROM and WHERE are keywords. SQL does not care whether you write them in capitals, but writing them in capitals is a strong convention because it makes queries easier to read.