Structured Query Language · Cambridge syllabus 9.4 · Lesson 1 of 4
SQL Basics
SQL describes requested data. SELECT chooses output fields and FROM identifies the table. A query result can contain selected columns without modifying the stored records.
By the end, you can
- Understand the purpose of SQL.
- Read SQL statements.
- Complete structured SQL queries.
- Select fields from a table.
SELECT queries
Structured Query Language is the standard language for working with a database. It is used to retrieve records, to add, change and delete them, and to create tables, and it is understood by almost every database system.
It is declarative, which is worth noticing: the query says which records are wanted, not how to find them. Finding them is the database’s problem, and it is why the same query works whether the table holds ten records or ten million.
A retrieval query has two required clauses. SELECT lists the fields wanted, and FROM names the table. SELECT Title, Author FROM Books returns just those two columns for every record, in the order the fields were listed. Writing an asterisk in place of the field list returns every field, which is convenient when exploring and wasteful in a finished program.
The conventions make a query readable: keywords in capitals, each clause on its own line, field names spelt exactly as they are in the table, and text values in quotation marks while numbers have none.
Reading an unfamiliar query is best done clause by clause: which table, which records, which fields, in what order. Written out, that is FROM, then WHERE, then SELECT, then ORDER BY — which is how the database itself works through it, even though SELECT is written first.
Worth remembering
- SELECT names the fields, FROM names the table.
- An asterisk in place of the field list returns every field.
- Text values go in quotation marks; numbers do not.
Worked example
SELECT Name, Score FROM Students returns those two fields from every record.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
SELECT Title FROM Books
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.