Sort results and add rows

ORDER BY and INSERT: arrange and grow data.

1. What is this?

ORDER BY sorts results: ORDER BY age DESC means oldest first (ASC = ascending, smallest first). INSERT INTO adds a row: INSERT INTO users (name, age) VALUES ('Noah', 25) creates that record. A function like COUNT(*) counts rows.

2. Why do we use it?

Leaderboards need sorting; sign-ups need inserts. Reading plus writing covers daily work.

3. Syntax breakdown

  • SELECT name, age FROM users ORDER BY age DESC;

    Returns rows sorted oldest first.

  • INSERT INTO users (name, age) VALUES ('Noah', 25);

    Adds one row with those column values.

  • SELECT name FROM users ORDER BY name ASC;

    Alphabetical A→Z.

5. Common mistakes

  • Assuming rows have an order without ORDER BY — without it, order is undefined.
  • Omitting the column list in INSERT and getting the order wrong.
  • Sorting numbers stored as text (lexicographic order surprises).

Practice task

Insert yourself (your name and age), then select all users ordered by name A→Z.

Hint

INSERT ..., then SELECT * FROM users ORDER BY name ASC;

Try it below — tests run automatically when you press Run.

sql

Output

Press Run to see output.

Mini quiz

3 questions. Answer, then check.

  1. 1. What does ORDER BY age DESC do?

  2. 2. What does INSERT INTO do?

  3. 3. Is table order guaranteed without ORDER BY?

4. Example — edit and run

sql

Output

Press Run to see output.