Tutelage

Lessons › coding and computer science · taught by Leo

SQL basics: SELECT, WHERE, and ORDER BY

Learn how SQL uses SELECT to choose columns, WHERE to filter rows, and ORDER BY to sort query results.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

SQL asks for data

SQL is the language you use to ask a database for data.

You do not usually edit the whole table by hand. You ask for the rows you want.

Today we will practice the three big moves: pick columns, pick rows, and sort the result.

Big idea: SELECT filters output

A query starts with what you want to see, then narrows down which rows qualify.

Think of SQL like a search request for a spreadsheet with superpowers.

The database reads your query and returns a new result table.

The main job of basic SQL is to choose columns with SELECT and rows with WHERE.

Predict a basic query part

Which SQL word starts a query that asks for data?

  1. SELECT
  2. UPDATE
  3. DROP
Show answer

SELECT: Yes. SELECT is the standard start when you want to read data.

SELECT chooses columns

Let’s work with one simple table called students.

We want only the name and age columns, not everything.

FROM tells SQL where the data lives.

SELECT name, age FROM students

Quick check on column choice

If you want only name and age, what should the SELECT part list?

  1. name, age
  2. students, name
  3. WHERE name, age
Show answer

name, age: Correct. SELECT lists the columns you want back.

Worked example: one simple query

Here is a full query for one table.

First we choose the columns, then the table, then the filter.

The filter keeps only eleventh graders.

Then ORDER BY sorts the final rows by age.

So the database returns only the matching rows, in order.

WHERE grade = '11' ORDER BY age

Quick check on filtering

In a WHERE clause, what does SQL do?

  1. Chooses which rows match
  2. Chooses which columns show
  3. Deletes the table
Show answer

Chooses which rows match: Exactly. WHERE filters rows by a condition.

Why WHERE works

First, SQL looks at each row one by one.

Then it tests your condition, like age is greater than 15.

Only rows that make the condition true stay in the result.

Why ORDER BY changes view

Sorting does not change the data itself.

It only changes the order of the result you see.

That is why ORDER BY is usually at the end.

Watch out: commas and equals

Two easy mistakes are tiny but important.

Use commas between column names.

And use one equals sign for a match, not two.

name, age age = 16

Your turn: build the query

Which query best means: show name and grade from students, but only for grade 10?

  1. SELECT name, grade FROM students WHERE grade = '10'
  2. SELECT students FROM name WHERE grade = 10
  3. SELECT name grade FROM students ORDER BY grade
Show answer

SELECT name, grade FROM students WHERE grade = '10': Nice. That one has SELECT, FROM, and WHERE in the right roles.

Recap: the SQL flow

Use this checklist when you write a basic query.

First choose the columns.

Then choose the table, filter rows, and sort if needed.

That is the core of SQL basics.

SELECT FROM → WHERE → ORDER BY
Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app