SQL Introduction
SQL (Structured Query Language) is the standard language for storing, reading and changing data in relational databases. Banks, shops, hospitals, apps and test frameworks all keep their data in databases, and SQL is how you talk to them. This lesson explains what SQL is, how a database is organised and what kinds of commands SQL has, then you run your first queries.
- What SQL, a database and an RDBMS are
- How tables, rows, columns and keys fit together
- The difference between SQL and MySQL
- The five groups of SQL commands: DQL, DML, DDL, DCL and TCL
What is SQL?
SQL stands for Structured Query Language. It is pronounced “S-Q-L” or “sequel”. You use it to:
- read data: which customers ordered this month?
- add, change and delete data: record a new order, update a price, remove a test account.
- define the structure: create tables and the rules the data must follow.
- control access: decide who may read or change what.
SQL is declarative: you describe what result you want, and the database works out how to get it. You never write loops to search a table yourself.
Databases, tables, rows and columns
A relational database stores data in tables. A table looks like a spreadsheet:
- each column has a name and a data type, for example
salary INT; - each row is one record, for example one department;
- a primary key column holds a value that identifies each row uniquely, such as
dept_id.
Here is the departments table from the sample database used throughout this tutorial:
| dept_id | dept_name | location |
|---|---|---|
| 1 | Engineering | Bengaluru |
| 2 | Sales | Mumbai |
| 3 | Marketing | Pune |
| 4 | HR | Delhi |
| 5 | Finance | Mumbai |
Tables are linked by keys. Every employee row stores a dept_id, which points at a row in departments. That link is called a foreign key, and it is what makes the database relational.
What is an RDBMS?
An RDBMS (Relational Database Management System) is the software that stores the tables and runs your SQL. Popular ones:
| RDBMS | Made by | Typical use |
|---|---|---|
| MySQL | Oracle (open source) | Websites, SaaS apps, WordPress |
| PostgreSQL | Open source community | Apps, analytics, GIS |
| SQL Server | Microsoft | Enterprise and .NET apps |
| Oracle Database | Oracle | Large enterprise systems |
| SQLite | Open source | Mobile apps, browsers, small tools |
SQL vs MySQL
SQL is the language; MySQL is a database system that understands it. The core of SQL (SELECT, INSERT, JOIN, GROUP BY …) is an ISO standard and works almost the same everywhere. Each product adds its own extras. This tutorial uses MySQL 8.4 LTS, and every result you see was produced by a real MySQL 8.4 server. Where MySQL differs from other databases, the lesson says so.
Your first SQL queries
Ask the database which departments exist and where they are:
SELECT dept_name, locationFROM departments;| dept_name | location |
|---|---|
| Engineering | Bengaluru |
| Sales | Mumbai |
| Marketing | Pune |
| HR | Delhi |
| Finance | Mumbai |
FROM departmentstells MySQL which table to read.SELECT dept_name, locationpicks two of its three columns.- MySQL returns a result set with one row per department: 5 rows.
Now a question with a condition and a sort: who earns more than 90,000, highest first?
SELECT first_name, job_title, salaryFROM employeesWHERE salary > 90000ORDER BY salary DESC;| first_name | job_title | salary |
|---|---|---|
| Meera | CEO | 250000 |
| Asha | Engineering Manager | 125000 |
| Vikram | Sales Manager | 105000 |
| Rahul | Senior Developer | 98000 |
FROM employeesreads all 12 employees.WHERE salary > 90000keeps only the rows whose salary is above 90,000.SELECTpicks the three columns to show.ORDER BY salary DESCsorts from the highest salary to the lowest.
Every example with a Try it button runs in your browser. Edit the query, press Run (or Ctrl+Enter), and Reset to get the original back.
The five types of SQL commands
| Group | Stands for | Commands | Used to |
|---|---|---|---|
| DQL | Data Query Language | SELECT | read data |
| DML | Data Manipulation Language | INSERT, UPDATE, DELETE | change rows |
| DDL | Data Definition Language | CREATE, ALTER, DROP, TRUNCATE | define tables and other objects |
| DCL | Data Control Language | GRANT, REVOKE | control permissions |
| TCL | Transaction Control Language | START TRANSACTION, COMMIT, ROLLBACK | group changes into all-or-nothing units |
You will learn each group in this tutorial: queries first, because reading data is what you will do most.
Why learn SQL?
- It is one of the most requested skills for developers, testers, data analysts and data engineers.
- It has barely changed in decades, so what you learn now stays useful.
- Testers use SQL to set up test data and check what an application really saved, not just what the screen shows.
- Python, Java and every other language talk to databases by sending SQL.
Quick quiz
1. What does SQL stand for?
SQL is short for Structured Query Language.
2. Which statement best describes MySQL?
MySQL is an RDBMS. SQL is the language you use to talk to it.
3. Which command group does INSERT belong to?
INSERT, UPDATE and DELETE change rows, so they are Data Manipulation Language.
4. What links an employee row to its department?
The dept_id value in employees refers to the matching dept_id in departments.
SQL introduction FAQ
What is SQL used for?
SQL is used to read, insert, update and delete data in relational databases, to create tables and other database objects, and to control who can access the data.
Is SQL a programming language?
SQL is a declarative query language. Standard SQL describes what data you want rather than step-by-step instructions, although database systems add procedural features such as stored procedures.
What is the difference between SQL and MySQL?
SQL is the language. MySQL is a relational database management system that stores data and executes SQL statements. PostgreSQL, SQL Server and Oracle also use SQL.
Is SQL hard to learn?
No. Basic queries read almost like English, and most people write useful queries within a day. Joins, grouping and window functions take more practice, which is why this tutorial explains each query step by step.
Which database should a beginner start with?
MySQL is a good first choice: it is free, widely used and well documented. The SQL you learn here transfers to PostgreSQL, SQL Server and others with small changes.