Topic 9 ยท Databases ยท 9.2

SQL Explained Simply

SQL is the language used to ask a database for information and return a clear answer.

Invitation

How do you ask a database for exactly the information you need?

A database may contain thousands of records. Reading every row yourself would be slow and impractical.

SQL lets you write a question called a query. The database processes that query and returns an output.

SQL turns a question into an answer.
Figure 1
The SQL journey
DATABASE
โ†“
SQL QUERY
โ†“
OUTPUT
Big Idea

SQL means Structured Query Language

Structured Query Language is used to communicate with a database.

A query tells the database what information to retrieve. The database then returns only the data that matches the query.

QueryA question or instruction sent to a database.
OutputThe information returned by the database.
Exam rule: SQL is its own language. Do not rewrite an SQL statement as pseudocode.
How It Works

The same three-step pattern appears in every SQL lesson

Look at Figure 2. The database stores the table. The SQL query asks for information. The output shows the answer.

DATABASEStored information
โ†’
SQL QUERYAsk a question
โ†’
OUTPUTSee the answer
Database โ†’ SQL query โ†’ Output.
Worked Example

Ask for every student's name

The Students table contains four fields. The query asks the database to return only the Name field.

1. Database

StudentIDNameAgeHouse
101Alice15Blue
102Ben16Green
103Chen15Blue

2. SQL Query

SQLAsk
SELECT Name
FROM Students;

3. Output

Alice
Ben
Chen
Read the Query

Translate SQL into plain English

SQL QueryMeaning
SELECT Name
FROM Students;
SELECT NameChoose the Name field.
FROM StudentsUse the Students table.
Plain English: โ€œShow me every name from the Students table.โ€
Important Distinction

SQL is not pseudocode and it is not Python

SQL has its own keywords and its own structure. In a database question, write the SQL statement directly.

You do not need to translate it into programming code before the database can use it.

Focused answer: SQL stands for Structured Query Language and is used to query a database.
Figure 3
One language, one purpose
SQL QUERY
โ†“
DATABASE PROCESSES IT
โ†“
RESULT RETURNED
No pseudocode conversion is needed.
Exam Tip

Always work out what the query returns

SQL questions often show a table and a query, then ask you to write the output.

Read the query one line at a time and ask:

1Which fields are selected?
2Which table is used?
3What appears in the output?
Exam habit: Do not copy the whole table. Return only the fields and records requested.
Common Mistake

Do not confuse the query with the output

โœ— Query

SELECT Name FROM Students;

This is the instruction sent to the database.

โœ“ Output

Alice Ben Chen

This is the answer returned by the database.

Remember: the query asks; the output answers.
Summary

SQL follows one clear journey

1SQL means Structured Query Language.
2A query asks the database for information.
3The database returns an output.
Final rule: Database โ†’ SQL query โ†’ Output.