Tri des résultats

SQL Intermédiaire

Jasmin Ludolf

Data Science Content Developer, DataCamp

Tri des résultats

Une illustration montrant deux garde-robes côte à côte, la gauche présentant une pile de vêtements en désordre tandis que la droite montre une garde-robe organisée

SQL Intermédiaire

ORDER BY

SELECT title, budget
FROM films
ORDER BY budget;
|title               |budget|
|--------------------|------|
|Tarnation           |218   |
|My Date with Drew   |1100  |
|A Plague So Pleasant|1400  |
|The Mongol King     |3250  |
...
SELECT title, budget
FROM films
ORDER BY title;
|title                     |budget  |
|--------------------------|--------|
|#Horror                   |1500000 |
|10 Cloverfield Lane       |15000000|
|10 Days in a Madhouse     |12000000|
|10 Things I Hate About You|16000000|
...
SQL Intermédiaire

ASCending

SELECT title, budget
FROM films
ORDER BY budget ASC;
|title               |budget|
|--------------------|------|
|Tarnation           |218   |
|My Date with Drew   |1100  |
|A Plague So Pleasant|1400  |
|The Mongol King     |3250  |
...
SQL Intermédiaire

DESCendant

SELECT title, budget
FROM films
ORDER BY budget DESC;
|title                         |budget|
|------------------------------|------|
|Love and Death on Long Island |null  |
|The Chambermaid on the Titanic|null  |
|51 Birch Street               |null  |
...
SELECT title, budget
FROM films
WHERE budget IS NOT NULL
ORDER BY budget DESC;
|title                  |budget     |
|-----------------------|-----------|
|The Host               |12215500000|
|Lady Vengeance         |4200000000 |
...
SQL Intermédiaire

Tri des champs

SELECT title
FROM films
ORDER BY release_year;
|title                                |
|-------------------------------------|
|Intolerance: Love's Struggle Throu...|
|Over the Hill to the Poorhouse       |
|The Big Parade                       |
|Metropolis                           |
...
SELECT title, release_year
FROM films
ORDER BY release_year;
|title                   |release_year|
|------------------------|------------|
|Intolerance: Love's S...|1916        |
|Over the Hill to the ...|1920        |
|The Big Parade          |1925        |
|Metropolis              |1927        |
...
SQL Intermédiaire

Regrouper sur plusieurs champs avec ORDER

  • ORDER BY field_one, field_two
SELECT title, wins
FROM best_movies
ORDER BY wins DESC;
|title                           |wins|
|--------------------------------|----|
|Lord of the Rings:Return of t...|11  |
|Titanic                         |11  |
|Ben-Hur                         |11  |
  • Pensez que field_two est comme le tie-break au tennis
SELECT title, wins, imdb_score
FROM best_movies
ORDER BY wins DESC, imdb_score DESC;
|title                |wins|imdb_score|
|---------------------|----|----------|
|Lord of the Rings:...|11  |9         |
|Ben-Hur              |11  |8.1       |
|Titanic              |11  |7.9       |
SQL Intermédiaire

Différentes commandes

SELECT birthdate, name
FROM people
ORDER BY birthdate, name DESC;
|birthdate |name            |
|----------|----------------|
|1990-01-01|Robert Brown    |
|1990-02-02|Anne Smith      |
|1991-05-14|Amy Miller      |
|1991-11-22|Adam Waters     |
...
SQL Intermédiaire

Ordre d'exécution

-- Written code:

SELECT item FROM coats WHERE color = `yellow` ORDER BY length LIMIT 3;
-- Order of execution:

SELECT item
FROM coats
WHERE color = `yellow`
ORDER BY length
LIMIT 3;
SQL Intermédiaire

Passons à la pratique !

SQL Intermédiaire

Preparing Video For Download...