CodeMIRAI
0 XP

Introduction to SQL

2 min read 2 runnable examples Quiz included

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:

DatabaseWhere you'll meet it
SQLiteinside phones, browsers and apps; this course's examples run on it
MySQLwebsites and web apps
PostgreSQLapps and analytics; it follows the standard closely
SQL Servercompanies built on Microsoft tools, and Azure SQL Database in the cloud
Oraclelarge 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:

TableWhat it holds
customerspeople who buy from the shop, with their city and country
productswhat the shop sells, with category, price and stock
orderseach order: who placed it, when, and its status
order_itemswhich products are in each order, and how many
employeesthe shop's staff, departments and salaries

Run this query to see every customer:

SQL
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:

SQL
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.

Finished this lesson?Mark it complete to earn +20 XP and move your progress on.