Beginner: asking questions · Databases and the relational model · topic 5 of 62 · 25 min
The SQL language and your tools
Explanation
One language, many dialects
SQL (Structured Query Language, said "S-Q-L" or "sequel") became an ANSI standard in 1986 and an ISO standard in 1987. The standard is still updated; the latest version is SQL:2023. Every product follows the standard for the core (SELECT, WHERE, JOIN, GROUP BY) and adds its own extras. These versions are called dialects.
SQL is declarative: you describe the result you want ("customers in Pune"), not the steps to get it. The database works out how to find the rows. That is why the same short query works on 10 rows and on 10 crore rows.
| Task | SQLite | PostgreSQL | MySQL | SQL Server |
|---|---|---|---|---|
| First 3 rows | LIMIT 3 | LIMIT 3 | LIMIT 3 | TOP 3 |
| Auto-numbered id | INTEGER PRIMARY KEY | GENERATED ALWAYS AS IDENTITY | AUTO_INCREMENT | IDENTITY(1,1) |
| Quote a column name | "name" | "name" | `name` (backticks) | [name] or "name" |
The five statement families
| Family | Stands for | Statements | What they do |
|---|---|---|---|
| DDL | Data Definition Language | CREATE, ALTER, DROP, TRUNCATE | Make, change and remove tables and other objects |
| DML | Data Manipulation Language | INSERT, UPDATE, DELETE | Add, change and remove rows |
| DQL | Data Query Language | SELECT | Read rows |
| DCL | Data Control Language | GRANT, REVOKE | Give and take away permissions |
| TCL | Transaction Control Language | BEGIN, COMMIT, ROLLBACK, SAVEPOINT | Group changes so they succeed or fail together |
Some books put SELECT inside DML instead of giving it its own family; both groupings are common in interviews. SQLite has no users or passwords, so it has no GRANT or REVOKE. You will meet DCL with PostgreSQL in module 11 and TCL in module 8.
The small rules of writing SQL
- A semicolon ; ends a statement. With one statement it is often optional, but with several statements in a row it is required. Always add it.
- Keywords are not case-sensitive: select, SELECT and Select are the same. The common style is keywords in CAPITALS and names in lower_case.
- Data is case-sensitive: WHERE city = 'pune' finds nothing when the value is stored as 'Pune'.
- Text values go in single quotes: 'Pune'. A single quote inside text is doubled: 'Raj''s Cafe'.
- Double quotes are for identifiers, which are names of tables and columns: "order date" (a name with a space). MySQL uses backticks by default.
- Spaces and new lines do not matter. Put each clause on its own line so queries are easy to read.
Comments and aliases
-- A comment runs to the end of the line
/* A block comment
can span many lines */
SELECT name AS product_name, -- AS gives the column a new label
stock AS units_left
FROM products;An alias is a temporary name given with AS. It changes the heading in the result, not the table. Module 2 uses aliases a lot.
Your tools
- This site: SQLite runs inside your browser. Nothing to install, and every task in this course runs here.
- SQLite on your computer: the sqlite3 command-line program, or DB Browser for SQLite, a free window-based app.
- PostgreSQL: a free, powerful server database. psql is its command-line tool; pgAdmin is its free graphical tool.
- MySQL: very widely used, for example by WordPress. MySQL Workbench is its free graphical tool.
- DBeaver Community: one free app that connects to SQLite, PostgreSQL, MySQL, SQL Server and many more.
Common mistakes
- Missing semicolons between statements, so the second statement becomes part of the first and you get "near ...: syntax error". End every statement with ;.
- Using double quotes for text. Use 'single quotes' for values.
- Writing -- comments in MySQL with no space after the dashes. MySQL needs "-- " (dash, dash, space).
- Copying a query from a SQL Server tutorial (TOP 3) into SQLite. Check which dialect a tutorial uses.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
The SQL language and your tools · 1/6
SQL is a standard language, but each product speaks its own dialect, with small extras and differences.
Prefer a video by a teacher? Find free videos on “sql ddl dml dql dcl tcl explained for beginners” on YouTube ↗
In real life: One report, several databases
A data analyst at a retail company pulls sales from a PostgreSQL database for the app, a MySQL database for the old website and a SQL Server database for the warehouse. The core of her queries (SELECT, WHERE, GROUP BY) is the same everywhere; she only changes small dialect parts like LIMIT versus TOP. Learning standard SQL first is what makes moving between products easy.
-- Same question, two dialects
SELECT name, price FROM products ORDER BY price DESC LIMIT 3; -- SQLite, PostgreSQL, MySQL
-- SELECT TOP 3 name, price FROM products ORDER BY price DESC; -- SQL ServerPractice (0/4 done)
Write a query with a comment on the first line (starting with --). From products, return name labelled product_name and stock labelled units_left, using AS. The checker compares the values in both columns.
Sort these into families: CREATE TABLE, INSERT, SELECT, GRANT, COMMIT. Which list is right?
Which of these statements belongs to DDL?
Keywords are not case-sensitive, but data is. Write the query in lowercase keywords if you like: return the name of every customer whose city is exactly 'Pune' (the stored value has a capital P).
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Keys and relationshipsDatabases and the relational model · same module
- MySQL vs PostgreSQL vs SQLite vs SQL ServerProcedures, triggers, SQL engines and NoSQL
- What is a database?Backend Development · Databases and SQL basics
- INSERTChanging data, transactions and ACID
- Choosing a database, and keeping it safeProcedures, triggers, SQL engines and NoSQL
- Databases with database/sqlGo Programming (backend and cloud) · Building a REST API with a database
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What does "SQL is declarative" mean?
Answer: You describe the result you want and the database works out the steps
You say what you want; the database plans how to get it.
Which family do GRANT and REVOKE belong to?
Answer: DCL
Data Control Language gives and takes away permissions.
WHERE city = 'pune' returns no rows, but the table has 'Pune'. Why?
Answer: Text values are compared exactly, including capital letters
Keywords ignore case; data does not. 'pune' and 'Pune' are different values.
How do you write the text Raj's Cafe as a SQL value?
Answer: 'Raj''s Cafe'
Inside single-quoted text, a single quote is written twice.
Which tool connects to SQLite, PostgreSQL, MySQL and SQL Server from one app?
Answer: DBeaver
DBeaver is a universal database tool; psql and pgAdmin are for PostgreSQL, Workbench for MySQL.
How do you return only the first 5 rows in SQL Server?Related
Answer: SELECT TOP 5 ...
SQL Server uses TOP, or OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY with ORDER BY.
Which of these is a SQL database?Related
Answer: PostgreSQL
PostgreSQL stores data in tables and uses SQL. MongoDB is a NoSQL document database.
From What is a database?
In SQLite, you write INSERT INTO tickets (customer_id, subject, status) VALUES (3, 'Hi', DEFAULT). What happens?Related
Answer: A syntax error: SQLite does not accept DEFAULT in VALUES
Leave the column out instead. PostgreSQL, MySQL and SQL Server do accept DEFAULT there.
From INSERT
Why does typing ' OR '1'='1 into a careless login form return every row?Related
Answer: The quote ends the string early and OR '1'='1' is true for every row
The input becomes part of the SQL. Parameters keep input as data.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- This is the last topic of “Databases and the relational model”. The module exam comes next.
- Module 1 exam: Databases and the relational model · 15 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, SELECT: filtering and sorting. 62 topics left in the course before the final exam.