TExcelTAD
Home › SQL › SQL Intro

SQL Introduction

SQL (Structured Query Language, pronounced "S-Q-L" or "sequel") is the standard language for working with relational databases. A relational database stores data in tables — grids of rows and columns — and SQL lets you ask questions of those tables, add new rows, change existing ones and define the tables themselves.

Almost every app you use talks to a database somewhere: online shops store products and orders, banks store accounts and transactions, and your phone keeps contacts and messages in a small SQL database called SQLite.

What can SQL do?

Task Example statement
Read data SELECT name, price FROM products
Filter and sort ... WHERE price < 5 ORDER BY price
Summarize SELECT COUNT(*) FROM orders
Add rows INSERT INTO customers ...
Change rows UPDATE products SET price = 9.5 ...
Remove rows DELETE FROM orders ...
Define structure CREATE TABLE, ALTER TABLE, DROP TABLE
Combine tables JOIN, UNION, subqueries

SQL is declarative: you describe what result you want, and the database engine works out how to fetch it efficiently. You never write loops over rows yourself.

Your first query

Every playground in this course runs against TAD Market, a small online shop database with products, customers, orders and more. Press ▶ Run to execute the query:

sql · try it yourself
SELECT name, price
FROM products
WHERE category_id = 8
ORDER BY price;
name                   | price
-----------------------+------
The SQL Handbook       | 29
Python Recipes         | 34.5
Learn Rust in Practice | 39
(3 rows)

Read it almost like English: select the name and price from the products table where the category is 8 (Books), ordered by price. The answer comes back as a table.

Tip: Every run starts from a fresh copy of the database. Feel free to experiment — you cannot break anything permanently.

A few more questions

You can ask several questions in one playground. Each statement ends with a semicolon ; and each result is printed separately:

sql · try it yourself
SELECT COUNT(*) AS total_customers FROM customers;

SELECT name, city
FROM customers
WHERE country = 'Japan';
total_customers
---------------
25
(1 row)

name          | city
--------------+------
Code & Coffee | Tokyo
(1 row)

RDBMS: the software behind SQL

The program that stores the data and runs your SQL is called a Relational Database Management System (RDBMS). Popular ones:

RDBMS Typical use
SQLite Embedded in apps, phones and browsers (used on this site)
PostgreSQL Powerful open-source server database
MySQL / MariaDB Very common for websites
Microsoft SQL Server Enterprise systems on Windows/Azure
Oracle Database Large enterprise systems

All of them understand the core of standard SQL (SELECT, INSERT, JOIN, GROUP BY, …). They differ in details such as data types, date functions and how to limit the number of rows. This course uses SQLite, and whenever another system writes something differently you will see a short comparison.

Changing data

SQL is not only for reading. This playground adds a new product and then reads it back:

sql · try it yourself
INSERT INTO products (name, category_id, supplier_id, unit, price, stock)
VALUES ('Matcha Powder', 1, 1, '100 g tin', 14.9, 20);

SELECT product_id, name, price
FROM products
WHERE name = 'Matcha Powder';
1 row affected.

product_id | name          | price
-----------+---------------+------
31         | Matcha Powder | 14.9
(1 row)

The INSERT reports how many rows it changed; the SELECT shows the new row, which received the next free product_id automatically.

Test yourself

What does SQL stand for?

  1. Simple Query Language
  2. Structured Query Language
  3. Sequential Query Logic
  4. Standard Question Language

Which statement reads data from a table?

  1. GET
  2. READ
  3. SELECT
  4. FETCH

SQL is called *declarative* because…

  1. you must declare every variable
  2. you describe the result you want, not the steps to compute it
  3. it only works with declared tables
  4. it has no loops or functions at all