Unable to open this exercise
Structured Query Language (SQL) is the standard declarative programming language used worldwide to retrieve, manipulate, and manage data within relational database management systems.
1. The Basic SQL Query Structure
To retrieve specific data from a database table, a query uses three fundamental clauses:
SELECT <Field1>, <Field2>, ...
FROM <TableName>
WHERE <SearchCondition>;
The Roles of the Three Clauses:
SELECT(What columns to display):
- Lists the fields (columns) you want returned in the final result table.
- To retrieve all fields from a table, use the asterisk wildcard symbol
*(e.g.,SELECT * FROM Products).
FROM(Where the data is stored):
- Specifies the exact table name containing the requested records.
WHERE(Which rows to include):
- Specifies the logical filter criteria. Only records for which the condition evaluates to
TRUEare extracted. - The
WHEREclause is optional; omitting it returns all records in the table.
2. SQL Comparison & Logical Operators
The WHERE clause uses standard mathematical comparison operators:
Operator | Meaning | Example |
|---|---|---|
| Equal to |
|
| Not equal to |
|
| Less than |
|
| Less than or equal to |
|
| Greater than |
|
| Greater than or equal to |
|
Combining Conditions with Logical Operators:
AND: Returns records only if all conditions separated byANDareTRUE.
WHERE ClassGrade = 10 AND FeesPaid = TRUE;
OR: Returns records if at least one condition isTRUE.
WHERE Subject = 'Physics' OR Subject = 'Chemistry';
NOT: Inverts the condition.
WHERE NOT (Status = 'Graduated');
3. Worked Example: Tracing Query Execution
Suppose we have a table named ExamResults:
+------------+--------------+-----------+-------+
| CandidateID| Subject | MarkScore | Passed|
+------------+--------------+-----------+-------+
| C101 | CS | 88 | TRUE |
| C102 | Maths | 92 | TRUE |
| C103 | CS | 44 | FALSE |
| C104 | CS | 76 | TRUE |
| C105 | Physics | 81 | TRUE |
+------------+--------------+-----------+-------+
Query:
SELECT CandidateID, MarkScore
FROM ExamResults
WHERE Subject = 'CS' AND MarkScore >= 50;
Step-by-Step Row Evaluation:
- Row 1 (
C101):Subject = 'CS'(TRUE) AND88 >= 50(TRUE) $\rightarrow$ MATCH (Included). - Row 2 (
C102):Subject = 'Maths'(FALSE) $\rightarrow$ NO MATCH (Skipped). - Row 3 (
C103):Subject = 'CS'(TRUE) AND44 >= 50(FALSE) $\rightarrow$ NO MATCH (Skipped). - Row 4 (
C104):Subject = 'CS'(TRUE) AND76 >= 50(TRUE) $\rightarrow$ MATCH (Included). - Row 5 (
C105):Subject = 'Physics'(FALSE) $\rightarrow$ NO MATCH (Skipped).
Result Table:
+------------+-----------+
| CandidateID| MarkScore |
+------------+-----------+
| C101 | 88 |
| C104 | 76 |
+------------+-----------+
4. Key Syntax Rules & Exam Pitfalls
- Text Literals: In SQL, text strings must always be enclosed in single quotation marks (e.g.,
'CS','London'). Numbers are written without quotes (e.g.,50). - Clauses Order: The order
SELECT ... FROM ... WHERE ...is fixed. PuttingFROMbeforeSELECTcauses a syntax error. - Punctuation: SQL statements conventionally conclude with a semicolon (
;).
Selecting every field
The asterisk below means all fields. It is not multiplication here.
SELECT * FROM Results;
Choose only the required fields when a question specifies them.
AI Socratic Tutor
Need guidance? Ask a question or use the hint below.
AI Socratic Tutor is an All-Access Benefit
Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.
You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.
Independent Practice: Select all fields
• SQL QueryReturn every field for records with Score at least 50, ordered by Score ascending.
Use the Results table: Name (TEXT), Team (TEXT), Score (INTEGER).
Results
Sample Dataset
|
Name
TEXT
|
Team
TEXT
|
Score
INTEGER
|
|---|---|---|
| Inez | Blue | 72 |
| Omar | Red | 49 |
| Suri | Blue | 90 |
| Tao | Red | 50 |
| Uma | Blue | 38 |
Use standard Cambridge SQL: SELECT, FROM, WHERE, ORDER BY, SUM(), COUNT().
Interactive exercises for Module 2 and beyond require an active plan.