Chapter 4

SQL Queries

SQL retrieves and changes data in a database; SELECT reads records, while INSERT, UPDATE and DELETE add, change and remove them.

SQL (Structured Query Language) is the language used to work with data in a database. A query is an instruction that asks the database to do something.

Reading data with SELECT

The most common query reads data. Its shape is SELECT field FROM table WHERE condition. SELECT names the fields (columns) you want, FROM names the table, and the optional WHERE keeps only records that match a condition.

Example

To list the names of students older than 12 from a Student table: SELECT Name FROM Student WHERE Age > 12. To show every column, use SELECT * FROM Student.

Changing data

Three more statements change the data:

  • INSERT adds a new record: INSERT INTO Student (Name, Age) VALUES ('Aina', 15).
  • UPDATE changes existing records: UPDATE Student SET Age = 16 WHERE Name = 'Aina'.
  • DELETE removes records: DELETE FROM Student WHERE Age < 12.

Key idea

SELECT reads, INSERT adds, UPDATE changes, DELETE removes. The WHERE clause chooses which records are affected — leaving it out affects every record.

The condition after WHERE can use comparisons such as =, > and <. For example WHERE Age > 12 keeps records whose Age is more than 12, while WHERE Name = 'Aina' keeps only Aina's record. Choosing the right condition is what makes a query return exactly the records you want.

Remember

  • SELECT … FROM … WHERE … reads matching records.
  • WHERE decides which records a query touches.
  • A DELETE without WHERE removes every record — be careful.

Stuck on this topic? A verified JomKelas tutor can walk you through it.

Find a verified tutor