Academic Block

SQL Introduction Get Certified

What is SQL?

SQL (Structured Query Language) is the standard language used to create, read, update, and delete data stored in relational databases. Where a spreadsheet stores data in flat sheets, a relational database organizes it into linked tables, and SQL is how you ask questions of — and make changes to — that data.

Why SQL is used

Almost every application that stores structured data — from banking systems to e-commerce sites to internal admin tools — relies on a relational database underneath. SQL gives every one of them a common, declarative way to retrieve exactly the data needed, filter and combine it across tables, and keep it consistent, without each application inventing its own storage format.

Features of SQL

  • Declarative querying — you describe what data you want, not how to fetch it
  • Joins across multiple related tables
  • Aggregation (COUNT, SUM, AVG, GROUP BY)
  • Filtering and sorting (WHERE, ORDER BY)
  • Data definition (CREATE, ALTER, DROP)
  • Transactions for safe, all-or-nothing updates

Advantages of SQL

  • Widely supported — nearly every relational database understands it
  • Efficient at handling large, structured datasets
  • Enforces data integrity through types, keys, and constraints
  • Mature tooling, documentation, and community knowledge
  • Readable, close to plain-English syntax

How SQL works with data

Data lives in tables made up of rows and columns. A statement like SELECT targets one or more tables, optionally filters rows with WHERE, and returns the matching data as a result set — the same shape you see in the query results below.

SQL Syntax

What this code does: Shows the basic structure of a SQL query — select specific columns from a table, optionally filter rows with a condition, and sort the final result.

SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1;

Example: Basic SELECT

What this code does: Retrieves the name, department, and salary of employees who earn more than 60,000 and sorts the results from highest to lowest salary.

SELECT name, department, salary
FROM employees
WHERE salary > 60000
ORDER BY salary DESC;

Example: GROUP BY

What this code does: Groups employees by department, counts how many people are in each department, calculates the average salary, and sorts departments by highest average salary first.

SELECT department, COUNT(*) AS headcount, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
ORDER BY avg_salary DESC;

Ways to run SQL

  1. Directly in a database client or admin tool (e.g. psql, MySQL Workbench)
  2. Embedded in application code via a database driver or ORM
  3. In-browser, as in the live editor on this page (via SQLite compiled to WebAssembly)

SQL Dialects

Dialect
Database
Notable for
T-SQL
SQL Server
Procedural extensions, error handling
PL/pgSQL
PostgreSQL
Rich type system, extensibility
SQLite dialect
SQLite
Lightweight, file-based, used in this page’s editor
MySQL dialect
MySQL / MariaDB
Widely used in web applications

Best Practices

  • Select only the columns you need, not SELECT *, in production code
  • Filter early with WHERE rather than pulling everything and filtering in code
  • Use meaningful table and column names
  • Index columns you filter or join on frequently
  • Wrap multi-step changes in a transaction

Try it yourself — click Test My Code on any example above to open a live editor that runs real SQL against a sample employees table, or click Code Explanation to walk through the query block by block.

Ctrl + Enter to run code in the editor Esc to close the editor

🧪 Test Your SQL Code

Edit the SQL code on the left and click “Run Code” to see the result on the right.

📝 SQL Code
👁️ Preview (query result)

Click “Run Code” to see the result here.