Skip to content

Beginner: asking questions · Databases and the relational model · topic 5 of 62 · 25 min

The SQL language and your tools

Explanation

SQL is a standard language with small differences (dialects) in each database product. Its statements fall into families: DDL defines tables, DML changes rows, DQL reads them, DCL controls access and TCL controls transactions. You need only this site to practise; PostgreSQL, MySQL and DBeaver are free when you want a real install.

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.

TaskSQLitePostgreSQLMySQLSQL Server
First 3 rowsLIMIT 3LIMIT 3LIMIT 3TOP 3
Auto-numbered idINTEGER PRIMARY KEYGENERATED ALWAYS AS IDENTITYAUTO_INCREMENTIDENTITY(1,1)
Quote a column name"name""name"`name` (backticks)[name] or "name"

The five statement families

FamilyStands forStatementsWhat they do
DDLData Definition LanguageCREATE, ALTER, DROP, TRUNCATEMake, change and remove tables and other objects
DMLData Manipulation LanguageINSERT, UPDATE, DELETEAdd, change and remove rows
DQLData Query LanguageSELECTRead rows
DCLData Control LanguageGRANT, REVOKEGive and take away permissions
TCLTransaction Control LanguageBEGIN, COMMIT, ROLLBACK, SAVEPOINTGroup 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.
Careful: SQLite quietly accepts WHERE city = "Pune": it first looks for a column named Pune, and when there is none it treats "Pune" as text. PostgreSQL gives the error column "Pune" does not exist. Always use single quotes for text, so your SQL works everywhere.

Comments and aliases

sql
-- 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;
Returns 8 rows with the headings product_name and units_left, for example Basmati Rice 5kg | 40.

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.
Tip: Installing anything is optional for this course. When you want to, start with PostgreSQL and DBeaver: they are free, and PostgreSQL is a common choice for new backends.

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

The SQL language

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.

sql
-- 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 Server
Only the "first 3 rows" part changes.

Practice (0/4 done)

Task 1 · Code · SQL

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.

Task 2 · Question

Sort these into families: CREATE TABLE, INSERT, SELECT, GRANT, COMMIT. Which list is right?

Task 3 · Question

Which of these statements belongs to DDL?

Task 4 · Code · SQL

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

Question 1

1.What does "SQL is declarative" mean?

Question 2

2.Which family do GRANT and REVOKE belong to?

Question 3

3.WHERE city = 'pune' returns no rows, but the table has 'Pune'. Why?

Question 4

4.How do you write the text Raj's Cafe as a SQL value?

Question 5

5.Which tool connects to SQLite, PostgreSQL, MySQL and SQL Server from one app?

Read more (free)

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.

From MySQL vs PostgreSQL vs SQLite vs SQL Server

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.

From Choosing a database, and keeping it safe

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

  1. This is the last topic of “Databases and the relational model”. The module exam comes next.
  2. Module 1 exam: Databases and the relational model · 15 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, SELECT: filtering and sorting. 62 topics left in the course before the final exam.

← Previous: CREATE TABLE and constraints