Pulsars
0 %
Log inSign up

Querying and modifying data with SQL

SELECT and WHERE: querying data

Once the eleves table has been created and filled in, we will want to ask it questions. This is the role of the SELECT statement, the most widely used SQL query.

Let's go back to our table

id | nom      | prenom  | note
---+----------+---------+------
1  | Martin   | Lucie   | 15.5
2  | Dubois   | Karim   | 9.0
3  | Leroy    | Emma    | 12.0

Displaying all the data

SELECT * FROM eleves;

The asterisk * means "all columns". This query returns all three rows in full.

Choosing specific columns

SELECT nom, prenom, note FROM eleves;

Here, only the nom, prenom, and note columns are displayed; the id column is omitted.

Filtering with WHERE

The WHERE clause lets you keep only the rows that satisfy a condition:

SELECT nom, prenom, note
FROM eleves
WHERE note >= 10;

This query only returns students whose grade is greater than or equal to 10, that is Lucie Martin (15.5) and Emma Leroy (12.0), excluding Karim Dubois (9.0). You can also combine several conditions with AND and OR:

SELECT nom, prenom
FROM eleves
WHERE note >= 10 AND note < 15;

This last query only returns Emma Leroy, the only student whose grade falls within the requested range (the WHERE clause acts as a filter applied before the result is displayed).