Introduction to SQL
In your Computer Science journey, you have learned how to store data in variables and arrays. But what happens when a business has millions of pieces of information, like a supermarket's stock list or a school's student records? They use a Database.
To talk to these databases and find the exact information we need, we use a special language called SQL (Structured Query Language). For your OCR J277 exam, you only need to master the three most important commands: SELECT, FROM, and WHERE.
Think of SQL like a professional "Search Tool." Instead of scrolling through thousands of rows, you write a tiny bit of code to grab exactly what you want!
The "Big Three" Commands
Every SQL query you write for this course will follow a specific order. If you change the order, the "computer" won't understand you. The order is always:
1. SELECT (The fields/columns you want to see)
2. FROM (The table where the data is stored)
3. WHERE (The specific rules or filters for the search)
Did you know? Even though you might learn complex programming, SQL is used by almost every major website in the world, from Netflix to Instagram, to manage their data!
1. The SELECT Command
The SELECT command tells the database which fields (columns) you want to display in your results.
Example: If you have a table of students, you might only want to see their names, not their home addresses or phone numbers.
The Asterisk \( * \): This is a special shortcut. If you want to see every single column in a table, you use SELECT *.
Memory Aid: "SELECT what you want to see."
2. The FROM Command
The FROM command is the easiest one! It simply tells the database which table it should look in. Large databases often have many different tables (e.g., a "Students" table, a "Teachers" table, and a "Lessons" table).
Example: FROM Students
Memory Aid: "FROM tells you the location."
3. The WHERE Command
The WHERE command is used to filter the data. Without this, you would just get a list of every single record in the table. WHERE allows you to set criteria.
To use WHERE, we use comparison operators that you have already seen in programming:
\( == \) : Equal to (Used to find an exact match)
\( != \) : Not equal to
\( < \) : Less than
\( <= \) : Less than or equal to
\( > \) : Greater than
\( >= \) : Greater than or equal to
Quick Tip: When searching for text (strings) in SQL, you must put the text inside quotation marks, like "Computing". Numbers do not need quotation marks.
Putting it All Together: A Real-World Example
Imagine we have a database table called Results that looks like this:
Name | Subject | Score
Abebi | Computing | 85
Charlie | Maths | 42
Daisy | Computing | 91
Edward | Science | 55
Scenario: We want to find the names of all students who scored more than 80 in Computing.
Our SQL Query would look like this:
SELECT Name
FROM Results
WHERE Score \( > \) 80
The Result:
Abebi
Daisy
Don't worry if this seems tricky at first! Just remember the "Shopping List" analogy: SELECT the items, FROM the specific aisle, WHERE the price is right.
Key Takeaways for the Exam
Order Matters: You must write them in the order SELECT, then FROM, then WHERE.
Be Specific: If the exam asks for all data, use SELECT *. If it asks for specific names or dates, list those field names after SELECT.
Check your Operators: Make sure you use the correct sign in the WHERE clause. For example, if a question says "at least 50", you must use \( >= 50 \), not just \( > 50 \).
Common Mistakes to Avoid
1. Swapping SELECT and FROM: Students often write FROM Table SELECT Field. This will not get marks! Always start with SELECT.
2. Missing Quotes: Forgetting to put quotes around text in the WHERE clause (e.g., WHERE Subject == Computing should be WHERE Subject == "Computing").
3. Spelling Field Names Wrong: Always use the exact field names provided in the table in the exam question.
Quick Review
1. What does the \( * \) do in a SELECT statement?
It selects every field (column) in that table.
2. Which command is used to filter results?
The WHERE command.
3. What is the correct order of the three SQL commands?
SELECT, then FROM, then WHERE.