SQL Tutorial › Getting Started › SQL Introduction

SQL Introduction

BeginnerLesson 1 of 6910 min read2 queries run on MySQL 8.4

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.

Advertisement
In this lesson
  • 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:

departments · 5 rows
dept_iddept_namelocation
1EngineeringBengaluru
2SalesMumbai
3MarketingPune
4HRDelhi
5FinanceMumbai

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.

Advertisement

What is an RDBMS?

An RDBMS (Relational Database Management System) is the software that stores the tables and runs your SQL. Popular ones:

RDBMSMade byTypical use
MySQLOracle (open source)Websites, SaaS apps, WordPress
PostgreSQLOpen source communityApps, analytics, GIS
SQL ServerMicrosoftEnterprise and .NET apps
Oracle DatabaseOracleLarge enterprise systems
SQLiteOpen sourceMobile 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:

SQL
SELECT dept_name, locationFROM departments;
ResultMySQL 8.4
dept_namelocation
EngineeringBengaluru
SalesMumbai
MarketingPune
HRDelhi
FinanceMumbai
5 rows in set
How this query works, step by step
  1. FROM departments tells MySQL which table to read.
  2. SELECT dept_name, location picks two of its three columns.
  3. 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?

SQL
SELECT first_name, job_title, salaryFROM employeesWHERE salary > 90000ORDER BY salary DESC;
ResultMySQL 8.4
first_namejob_titlesalary
MeeraCEO250000
AshaEngineering Manager125000
VikramSales Manager105000
RahulSenior Developer98000
4 rows in set
How this query works, step by step
  1. FROM employees reads all 12 employees.
  2. WHERE salary > 90000 keeps only the rows whose salary is above 90,000.
  3. SELECT picks the three columns to show.
  4. ORDER BY salary DESC sorts from the highest salary to the lowest.
Tip

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.

Advertisement

The five types of SQL commands

GroupStands forCommandsUsed to
DQLData Query LanguageSELECTread data
DMLData Manipulation LanguageINSERT, UPDATE, DELETEchange rows
DDLData Definition LanguageCREATE, ALTER, DROP, TRUNCATEdefine tables and other objects
DCLData Control LanguageGRANT, REVOKEcontrol permissions
TCLTransaction Control LanguageSTART TRANSACTION, COMMIT, ROLLBACKgroup 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?

2. Which statement best describes MySQL?

3. Which command group does INSERT belong to?

4. What links an employee row to its department?

Advertisement

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.