- Foundation
- About 13 min
- 5 practice questions
SQL: asking questions of a database
Read and write simple SQL commands such as SELECT, INSERT, UPDATE and DELETE with a WHERE condition.
If something looks wrong, please tell us.
SQL is the standard language for working with relational databases. Even without being a programmer, you can read a simple SQL statement, and exams often ask what one returns.
What SQL is
SQL stands for Structured Query Language. A query is a request for data. We will use the table Students with the fields Roll, Name, City and Marks:
| Roll | Name | City | Marks |
|---|---|---|---|
| 1 | Asha | Pune | 72 |
| 2 | Ravi | Jaipur | 55 |
| 3 | Meena | Patna | 90 |
| 4 | Imran | Pune | 63 |
The four main commands
| Command | Purpose | Example |
|---|---|---|
| SELECT | Read data | SELECT Name FROM Students WHERE Marks > 60; |
| INSERT | Add a record | INSERT INTO Students VALUES (5, 'Kiran', 'Surat', 80); |
| UPDATE | Change a record | UPDATE Students SET Marks = 58 WHERE Roll = 2; |
| DELETE | Remove records | DELETE FROM Students WHERE Roll = 4; |
WHERE filters the rows. Without WHERE, an UPDATE or DELETE affects every row, so always check it. Other useful pieces are ORDER BY (sort), COUNT, SUM, AVG, MAX and MIN.
Worked examples
Example 1. SELECT Name FROM Students WHERE Marks > 60; returns Asha (72), Meena (90) and Imran (63).
Example 2. SELECT AVG(Marks) FROM Students; returns (72 + 55 + 90 + 63) / 4 = 280 / 4 = 70.
Example 3. SELECT Name FROM Students WHERE City = 'Pune'; returns Asha and Imran.
Example 4. SELECT MAX(Marks) FROM Students; returns 90.
SELECT reads, INSERT adds, UPDATE changes, DELETE removes; WHERE picks the rows.
Key takeaways
- Know SELECT, INSERT, UPDATE and DELETE.
- WHERE limits the rows; leaving it out affects all rows.
- COUNT, SUM, AVG, MAX, MIN summarise data.
Check your understanding
-
1. Which SQL command changes values in an existing record?
- INSERT
- SELECT
- DELETE
- UPDATE
Show answer
Answer: D. UPDATE changes records; INSERT adds, SELECT reads and DELETE removes.
-
2. Students table: Asha (Pune, 72), Ravi (Jaipur, 55), Meena (Patna, 90), Imran (Pune, 63), with fields Roll, Name, City and Marks. What does SELECT COUNT(*) FROM Students WHERE Marks > 60; return?
- 3
- 2
- 4
- 1
Show answer
Answer: A. Marks above 60 are 72, 90 and 63, so three rows are counted. Ravi's 55 is left out.
-
3. Students table: Asha (Pune, 72), Ravi (Jaipur, 55), Meena (Patna, 90), Imran (Pune, 63), with fields Roll, Name, City and Marks. What does SELECT SUM(Marks) FROM Students; return?
- 270
- 280
- 70
- 290
Show answer
Answer: B. 72 + 55 + 90 + 63 = 280. (70 would be the average, 280 / 4.)
-
4. Students table: Asha (Pune, 72), Ravi (Jaipur, 55), Meena (Patna, 90), Imran (Pune, 63), with fields Roll, Name, City and Marks. What does SELECT Name FROM Students WHERE City = 'Pune'; return?
- Asha only
- Ravi and Meena
- Asha and Imran
- All four names
Show answer
Answer: C. Only Asha and Imran have City = Pune.
-
5. What happens when UPDATE Students SET Marks = 50; is run with no WHERE clause?
- Only the first record changes
- Nothing changes
- The whole table is deleted
- Every record's Marks becomes 50
Show answer
Answer: D. Without WHERE, an UPDATE affects every row, so all marks are set to 50.
Sign in to keep track of the lessons you finish.