Thuta Learning
SQL
BasicData & DatabasesBeginner

What is SQL?

SQLLesson 215 min

What you'll walk away with

  • Sort an SQL statement into its family: DQL, DML, DDL, DCL or TCL
  • Explain every part of SELECT * FROM Customers;
  • Describe how the SQL standard relates to dialects such as PostgreSQL, MySQL and SQLite
  • Explain why production code should avoid SELECT *

Explain it simply

Explain it simply

Think of a database as a huge library and SQL as the request slip you hand to the librarian. "Show me the customers from Mexico", "count the orders" and "change this customer's phone number" each become one SQL statement.

The librarian (the database) never asks you which aisles to walk down; it finds the fastest way itself and hands the answer back as a table. Reading, adding, changing and deleting data, and even creating new tables, all happen in this one language.

Problem → Why → Solution

The problem: the data is there, but you cannot ask it anything

An admin dashboard must list customers, a manager wants a monthly sales report, a search box must find products. All of that data already sits in the database, but without the language the database speaks, someone ends up opening exported CSV files by hand. Or they write the question in a form the database cannot understand, like this:

-- A wish, not a statement: the database rejects it
show me all customers please;
-- ERROR:  syntax error at or near "show"

Why does this happen?

A database does not understand human sentences; it understands SQL, which has precise keywords and grammar. Once you know the SELECT (what) and FROM (where from) shape, the same request becomes SELECT * FROM Customers; and the database answers immediately.

Core concept: the five families of SQL statements

SQL is the language for querying a relational database, inserting, updating and deleting its data, and managing its structure. Each piece of SQL is called a statement, and statements fall into five families by job. You will meet these family names often in documentation and job interviews.

FamilyExample statementsJob
DQL (Data Query)SELECTReads data, changes nothing
DML (Data Manipulation)INSERT, UPDATE, DELETEAdds, changes, removes rows
DDL (Data Definition)CREATE, ALTER, DROPBuilds and changes structure such as tables
DCL (Data Control)GRANT, REVOKEDecides who may do what
TCL (Transaction Control)BEGIN, COMMIT, ROLLBACKCommits or cancels changes as one unit

SQL is defined by an international standard, ISO/IEC 9075. PostgreSQL, MySQL, SQLite, SQL Server and Oracle follow its core and add their own extensions. So basics like SELECT, WHERE and JOIN look the same everywhere, while details such as date functions and quoting rules differ.

Databases such as PostgreSQL work client-server. The app (the client) sends the SQL statement to the server as text. The server parses it and checks the grammar, the planner picks the fastest strategy, and the executor runs it and sends back the result. For a SELECT that result is a table of rows; for a DML statement such as INSERT it is a count of affected rows.

Some people say "S-Q-L" letter by letter and others say "sequel"; both are accepted. What matters more is keeping two ideas apart: SQL is the language, and PostgreSQL is database software that understands it. "SQL database" is a category, not the name of one product.

Mental model: the journey of one query
  1. App sends SQL text
  2. Parser checks the grammar
  3. Planner picks a strategy
  4. Executor runs it and returns rows

A grammar mistake stops the query at the parser, before any data is touched.

General pattern

sql
SELECT * FROM table_name;          -- every column, every row
SELECT col_a, col_b FROM table_name; -- only the columns you name

* means "every column". Naming columns separated by commas returns just those columns. Without a WHERE clause, every row comes back.

Example: look at the whole Customers table

The course's sample Customers table holds four customers (Lesson 3 shows exactly how it is built). This statement reads every column and every row of it. It changes nothing, so it is safe to run as often as you like. Before you look at the output, guess how many columns and rows it will have.

all-customers.sql
SELECT * FROM Customers;

Code breakdown

  1. SELECTSELECT

    Marks a DQL statement that reads data. A SELECT never modifies the table.

  2. **

    Takes every column, in the order the table defines them. That is why the output has five columns, from customerid to country.

  3. FROM CustomersFROM Customers

    Names the table to read. Misspell it, or run it before the table exists, and you get relation "customers" does not exist.

  4. ;;

    Ends the statement. In psql, a missing ; makes the client wait for more lines because it thinks the statement is unfinished.

  5. Output

    Notice the headers are lowercase, customerid and customername: PostgreSQL folds unquoted names to lowercase. Lesson 4 explains this in detail.

You should see
 customerid |    customername     |  contactname   |    city     | country
------------+---------------------+----------------+-------------+---------
          1 | Alfreds Futterkiste | Maria Anders   | Berlin      | Germany
          2 | Ana Trujillo        | Ana Trujillo   | Mexico City | Mexico
          3 | Antonio Moreno      | Antonio Moreno | Mexico City | Mexico
          4 | Around the Horn     | Thomas Hardy   | London      | UK
(4 rows)

Why production code avoids SELECT *

SELECT * is great for exploring a table you have just met. Inside application code it causes three problems: unneeded columns (large text, password hashes) travel over the network, the shape of the result changes when someone adds a column later, and nobody reading the code can tell which columns are really used.

explicit-columns.sql
-- Better for an app screen: name exactly what the screen shows
SELECT CustomerName, City
FROM Customers;

This query still returns all four rows, but only two columns. Even if Phone or Notes columns are added to the table later, the shape of this result stays the same, so the app code that reads it keeps working untouched.

In real projects

Where this shows up in real projects

Opening the user list page of an admin dashboard makes the backend run a SELECT. Submitting a sign-up form runs an INSERT, editing a profile runs an UPDATE, and closing an account runs a DELETE. When a new release needs a new column, a developer writes a migration file containing DDL such as ALTER TABLE.

Knowing the families matters for security too. Connect a reporting tool through a database user that may only run SELECT, and even a bug in that tool cannot change or delete data. Those permissions are set with GRANT, a DCL statement. Code reviewers also read a change differently depending on its family: a new SELECT is low risk, while a DELETE or a DROP gets a much closer look.

Common mistakes

Common mistakes

1. Querying a table that does not exist yet

Copying SELECT * FROM Customers; into an empty database gives ERROR: relation "customers" does not exist. The syntax is fine; the table simply is not there. Run Lesson 3's setup script first, and in psql you can list existing tables with \dt.

2. Leaving SELECT * in application code

A SELECT * written for a prototype tends to survive into production. When someone later adds a large column, every page slows down, and sensitive columns can leak into API responses. Name only the columns the screen actually shows.

3. Pasting a MySQL example into PostgreSQL unchanged

MySQL habits such as table names in backticks (`) are syntax errors in PostgreSQL. When an online example fails, first ask whether it was written for another dialect.

Best practices

Best practices

  • Before running a statement, notice whether it only reads (SELECT) or changes something (DML or DDL).
  • Explore a new table with SELECT *, then switch to named columns when the query moves into app code.
  • When you copy an example from the web, note which database it was written for.
  • On a production database, give your own account only the permissions it really needs.

Mini challenge

Mini challenge: write one statement from three families

In an empty database, create a tiny notes table, put data in it and read it back. Label each statement's family in a comment.

  • DDL: CREATE TABLE notes with an integer id (PRIMARY KEY) and a text body column
  • DML: insert two rows with a single INSERT statement
  • DQL: read back the id and body columns by name (no SELECT *)
families.sql
-- DDL: create the notes table (id integer primary key, body text)
-- TODO

-- DML: insert two notes in one statement
-- TODO

-- DQL: read id and body back, naming the columns
-- TODO

Expected: The final SELECT returns two rows: 1 | Learn SELECT and 2 | Learn WHERE (or whatever text you inserted).

Show hint

Use the shape CREATE TABLE name (column type, column type);. INSERT INTO notes (id, body) VALUES (...), (...); adds several rows separated by commas.

Show solution
families.sql
-- DDL
CREATE TABLE notes (
  id   integer PRIMARY KEY,
  body text NOT NULL
);

-- DML
INSERT INTO notes (id, body) VALUES
  (1, 'Learn SELECT'),
  (2, 'Learn WHERE');

-- DQL
SELECT id, body FROM notes;

Quick quiz 1 / 3

Which family does UPDATE Customers SET City = 'Yangon' WHERE CustomerID = 1; belong to?

Quick quiz 2 / 3

What happens when you run SELECT * FROM Customers; in a brand-new, empty database?

Quick quiz 3 / 3

A mobile app's customer list shows only name and city. Which query fits best?

What to remember

  • SQL queries a relational database, changes its data and manages its structure.
  • Statements group into DQL (SELECT), DML (INSERT/UPDATE/DELETE), DDL (CREATE/ALTER/DROP), DCL and TCL.
  • There is an SQL standard, but PostgreSQL, MySQL and SQLite differ in dialect details.
  • SELECT * is fine for exploring; in app code, name the columns you need.

PostgreSQL SQL Tutorial — PostgreSQL Global Development Group

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

What is SQL? | Thuta Learning