• 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:

Students
RollNameCityMarks
1AshaPune72
2RaviJaipur55
3MeenaPatna90
4ImranPune63

The four main commands

Core commands
CommandPurposeExample
SELECTRead dataSELECT Name FROM Students WHERE Marks > 60;
INSERTAdd a recordINSERT INTO Students VALUES (5, 'Kiran', 'Surat', 80);
UPDATEChange a recordUPDATE Students SET Marks = 58 WHERE Roll = 2;
DELETERemove recordsDELETE 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.

— Remember

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. 1. Which SQL command changes values in an existing record?

    1. INSERT
    2. SELECT
    3. DELETE
    4. UPDATE
    Show answer

    Answer: D. UPDATE changes records; INSERT adds, SELECT reads and DELETE removes.

  2. 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?

    1. 3
    2. 2
    3. 4
    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. 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?

    1. 270
    2. 280
    3. 70
    4. 290
    Show answer

    Answer: B. 72 + 55 + 90 + 63 = 280. (70 would be the average, 280 / 4.)

  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?

    1. Asha only
    2. Ravi and Meena
    3. Asha and Imran
    4. All four names
    Show answer

    Answer: C. Only Asha and Imran have City = Pune.

  5. 5. What happens when UPDATE Students SET Marks = 50; is run with no WHERE clause?

    1. Only the first record changes
    2. Nothing changes
    3. The whole table is deleted
    4. 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.

Next lesson → Integrity, security and database users

Privacy · Terms · Refunds · Delivery