Rullst AcademyRullst Academy/Lesson
Databases and SQL I · Lesson 03 / 12

Query with intent

Use SELECT, aliases, ORDER BY and LIMIT with explicit criteria.

Learning paths14 min

Let's discover together

  1. 1

    SELECT chooses expressions and FROM names the source. SELECT * helps exploration; reports should list necessary columns. This reduces noise and unnecessary data exposure.

  2. 2

    Without ORDER BY, do not rely on row order. stock DESC and id ASC break ties. LIMIT 2 limits rows; by itself it does not pick the two most relevant records.

  3. 3

    AS labels output without changing stored columns. stock * 2 AS planned calculates a hypothesis; it does not double stock. Distinguish stored facts from calculations in report names.

  4. 4

    Predict ids, run and change DESC to ASC. Query stock again to confirm SELECT changed no values. Add a tie breaker when the first column may repeat.

Your data workbench

Our model: kits.id ← loans.kit_id. One material can have many loans; names may repeat. We use fictional data only.

Predict before opening the reference. It shows verified output from the original example, not execution of your text. Run your changes with the kit on your computer.

-- Query with intent
SELECT id, label, stock * 2 AS planned FROM kits ORDER BY stock DESC, id ASC LIMIT 2;
Compare with reference output
[
  {
    "columns": [
      "id",
      "label",
      "planned"
    ],
    "rows": [
      [
        1,
        "Orbit",
        6
      ],
      [
        2,
        "Sprout",
        4
      ]
    ]
  }
]

Run the command in the extracted folder’s terminal. Use python3 on macOS/Ubuntu or py if that is your Windows launcher. Every run starts in memory; read README.md for saving files and recovery practice.

python lab.py run lesson.sql
My files and GitHub

The editor saves your .sql draft in this browser. Download it and replace lesson.sql in the kit; for the final project, availability.sql. The ZIP remains the original template. GitHub publishing sends only the confirmed file, without executing SQL or sending a database.

Official links and next step

✦
YOUR MISSION

Use SELECT, aliases, ORDER BY and LIMIT with explicit criteria.

Now it's your turn

Discovery workshop

Read, predict and check

Use SELECT, aliases, ORDER BY and LIMIT with explicit criteria.
Build your answers and check when you are ready.
A moment to understand

What makes the first results reproducible?

I need a hint

Without ORDER BY, do not rely on row order. stock DESC and id ASC break ties. LIMIT 2 limits rows; by itself it does not pick the two most relevant records.

Loading your journey…

Take it with you

List three materials by name and id showing only code and label. Explain tie breaking.

At your own pace

My files and GitHub

Every lesson can become your own record: code, notes or a practice plan. Saving a file to GitHub is optional and does not change your XP.

Student guide

The editor and downloads open when this lesson is available to your account. Check access below or return to the track map.

Checking your access to this lesson…

Rullstpetbot

AI can make mistakes. Check the lesson material.