What Is SQL

Updated · 12 min read
easyPostgreSQL 18sqlbasicssql-standard

SQL is the language you use to talk to a relational database. You write a short statement such as "give me the names of the students in California," the database finds the answer, and it sends back a table. The same language can add and change data, create database structures, manage permissions and control transactions.

SQL stands for Structured Query Language. It is one of the oldest languages still in everyday use, and one of the most useful: developers, data analysts, testers and product managers all write it.

You describe the answer in SQL; the database plans the steps and sends back a table

In One Minute

  • SQL is the language for relational databases: you send a statement, the database sends back a result.
  • For learning, its statements fall into four practical jobs: ask for data, change data, define structures, and manage access and transactions.
  • SQL is declarative: you describe the result you want, and the database works out how to get it.
  • SQL is an international standard. Each database supports its own mix of standard features and extras, called its dialect.
  • You can say it as "S-Q-L" or "sequel." Both are common and both are fine.

Your First Look at SQL

Here is a real question and its SQL. "Which students live in California?" Press Try it to run it on the Fee Tracker database:

SQL▶ Try it
SELECT first_name, city
FROM students
WHERE state = 'CA'
ORDER BY first_name;
Output
 first_name |     city
------------+---------------
 Advitha    | San Francisco
 Srihitha   | San Francisco
(2 rows)

Read it almost like English: select the first name and city from the students table, where the state is CA, ordered by first name. The result is itself a small table, with a line under the column names and a count of the rows at the bottom.

You'll learn every part of this statement in the lessons ahead. For now, notice what you did not write: you never said which file to open, how to loop through the rows, or how to sort them. You described the answer, and the database did the rest.

The Four Jobs of SQL

SQL statements do several kinds of work. For learning, we'll group them into four practical jobs. You'll also see the jobs described with short family names, such as DQL, DML, DDL, DCL and TCL, in job posts and documentation:

Practical jobCommon family namesMain statementsExample
Ask for dataDQL, data query language (often grouped with DML)SELECTWhich courses cost more than $400?
Change dataDML, data manipulation languageINSERT, UPDATE, DELETERecord a new payment
Define structuresDDL, data definition languageCREATE, ALTER, DROPCreate a table for teachers
Manage permissions and transactionsDCL, data control language (permissions), and TCL, transaction control language (transactions)GRANT, REVOKE (DCL); COMMIT, ROLLBACK (TCL)Let the accountant read payments; save a group of changes together

These family names are a handy way to organize statements, not strict rules. Different books and databases sometimes sort them differently; some, for example, count SELECT as DML. Focus first on what each statement does.

Each job, with a real statement and PostgreSQL's real response

Here are the first three jobs working together. This example creates a small table, adds two rows to it and then asks for them:

SQL▶ Try it
CREATE TABLE notes (
  note_id INTEGER PRIMARY KEY,
  body    TEXT
);
 
INSERT INTO notes (note_id, body)
VALUES (1, 'Bring the Java book'),
       (2, 'Pay the SQL Basics fee');
 
SELECT note_id, body
FROM notes
ORDER BY note_id;
Output
CREATE TABLE
 
INSERT 0 2
 
 note_id |          body
---------+------------------------
       1 | Bring the Java book
       2 | Pay the SQL Basics fee
(2 rows)

The database answered each statement in turn. CREATE TABLE means the table now exists, INSERT 0 2 means two rows were added, and then the SELECT returned them.

Describe the Result, Not the Steps

Most programming languages are step by step. In Java or JavaScript you tell the computer exactly what to do: open the list, look at each item, keep the ones that match, sort them, print them. That style is called imperative.

SQL is declarative. You describe what you want, and the database decides how. Think of ordering at a restaurant: you say "a veggie burger, no onions," not "walk to the fridge, take out the bun, slice a tomato." The kitchen knows the steps.

Imperative (step by step)Declarative (SQL)
Open the students listSELECT first_name
Look at every studentFROM students
Keep only those whose state is CAWHERE state = 'CA'
Sort the ones you kept by first nameORDER BY first_name;
You order the dish; the kitchen, like the database, works out the steps

Because the database plans the steps itself, you can usually keep the same query as a table grows from three rows to three million. The answer means the same thing, and the database may choose a different plan for the bigger table without you rewriting the query. Speed isn't automatic, though; later lessons show how indexes and good query design help.

One Request Works on Many Rows

SQL also works on whole sets of rows at once. Imagine the Fee Tracker had 1,000 students. You wouldn't ask the database about each student one by one; the query above already covers them all. The database checks the condition across the relevant rows and returns every match. This is called set-based processing: you describe what should happen to a set of rows, rather than writing a separate instruction for each row.

Here one short request looks at all seven payments and returns the ones over $300:

SQL▶ Try it
SELECT payment_id, amount_usd
FROM payments
WHERE amount_usd > 300
ORDER BY payment_id;
Output
 payment_id | amount_usd
------------+------------
          1 |     450.00
          4 |     550.00
(2 rows)

The same idea runs through filtering, joins and grouping, which you'll learn soon: each one works on a set of rows, not on one row at a time.

Where SQL Came From

SQL grew out of research at IBM in the 1970s. In 1970, the IBM researcher Edgar F. Codd described the relational model: keep data in tables and connect them through shared values. A few years later, in 1974, the IBM researchers Donald Chamberlin and Raymond Boyce designed a language called SEQUEL for working with relational data. It was renamed SQL in 1977 because of a trademark problem, and it evolved, with work from many others, into the language relational databases use today.

Companies soon built their own relational databases, and each one spoke a slightly different SQL. To keep them close, SQL became an official standard: first from ANSI (the American National Standards Institute) in 1986, then from ISO (the International Organization for Standardization) in 1987. The standard has been updated many times since; the edition published in 2023 is called SQL:2023.

From Codd's relational model in 1970 to SQL:2023

The Standard and the Dialects

Most relational databases support a large part of standard SQL, but each one implements its own combination of standard features and extras. That combination is called the database's dialect. It's like English: someone from Texas and someone from London understand each other, but they say a few things differently.

The SQL you'll use most (selecting, filtering, sorting, joining and grouping data, and INSERT, UPDATE and DELETE) is widely supported. Some syntax and behavior still differ between databases, such as how to return only the first few rows:

DatabaseReturn the first 3 rows
Standard SQLFETCH FIRST 3 ROWS ONLY
PostgreSQLLIMIT 3 or the standard form
MySQLLIMIT 3
SQL ServerSELECT TOP 3 ... or OFFSET 0 ROWS FETCH NEXT 3 ROWS ONLY
OracleThe standard form, or ROWNUM in older code
SQLiteLIMIT 3
A widely shared core, with differences in places such as limiting rows

This course teaches standard SQL first, runs every example on PostgreSQL, and clearly labels anything that works in only one database.

PostgreSQL is free to use, widely adopted and supports many standard SQL features. The Try it buttons run the examples in a browser-based learning environment, so you don't need to install anything.

How to Say "SQL"

You'll hear both "S-Q-L" (spelling out the letters) and "sequel." The standard spells the letters, many developers say "sequel" because of the language's original name, and some databases lean one way: Microsoft says "sequel server." Nobody will judge you for either one.

Common Mistakes

Thinking SQL is a programming language like Java. SQL describes data, not step-by-step programs. Many databases add programming features (loops, variables), but everyday SQL is a set of statements that each return a result or make a change.

Thinking every database speaks a different language. The everyday core is widely shared. Some syntax and behavior differ, and the course marks those places.

Learning a dialect's extras first. If the first thing you learn is SQL Server's TOP or MySQL's backticks, you'll be surprised by every other database. Start with the standard form and learn the extras when you need them.

Practice

Challenge: Write a query that returns the title and fee_usd of every course in the Fee Tracker database, sorted by title. Use the first example in this lesson as your model: change the column names, the table (courses) and the sort column, and leave out the WHERE line.

Show solution
SQL▶ Try it
SELECT title, fee_usd
FROM courses
ORDER BY title;
Output
       title       | fee_usd
-------------------+---------
 Git Basics        |  250.00
 Java Basics       |  450.00
 Java Collections  |  550.00
 Java OOP          |  500.00
 JavaScript Basics |  450.00
 SQL Basics        |  400.00
(6 rows)

Quick check: What is SQL

3 questions
1What does "declarative" mean for SQL?
2Which statement belongs to the "change data" family (DML)?
3What is a SQL dialect?

Key Takeaways

You want toSQL familyExample statement
Ask a questionDQLSELECT title FROM courses;
Add, change or remove rowsDMLINSERT, UPDATE, DELETE
Create or change a tableDDLCREATE TABLE, ALTER TABLE, DROP TABLE
Manage access or group changesDCL, TCLGRANT, REVOKE, COMMIT, ROLLBACK

Was this tutorial helpful?

Lesson 2 of 115